Important HAProxy Sticky Sessions Tutorial

History of HAProxy

HAProxy is used to Balance Load between Server. There is a various algorithm based on which it distributes the load between server. 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.

First Setup Simple Server Configuration to Practise HAProxy Sticky Sessions. In this, I have One Master Server(18.221.213.188) on which HAProxy is installed and 2 Slave Server(18.216.160.202 or 18.218.98.36) on which Application is running. If while installing you are getting the error “cannot bind socket [0.0.0.0:80]” then you can check if port 80 is up. If it is up then you need to kill port 80. After killing port 80 Error will be resolved and HAProxy will start working.

On Slave Server, I have simply created HTML File to serve.

Server Configuration

  1. 18.221.213.188 : HAProxy Installed
  2. 18.216.160.202 : Server 1 that need to be balanced
  3. 18.218.98.36 : Server 2 that need to be balanced
frontend haproxynode
        bind *:80
        default_backend backendnodes

backend backendnodes
        balance roundrobin
        server server1 18.216.160.202:80 check
        server server2 18.218.98.36:80 check

Frontend: Frontend covers the client-facing duties of a listening section.

Backend: Backend covers the server pool duties.

In the above example frontend relays all traffic to the backend with the name backendnodes by looking at the default_backend directives. If such a backend is not found as a proxy will fail with the new configuration and gave an error.

In this, I have used a round-robin. Round Robin is the most common algorithm used. It sends requests alternatively on the server. Round Robin Working:

HAProxy Sticky Sessions

HAProxy Sticky Sessions

Need of Stick Sessions

It attaches the Session of the User to a particular server. If any request is made from that session then the request is served from that Server only. In Sticky Session, it is 100% that a user will get redirected to a single server.

HAProxy Sticky Sessions

Solution 1:

Inject Cookie in the Client Browser. This will make the client tell HAProxy that I was redirected to Server 1. So Always redirect me to Server 1. Here SRVNAME is a cookie name that gets inserted. In this, we have S01 and S02 cookie names. If the same cookie makes the request again then the request is served from the same IP that it was served before.

frontend haproxynode
        bind *:80
        default_backend backendnodes

backend backendnodes
        balance roundrobin
        cookie SRVNAME insert
        server server1 18.216.160.202:80 cookie S01  check
        server server2 18.218.98.36:80 cookie S02  check

Solution 2:

If you have a PHP Application running then you can use PHPSESSID variables in HTTP Header. example appsession PHPSESSID len 64 timeout 3h request-learn prefix

backend app
    appsession PHPSESSID len 64 timeout 3h request-learn prefix
    server websrv01 192.168.132.143:80 cookie S01 check
    server websrv02 192.168.132.144:80 cookie S02 check

Hope you like our HAProxy Sticky Sessions Blog. If you face any issues the please let us know in the comment section. Please subscribe to our blog for upcoming blogs.

Read More: Docker Tutorial, Kubernetes Tutorial

Happy Coding!

Reference:

For More related on HAProxy Visit: http://www.haproxy.org/

Leave a comment

Your email address will not be published.


*