Send the same user to the same server every time, and why that is a trade-off.
Sticky sessions (also called session affinity) means the load balancer sends a user to the same server every time, instead of picking a new one for each request.
A normal load balancer treats every request on its own. Request 1 may go to Server A, and request 2 from the same user may go to Server B. That is fine when servers keep no memory of the user (they are stateless).
But some apps keep session data inside the server: who is logged in, what is in the cart. If request 2 lands on Server B, Server B has never heard of this user. The user is asked to log in again. Sticky sessions fix this by pinning a user to one server.
| Method | How it works | Good and bad |
|---|---|---|
| Load balancer cookie | The balancer creates a cookie that names the server | Works at layer 7, precise per browser. Needs HTTP cookies, so a plain TCP balancer cannot do it. |
| Application cookie | The balancer reuses the app's own session cookie | Matches the real session life time. The balancer must know the cookie name and keep a table. |
| Source IP hash | Hash of the client IP picks the server | Works at layer 4, no cookie. Many users behind one office or mobile network share an IP and pile onto one server. |
| Consistent hash on a key | Hash a header, user ID or URL, using consistent hashing | Few users move when servers change. Needs the key to be in every request. |
| Connection stickiness | Keep all requests of one TCP connection on one server | Natural for WebSockets. Does not help the next connection. |
Move the session out of the server into a shared store such as Redis, a database, or into a signed token held by the client (see Session vs Token Based Auth). Then every server can serve every request, and you do not need stickiness at all.
A simplified model with expected values and no random numbers. User loads are illustrative. With spread routing and sessions in memory, a request finds its session only if it lands on the one server that holds it.
# Typical idea, shown generically affinity: cookie cookie_name: LB_SERVER cookie_lifetime: session # or e.g. 30 minutes # Always check the docs for the exact names in your load balancer.
| Term | Meaning in easy words |
|---|---|
| Session | The data a server keeps about one user between requests (login, cart) |
| Session affinity | Another name for sticky sessions: the same user goes to the same server |
| Sticky cookie | A cookie the load balancer sets to remember the chosen server |
| Source IP hash | Choosing a server by hashing the client IP address |
| Stateless server | A server that keeps no user data between requests, so any server can serve any request |
| Stateful server | A server that remembers user data between requests |
| Session store | A shared place (Redis, database) where all servers keep sessions |
| Connection draining | Letting existing sessions finish before a server is taken out of rotation |
| Hot server | A server that gets much more load than its peers |
| Session replication | Copying each session to other servers so one failure does not lose it |
| Failover | Moving traffic to another server when one fails |
| Choice | We gain | We lose |
|---|---|---|
| Sticky sessions | Local session memory just works | Even load and easy failover |
| Shared session store | Any server can serve any user, failures are invisible | One more system to run, and a network call per request |
| Signed token in the client | No server-side session at all | Harder to revoke early, larger requests |
| Session replication between servers | Survives one server failing | Extra network traffic and complexity, grows with the cluster |
| Cookie stickiness | Fair per browser | Needs layer 7 and HTTP cookies |
| IP hash stickiness | Works at layer 4 without cookies | Shared IPs create hot servers |