What I'm Doing...

Sticky load balance with Apache HTTPD 2.2

Apache HTTPD 2.2 includes some interesting features for making a basic load balancer.

This configuration makes a sticky loadbalancer with 3 backend servers. It is not dependant on any specific configuration of the backend server such as JSESSIONID or PHPSESSIONID.
It will create a BALANCEID-cookie with a route to a backend server.


#You need at least these modules
#Header used for setting cookie
LoadModule headers_module ...
#Proxy
LoadModule proxy_module ...
LoadModule proxy_http_module ...
LoadModule proxy_balancer_module ...

#Set a cookie if BALANCER_ROUTE_CHANGED containing BALANCER_WORKER_ROUTE environment variable
Header add Set-Cookie "BALANCEID=hej.%{BALANCER_WORKER_ROUTE}e; path=/;" env=BALANCER_ROUTE_CHANGED

#Show balancer-manager
<Location /balancer-manager>
  SetHandler balancer-manager
  Order allow,deny
  Allow from all
</Location>

ProxyRequests Off

#Configure members for cluster
<Proxy balancer://jakeri>
  BalancerMember http://b1.jakeri.net:80 route=server1
  BalancerMember http://b2.jakeri.net:80 route=server2
  BalancerMember http://b3.jakeri.net:80 route=server3
</Proxy>

#Do not proxy balancer-manager
ProxyPass /balancer-manager !

#The actual ProxyPass
ProxyPass / balancer://jakeri/ stickysession=BALANCEID nofailover=Off

#Do not forget ProxyPassReverse for redirects
ProxyPassReverse / http://b1.jakeri.net/
ProxyPassReverse / http://b2.jakeri.net/
ProxyPassReverse / http://b3.jakeri.net/

I believe that this configuration is somewhat better compared to this guide.

I got to fix my css for the <code></code> :-/

  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • Reddit

10 comments to Sticky load balance with Apache HTTPD 2.2

  • Mark

    Is there a typo in your post? You are setting the value for the cookie to:

    BALANCEID=hej.%{BALANCER_WORKER_ROUTE}e

    But your routes are specified as:

    route=server1
    route=server2
    route=server3

    And your stickiness is specified as the value of the cookie which won’t match the route values:

    stickysession=BALANCEID

    Mark

  • No. I don’t think so.

    I believe you are referring to the “hej.”-part. Take a look at mod_proxy (http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass) documentation and you will see that the route-directive is “Route of the worker when used inside load balancer. The route is a value appended to session id.”.

    Take for instance a JSESSIONID-cookie for a tomcat. It usually look something like this JSESSIONID=42345234jfrfj4343.instance1, where the first part is the actual sessionid and the second part is the jvmroute set in server.xml.
    If you would use a tomcat you could just update the jvmroute in server.xml (in this example to server1, server2 and server3) for your servers and then change stickysession to JSESSIONID. No set header part needed at all.

    But sometimes you do not have good routing field set for each server or you are just lazy and want the proxy handle it.
    The httpd will elect one server based on the loadbalancing algorithm (default is byrequest) and set this value to the BALANCER_WORKER_ROUTE environment variable. If there is no cookie set (or a failure occurs) BALANCER_ROUTE_CHANGED will be 1 and write header SetCookie BALANCEID=hej.server1; path=/;. Next time when the same user arrives BALANCEID cookie is set and routed to server1.

    I hope my explanation was understandable. :-)

    Jakob

  • Simple GREAT!!!
    would you mind if i translate your post to PT_BR??

  • No at all.
    Spread the word and keep a link to the original post.

  • igor

    Nice but I have one question: what will happen with the sticky sessions in case of backend server failure? Let’s say the apache receives a request with a cookie for server1 but the server1 is down? Will it continue sending the requests to the dead server because of the sticky session?

    Thanks,
    Igor

  • Hi Igor,

    If a backend server is down Apache proxy balancer will set BALANCER_ROUTE_CHANGED to a new server and the user will get a new BALANCEID-cookie.

    /Jakob

  • Can you easily make Cookies created on from the apache load balancer expire, say 5 minutes into the future?
    mod_rewrite appears to create Cookies that can expire in 5 minutes but can it utilize the environment variables you are using, e.g. BALANCER_WORKER_ROUTE?
    There are times where Cookies with a short life span can be advantageous.

  • Ramesh M

    Would this be of any help for the syntax?
    Thanks,
    Ramesh M

  • Ramesh M

    Sorry, I missed out the link, here it is. Actually this is apache 2.3

    http://httpd.apache.org/docs/2.3/mod/mod_proxy_balancer.html

    Regards,
    Ramesh M

  • Ah, they have added an example to the Apache 2.3 documentation. Nice!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>