Load Balancing
     with Apache
   Bradley Holt (http://bradley-holt.com/)
@BradleyHolt (http://twitter.com/BradleyHolt)
About Me
Co-Founder and
Technical Director
Contributor
Author




         http://oreilly.com/catalog/9781449303129/   http://oreilly.com/catalog/9781449303433/
Apache HTTP Server
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
Alternatives
Software Load Balancers
HAProxy

Varnish

Pound

Perlbal

Squid

nginx

Linux-HA (High-Availability Linux) on Linux Standard Base (LSB)
Hosted Load Balancers
Amazon’s Elastic Load Balancing

Rackspace Cloud Load Balancers
Shared Nothing Architecture (SN)
Properties of a SN System
Each node operates independently

No single point of contention

PHP is shared nothing by default…
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)
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).
Load Balancing Examples
Required Modules
mod_proxy

mod_proxy_http (assuming you’re load balancing HTTP requests)

mod_proxy_balancer

mod_headers (for sticky sessions)

mod_rewrite (for advanced con gurations)
Basic Load Balancing
# 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
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).
Sticky Sessions in PHP
(your mileage may vary)
# 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
Create Your Own Sticky Sessions
# 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
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.
Route Based on HTTP Method
# 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
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.
Distributed Load Testing with Tsung
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
XML Con guration File
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd">
<tsung loglevel="notice" version="1.0">
  <!-- … -->
</tsung>
Client Side Setup
<!-- Client side setup -->
<clients>
  <client
     host="test-a"
     weight="1"
     maxusers="10000"
     cpu="4"
  />
  <client
     host="test-b"
     weight="1"
     maxusers="10000"
     cpu="4"
  />
</clients>
Server Side Setup
<!-- Server side setup -->
<servers>
  <server
     host="www"
     port="80"
     type="tcp"
  />
</servers>
Load Setup
<!-- Load setup -->
<load>
  <arrivalphase
    phase="1"
    duration="5"
    unit="minute"
  >
    <users
       arrivalrate="200"
       unit="second"
    />
  </arrivalphase>
</load>
Session Setup
<!-- Session setup -->
<session
  name="default"
  probability="100"
  type="ts_http"
>
  <thinktime
     value="1"
     random="true"
  />
  <request>
     <http
       method="GET"
       url="/"
       />
  </request>
</session>
Monitoring Setup
Tsung allows for monitoring
using Erlang, SNMP, or Munin
<!-- Monitoring setup -->
<monitoring>
  <monitor host="www" type="munin" />
  <monitor host="www1" type="munin" />
  <monitor host="www2" type="munin" />
  <monitor host="www3" type="munin" />
</monitoring>
Reports
From Scaling CouchDB by Bradley Holt (O’Reilly). Copyright 2011 Bradley Holt, 978-1-449-30343-3
Graphs
From Scaling CouchDB by Bradley Holt (O’Reilly). Copyright 2011 Bradley Holt, 978-1-449-30343-3
Questions?
Thank You
                Bradley Holt (http://bradley-holt.com/)
             @BradleyHolt (http://twitter.com/BradleyHolt)




Copyright © 2011 Bradley Holt. All rights reserved.

Load Balancing with Apache

  • 1.
    Load Balancing with Apache Bradley Holt (http://bradley-holt.com/) @BradleyHolt (http://twitter.com/BradleyHolt)
  • 2.
  • 3.
  • 4.
  • 5.
    Author http://oreilly.com/catalog/9781449303129/ http://oreilly.com/catalog/9781449303433/
  • 6.
  • 7.
    About Apache Open source Servesover 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
  • 8.
  • 9.
    Software Load Balancers HAProxy Varnish Pound Perlbal Squid nginx Linux-HA(High-Availability Linux) on Linux Standard Base (LSB)
  • 10.
    Hosted Load Balancers Amazon’sElastic Load Balancing Rackspace Cloud Load Balancers
  • 11.
  • 12.
    Properties of aSN System Each node operates independently No single point of contention PHP is shared nothing by default…
  • 13.
    Breaking SN Sessions requiresharing (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 Storesessions 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).
  • 15.
  • 16.
    Required Modules mod_proxy mod_proxy_http (assumingyou’re load balancing HTTP requests) mod_proxy_balancer mod_headers (for sticky sessions) mod_rewrite (for advanced con gurations)
  • 17.
  • 18.
    # Create aload 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 trafficto be balanced by number of requests (lbmethod=byrequests), bytes transferred (lbmethod=bytraffic), or by the number of currently pending requests (lbmethod=bybusyness).
  • 20.
    Sticky Sessions inPHP (your mileage may vary)
  • 21.
    # Create aload 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
  • 22.
    Create Your OwnSticky Sessions
  • 23.
    # Set acookie 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 privatenetwork 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.
  • 25.
    Route Based onHTTP Method
  • 26.
    # Enable mod_rewrite RewriteEngineOn # 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 guringmultiple 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.
  • 28.
  • 29.
    Tsung Distributes load testingacross 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
  • 30.
  • 31.
    <?xml version="1.0"?> <!DOCTYPE tsungSYSTEM "/usr/share/tsung/tsung-1.0.dtd"> <tsung loglevel="notice" version="1.0"> <!-- … --> </tsung>
  • 32.
  • 33.
    <!-- Client sidesetup --> <clients> <client host="test-a" weight="1" maxusers="10000" cpu="4" /> <client host="test-b" weight="1" maxusers="10000" cpu="4" /> </clients>
  • 34.
  • 35.
    <!-- Server sidesetup --> <servers> <server host="www" port="80" type="tcp" /> </servers>
  • 36.
  • 37.
    <!-- Load setup--> <load> <arrivalphase phase="1" duration="5" unit="minute" > <users arrivalrate="200" unit="second" /> </arrivalphase> </load>
  • 38.
  • 39.
    <!-- Session setup--> <session name="default" probability="100" type="ts_http" > <thinktime value="1" random="true" /> <request> <http method="GET" url="/" /> </request> </session>
  • 40.
  • 41.
    Tsung allows formonitoring using Erlang, SNMP, or Munin
  • 42.
    <!-- Monitoring setup--> <monitoring> <monitor host="www" type="munin" /> <monitor host="www1" type="munin" /> <monitor host="www2" type="munin" /> <monitor host="www3" type="munin" /> </monitoring>
  • 43.
  • 44.
    From Scaling CouchDBby Bradley Holt (O’Reilly). Copyright 2011 Bradley Holt, 978-1-449-30343-3
  • 45.
  • 46.
    From Scaling CouchDBby Bradley Holt (O’Reilly). Copyright 2011 Bradley Holt, 978-1-449-30343-3
  • 47.
  • 48.
    Thank You Bradley Holt (http://bradley-holt.com/) @BradleyHolt (http://twitter.com/BradleyHolt) Copyright © 2011 Bradley Holt. All rights reserved.