Sub topic 6
Session persistence and when to avoid it
This chapter assumes you know how HTTP requests and cookies work, and that you have read the chapters on balancing algorithms and Layer 4 vs Layer 7 balancing. You should be able to read a basic NGINX or HAProxy configuration.
The problem: state stored on one server
Algorithms such as round robin and least connections choose a backend for each request independently. That works when every backend can answer any request. It breaks when a backend keeps per-user state in its own memory, such as a login session or a shopping cart. If the next request lands on a different server, that server has no record of the user, and the user appears logged out.
Session persistence (also called sticky sessions or session affinity) makes the load balancer send a client's requests to the same backend for as long as that backend is available.
Persistence mechanisms
| Mechanism | How it works | Layer needed | Weakness |
|---|---|---|---|
| Source IP hash | Hash of the client IP selects the backend | L4 or L7 | Clients behind one NAT or proxy all land on one server |
| Load balancer cookie | The balancer sets a cookie naming the backend | L7 | Needs HTTP visibility, so TLS must be terminated |
| Application cookie | The balancer reads the session cookie your app already sets | L7 | Needs extra configuration and a cookie the balancer can parse |
| Header or URL hash | Hash of a chosen header or path selects the backend | L7 | Uneven distribution if the key is skewed |
Source IP hashing is the only option that works for encrypted traffic you cannot read. Cookie-based persistence is more precise, because it identifies a browser rather than a network address.
Example: cookie persistence in HAProxy
In this configuration, HAProxy inserts a cookie named SRV on the first response. Later requests carrying SRV=app1 go to app1.
backend app
mode http
balance roundrobin
option httpchk GET /healthz
http-check expect status 200
cookie SRV insert indirect nocache
server app1 10.0.0.11:8080 check cookie app1
server app2 10.0.0.12:8080 check cookie app2insert makes HAProxy create the cookie. indirect hides it from the backend. nocache tells shared caches not to store responses that carry it. The check keyword enables health checks. If app1 fails its check, HAProxy sends the client to app2 and updates the cookie.
In NGINX, ip_hash; inside an upstream block gives source IP persistence. Cookie-based persistence is available in NGINX Plus through the sticky directive.
What happens when a backend fails
Persistence is a preference, not a guarantee. When the chosen backend is marked down, the balancer must pick another one. The new server lacks the user's in-memory session, so the user loses it anyway.
Costs of sticky sessions
- Uneven load. A few heavy users pinned to one server can overload it while others sit idle. Least connections cannot help once clients are pinned.
- Harder deploys. Removing a server drops every session pinned to it. Connection draining reduces this but does not remove it.
- Weaker scaling. Adding a server does not relieve existing pinned users.
- Failure loses state. As shown above, a crashed server takes its sessions with it.
Prefer stateless backends
The better fix is to move session state out of the backend processes. Common options:
- Store sessions in a shared store such as Redis or a database.
- Put the session in a signed or encrypted token, such as a JWT, that the client sends with each request.
With either option, any backend can serve any request, and you can use any algorithm. Keep persistence for cases where you cannot change the application, or where affinity gives a real benefit, such as a per-server local cache or long-lived WebSocket connections.
Exercise
An application stores login sessions in each server's memory, and you use cookie persistence. One server crashes. What happens to the users pinned to it, and what change to the application avoids the problem?
Show answer
The balancer marks the server down and sends those users to another backend. That backend has no copy of their sessions, so they must log in again. Storing sessions in a shared store such as Redis, or in a signed token held by the client, lets any backend serve them without sticky sessions.