Practical Load
Balancing
Ride the Performance
Tiger
Illtil
Peter
Membrey
David Hows
Eelco
Plugge
Contents
About the Authors xvii
About the Technical Reviewers xviii
Special
Thanks to serverlove xixAcknowledgments
xxPreface xxi
Chapter
1: Introduction 1The Problem 1
The Solution 2
What Is Load
Balancing?
3Load Balancing,OldStyle 3
Load Balancing,NewStyle 3
Scaling Up 3
ScalingOut 4
Load
Balancing,
Finally
4What Makes the Web Tick? 4
Caching: WarpDrive for Your Web Site 5
Load Balancingwith DNS 5
ContentDeliveryNetworks 5
Proper PlanningPreventsPrettyPoor Performance 6
The Essentials 6
HTTP LoadBalancing 6 LoadBalancing YourDatabase 6
CONTENTS
LoadBalancingYour Network Connection 7
SSL LoadBalancing 7
ClusteringforHigh Availability 7
LoadBalancingin the Cloud 7
IPv6:Implementation andConcepts 7
Where To Go Next 8
Summary
8Chapter
2: How Web Sites Work 9Let the Games Commence! 10
ComingfromaNon-ITBackground 10
KickingOff the Process 10
Finding theSite with DNS 11
Connectingto the Server(Finally!) 11
On the Server itself 12 Talkingto the Database 12
QuickLook atCaching 13 Backto theClient 13
Taking
a Closer Look 13The Network 14
TCP 14
DNS 16
Speed, Bandwidth,and Latency 17
NetworkSummary 20
HTML and the Web 20
HTML 21
Whyis Text-BasedImportant? 21
Why isLinkingImportant? 22
HTMLSummary 23
«CONTENTS
Web Content 24
StaticContent 25
DynamicContent 25
CreatingDynamicContent 25
WebContent Summary 27
Databases: The Weakest Link 27
Summary
27Chapter
3: ContentCaching:
Keeping
the LoadLight
29What IsaCache? 30
Whistle
Stop
Tour 30Browser-basedCaching 31 Web Accelerators 31
Web Proxies 32
TransparentWeb Proxies 33
Edge-based Caching 35
PlatformCaching 35
Application Caching 36
DatabaseCaching 37
JusttheBeginning 37
Caching
Theory:
Why
Is ItsoHard? 37HTTP1.0CachingSupport 38
HTTP1.1 EnhancedCachingSupport 38
The Solution 39
Caching Isn'tasEasyasItLooks 40
Web Proxies 40
TheSquidProxyServer 41
GettingStarted 41
Troubleshooting 43
mCONTENTS
TransparentProxies 43
What's Going On 44
GettingaHelpingHand 45
Squid,the SwissArmyKnife ofProxies 46
Edge-based Caching: Introducing
Varnish 46Sane CachingbyDefault 47
InstallingVarnish 48
Getting UpandRunning 49
CustomizingVarnish 51
Summary
51Chapter
4: DNS Load Balancing 53DNS Details 53
The IPAddress 53
TheProblem 54
The Solution 54
Taking aStepBack 55
DNS in
Depth
55QueryingYourself 56
Advanced DNS Queries 57
DNS
Caching
58QueryingyourDNS Cache 59
DNSCache in Linux 60
The RealStuff 60
BIND9 61
DNS DB Header 62
DNSDatabase Entries 63
LoadingyourDB 65
CheckingtheConfig 65
CONTENTS
Common Issues 66
TestingyourDNS 66
DNS Load
Balancing
67Advantagesof DNSLoadBalancing 67
Issues with DNSLoadBalancing 68
Summary
69Chapter
5: ContentDelivery
Networks 71Choosing
aCDN Provider 72Getting
Started withRackspace
72Adding
Content to Your CDN Account 74The
Rackspace
Cloud Files API 77Integratingthe API into PHP 77
Authenticating with YourAPI Key 78
Connecting and Disconnecting 78
Workingwith Containers 79
Workingwith Files 85
OtherUseful Functions 91
Summary
92Chapter
6:Planning
for Performance andReliability...
...93yoU
MAke Dinner In TiME 94yoU 94
MAke 97
Dinner 97
In 98
TiME 100
TheImportanceofPlanning 101
SCONTENTS
Backups
103WhyBackupsAresoImportant 103
There MayBe Trouble Ahead 103
Automation isaMust 104
Tactical Backups 105
StrategicBackups 105
Incrementalvs. Full 106
Please, PleasePerform Test Restores! 106
Summary
106Chapter
7: LoadBalancing
Basics 109What Is Load
Balancing?
109What
Computing
Resources AreAvailable? 110Processor(CPU) 110
Memory(RAM) 111
CheckingPerformance ofCPUand RAM with top 111
Network 113
Storage (Disk) 114
Load
Balancing
in Practice 114Guiding
Principles
115UnderstandYourSystem 115
Planning 116
MonitoringandTesting 116
Summary
116Chapter
8: LoadBalancing
Your Web Site 117Measuring
Web Server Performance 117Accelerating Apache
HTTP 120Disabling UnneededModules 120
aCONTENTS
UsingCompression 121
FollowSymLinksandSymLinkslfOwnerMatch 123
Accelerating
nginx
123worker_processesandworker_cpu_affinity 124
Gzip Compression 125
Load
Balancing
Your Web Server 126TheSetup 127
Preparingthe Server 128
Preparingthe Workers 130
TestingtheLoad Balancer 131
Best of Both Worlds 132
Summary
134Chapter
9: LoadBalancing
Your Database 135Setting
up theMySQL
Cluster 135InstallingtheManagementSoftware 137
Configuringthe ManagementSoftware 138
Preparingthe Cluster'sDataNodes 140
Installingthe MySQLServer and NOB Daemon 141
Configuringthe NDB Daemon 142
StartingtheCluster Node's Services 143
Updating MySQL'sRoot User 144
TestingtheInstallation 145
Applying
LoadBalancing
147TheSetup 148
Settingupthe Load Balancer 148
Preparingthe Workers 150
Testingthe Load Balancer 150
Summary
151CONTENTS
Chapter
10: Network LoadBalancing
153Sharing
the Workload 153TCP/IP 154
TCP 154
IP 155
Routing
155The Load Balancer 157
IPVS 160
IPVSScheduling 160
IPVS Installation onUbuntu 162
IPVSInstallationonCentos 163
IPVSADM 164
Expanding
IPVS 166Advanced IPVS 167
Changing Scheduling Algorithm 167
Assigning WeightValues 168
Protocol andMultipleVirtual Servers 170 Another IP Address 171
Making
It Stick 173Summary
174Chapter
11: SSL LoadBalancing
175Whatare SSLandTLS? 175
Public-Key
Cryptography
176Trust and Certificate Authorities 177
TLS
Encryption
178TLS Load
Balancing
178CONTENTS
Configuring
SSL onYour WebServer 179Apache 181
Nginx 182
SSLAcceleration 182
Apache 183
Nginx
183SSL Termination 183
Configuring Nginx
184Testing
the SSL 185Further
Configurations
187Engine 187
Caching 188
Protocols 189
SpecificCiphers 189
LVS and Your SSL Terminator 191
Integrated
Load Balancer/SSL Terminator 192Summary
192Chapter
12:Clustering
forHigh Availability
193High
Availability
194Single
Point of Failure 195Clustering
197IPVS Failover 198
Install with Ubuntu 199
Install with Centos 200
Configure 200
CommonConfigProblems 203
ConfirmingYourSystem 204
ilCONTENTS
Testing
206Web Server
Specifics
207Ubuntu 207
Centos 207
Advanced
Config Options
208ha.cf 208
Idirectord.cf 208
Web Server 209
Summary
209Chapter
13: LoadBalancing
in the Cloud 211Cloud
Computing
211Virilization 212
Hypervisor 212
Virtualized Resources 215
Managing
Virtual Resources 216Balancing 217
Overprovisioning 219
Planning 219
Cloud
Elasticity
220Working
withaCloud Server 221Summary
224Chapter
14: IPv6:Implications
andConcepts
225IPv6 225
Hexadecimal Notation 225
mCONTENTS
IPv4 Exhaustion 226
Approach
to IPv6 227Advantages
of IPv6 227Implementation
228Internet Connection 229
DNS 230
Operating
System
230Networking
230Single GatewayNetwork 231
Dual Network 231
Application Support
231Apache 231
Nginx 232
Varnish Cache 232
Memcached 233
IPVS 233
Ldirectord 234
Heartbeat 234
Summary
234Chapter
15: Where to Go Next 235Recap 236
Monitoring
236Security
237AccessControl 238
Views 238
CommonExploitPrevention 239
*CONTENTS
Operating
System
Performance 240CompileYourself 240
CutDown 240
High-PerformanceOS 241
Planning 241
Summary
241Index.. ....243