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(
126.96.36.199) on which HAProxy is installed and 2 Slave Server(
188.8.131.52) 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.
- 184.108.40.206 : HAProxy Installed
- 220.127.116.11 : Server 1 that need to be balanced
- 18.104.22.168 : Server 2 that need to be balanced
frontend haproxynode bind *:80 default_backend backendnodes backend backendnodes balance roundrobin server server1 22.214.171.124:80 check server server2 126.96.36.199: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
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.
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 188.8.131.52:80 cookie S01 check server server2 184.108.40.206:80 cookie S02 check
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.
For More related on HAProxy Visit: http://www.haproxy.org/