HAProxy Enterprise Documentation 2.1r1
Session Persistence
Session persistence means that HAProxy Enterprise routes a client to the same backend server once they have been routed to that server once. It avoids the overhead of re-establishing a client's state on a new server with each request, since the same server is always chosen. Often, it is optimal to avoid session persistence altogether by building your services to be stateless, such as by storing state off of the application servers in a shared database. However, for certain applications this is not possible.
HAProxy Enterprise supports two forms of session persistence:
Cookie-based persistence
To enable session persistence based on an HTTP cookie:
Add the cookie
directive to your backend
section. It instructs HAProxy Enterprise to place a cookie in the client's browser, which it will use to remember which server the client was routed to before.
Place a cookie
parameter on each server
line. This sets the value that will be stored in the cookie and it should be unique for each server.
backend servers
mode http
cookie SERVER_USED insert indirect nocache
server s1 192.168.0.10:80 check cookie s1
server s2 192.168.0.11:80 check cookie s2
IP-based persistence
To enable session persistence based on the client's IP address:
Add a stick-table
line to your backend
section. It creates an in-memory storage to hold client IP addresses. Entries in the stick table expire after 30 minutes if they are not used.
Add the line stick on src
, which stores a client's IP address in the stick table and associate it with the server to which they were routed. On subsequent visits, they will use this same server.
backend servers
mode tcp
stick-table type ip size 1m expire 30m
stick on src
server s1 192.168.0.10:80 check
server s2 192.168.0.11:80 check
Next up
Traffic Routing