This is a presentation made at the Burlington, Vermont PHP Users Group about configuring load balancing using the Apache HTTP Server. Load balancing is a technique that can distribute work across multiple server nodes—here we will discuss load balancing HTTP (i.e. web) traffic. There are many software and hardware load balancing options available including HAProxy, Varnish, Pound, Perlbal, Squid, nginx, and Linux-HA (High-Availability Linux) on Linux Standard Base (LSB). However, many web developers are already familiar with Apache as a web server and it is relatively easy to also configure Apache as a load balancer.
Related concepts such as shared nothing architecture are discussed. We also take a look at some basic load balancing scenarios and features including sticky sessions and proxying requests based on HTTP method. Distributed load testing with Tsung is briefly discussed as well.
7. About Apache
Open source
Serves over 100 million websites
Can run in many different modes, depending on your needs,
via MultiProcessing Modules (MPMs)
The Apache Software Foundation (ASF) supports many other
open source software projects
12. Properties of a SN System
Each node operates independently
No single point of contention
PHP is shared nothing by default…
13. Breaking SN
Sessions require sharing (or require the use of sticky sessions)
Databases are the most common single point of contention
(this is why eventual consistency is important)
Storing variables in memory between requests breaks SN
(possible in Java and .NET)
14. Practical SN Techniques
Store sessions in a memcached cluster—this makes web nodes SN
For database applications:
• send writes to a write-only master
• replicate to multiple read-only nodes
• read-only nodes are now effectively SN
Clustering/partitioning/sharding can help, too (but painful).
Alternatively, use a “NoSQL” database (e.g. CouchDB is SN).
18. # Create a load balancer named "web-nodes"
<Proxy balancer://web-nodes>
# Add three load balancer members
BalancerMember http://www1.example.com
BalancerMember http://www2.example.com
BalancerMember http://www3.example.com
</Proxy>
# Send all requests to the "web-nodes" balancer
ProxyPass / balancer://web-nodes
19. Apache allows traffic to be
balanced by number of requests
(lbmethod=byrequests), bytes
transferred (lbmethod=bytraffic),
or by the number of currently pending
requests (lbmethod=bybusyness).
21. # Create a load balancer named "web-nodes"
<Proxy balancer://web-nodes>
# Add three load balancer members
BalancerMember http://www1.example.com
BalancerMember http://www2.example.com
BalancerMember http://www3.example.com
# Use the PHPSESSID for sticky sessions
ProxySet stickysession=PHPSESSID
</Proxy>
# Send all requests to the "web-nodes" balancer
ProxyPass / balancer://web-nodes
23. # Set a cookie
Header add Set-Cookie
"NODE=%{BALANCER_WORKER_ROUTE}e; path=/"
env=BALANCER_ROUTE_CHANGED
# Create a load balancer named "web-nodes"
<Proxy balancer://web-nodes>
# Add three load balancer members
BalancerMember http://www1.example.com route=1
BalancerMember http://www2.example.com route=2
BalancerMember http://www3.example.com route=3
# Use the NODE cookie for sticky sessions
ProxySet stickysession=NODE
</Proxy>
# Send all requests to the "web-nodes" balancer
ProxyPass / balancer://web-nodes
24. Use a private network to connect your
load balancer to your balancer
members.
This allows for dedicated bandwidth
and opens up the possibility of
offloading SSL handling to the load
balancer.
26. # Enable mod_rewrite
RewriteEngine On
# Send POST, PUT, and DELETEs to "write" balancer
RewriteCond %{REQUEST_METHOD} ^(POST|PUT|DELETE)$
RewriteRule ^/(.*)$ balancer://write$1 [P]
# Send GET, HEAD, and OPTIONS to "read" balancer
RewriteCond %{REQUEST_METHOD} ^(GET|HEAD|OPTIONS)$
RewriteRule ^/(.*)$ balancer://read$1 [P]
# Modify HTTP response headers (e.g. Location)
ProxyPassReverse / balancer://write
ProxyPassReverse / balancer://read
27. Consider con guring multiple load
balancers, removing the load balancer
as a single point of failure.
This typically involves having two or
more load balancers sharing the same
IP address, with one con gured as a
failover.
29. Tsung
Distributes load testing across multiple testing clients
Can generate huge numbers of concurrent users
Monitors CPU, memory, load, and network traffic
Simulates dynamic sessions, as described in a con guration le
Randomizes traffic patterns based on de ned probabilities
Recording and playback of sessions
HTML reports and graphs