Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Balancing (HAProxy or other) - Sticky Sessions

I'm working on scaling out my app to multiple servers, and one requirement is that a client is always communicating with the same server (too much live data is used to allow bouncing between servers efficiently).

My current setup is a small server cluster (using Linode). I have a frontend node running HAProxy using "balance source" so that an IP is always pointed towards the same node.

I'm noticing that "balance source" is not a very even distribution. With my current test setup (2 backend servers), one server often has 3-4x as many connections when using a sample size of 80-100 source IPs.

Is there any way to achieve a more balanced distribution? Obviously sticky sessions prohibits a "perfect" balance, but a 40/60 split would be preferred to a 25/75 split.

like image 903
Michael Marsee Avatar asked Jun 27 '11 19:06

Michael Marsee


People also ask

What is the difference between HAProxy and load balancer?

Difference between ELB and HAProxyELB can handle only less number of concurrent connections when compared with HAProxy. HAProxy does not support SSL out of the box. ELB has SSL support, SSL offload or SSL termination. Algorithm options are available for HAProxy whereas ELB supports only Roundrobin.

What is sticky session HAProxy?

HAProxy Sticky Sessions is necessary if we want to stick requests to serve from a single server rather than distributing load between servers. Basically, a Cookie Name is assigned to request and when the same user with the same session requests another time then the request is served from the same IP.

Which load balancer supports sticky sessions?

To use sticky sessions, the client must support cookies. Application Load Balancers support both duration-based cookies and application-based cookies. Sticky sessions are enabled at the target group level.

What are load balancer sticky sessions?

What is a sticky session. Session stickiness, a.k.a., session persistence, is a process in which a load balancer creates an affinity between a client and a specific network server for the duration of a session, (i.e., the time a specific IP spends on a website).


1 Answers

HAProxy supports modifying or inserting a cookie to provide session persistence with the cookie parameter.

In either backend or listen sections, add the following:

cookie COOKIENAME prefix

This example will modify an existing cookie by adding the name of the server to a cookie called COOKIENAME. Your client will see something like server1~someotherdata but your application will only see the someotherdata part. So you can use this on existing cookies. Additionally this method allows you to only enforce session persitence when that cookie exists, meaning you can still evenly balance people around the static portions of your site and only enforce stickyness when needed, but adding that cookie name to the session.

Also name your servers, so your server lines look like the following:

server server1 1.2.3.4 cookie server1

More detail is in the HAProxy config guide, it also looks like you can use the appsession config parameter as well.

Once you've done this, you can pick your own balance method from the list, I tend to use roundrobin but leastconn might give you a better balance once sticky sessions are taken into account.


More from documentation to make it easier to find reference section:

cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
              [ postonly ] [ preserve ] [ domain <domain> ]*
              [ maxidle <idle> ] [ maxlife <life> ]
  Enable cookie-based persistence in a backend.
  May be used in sections :   defaults | frontend | listen | backend
                                 yes   |    no    |   yes  |   yes
like image 95
Richard Benson Avatar answered Oct 18 '22 16:10

Richard Benson