Enabling Sticky-Mode for HAProxy - the easy way.

Switching the load balancing algorithm from round robin to the source algorithm (seems to be) an easy way to enable sticky-mode on your HAProxy load balancer.
created by on 2011-03-18

Ever since I put a load balancer in front of my web servers lots of the session-based features of my site have stopped working.

The reason for this was simple. I installed HAProxy with a very basic
configuration using a Round-Robin load balancing algorithm – the session variable was created on web server A while the user was one web server B and vice versa.

listen LoadBalancer 95.84.73.62:80
    mode http
    stats enable
    balance roundrobin
    option httpclose
    option forwardfor
    option httpchk HEAD /check.txt HTTP/1.0
    server inst1 127.0.0.15:80 cookie ServerA check inter 2000 fall 3
    server inst2 127.0.0.16:80 cookie ServerB check inter 2000 fall 3
    capture cookie vgnvisitor= len 32

So it was implossible to use functions which depend somehow or another on session-variables:

… without having a hosts file entry for a specific server activated.

But today, I finally took the time to read the HAProxy documentation.
And the solution to my problem seems quite easy and obvious:

In order to enable sticky-mode on my load balancer I can use the “source” load balancing algorithm which leads users from the one specific IP-address always to the same server, unless this server is down.

listen LoadBalancer 95.84.73.62:80
    mode http
    stats enable
    balance source
    option httpclose
    option forwardfor
    option httpchk HEAD /check.txt HTTP/1.0
    server inst1 127.0.0.15:80 cookie ServerA check inter 2000 fall 3
    server inst2 127.0.0.16:80 cookie ServerB check inter 2000 fall 3
    capture cookie vgnvisitor= len 32

Since I don’t need “real” load balancing on my servers, I think this option will be just fine for me.
But I am not sure if there is a better way to enable sticky-mode on your load-balancer.

I briefly tried using cookies, but it did not work for me. If you have any ideas on how to make the cookie solution work please drop me line.

Mahalo
– Andreas Koch

Shortlink:
Tags: