Step 1 · The pattern
The session lives on one server
Each server keeps its users' sessions (login, cart) in its own memory. Only server 2 knows users B and D, so the load balancer must send them to server 2 every time. That is a sticky session.
- request
- served
In short
Sticky sessions: how it works and how it breaks
A session is what the server remembers about a logged-in user: who they are, what is in their cart. If each server keeps sessions in its own memory, only that server can serve that user, so the load balancer has to be sticky: it sets a cookie on the first response and uses it to send every later request to the same server. This works until the server restarts, is redeployed or dies. Its memory is gone, its users are logged out, and the balancer can only hand them to a server that has never heard of them. Pinned users also cannot be moved, so one heavy user keeps one server busy while others sit idle. The fix is to keep session state outside the servers: in a shared session store, or in a signed token the client carries. Then any server can answer any request.
How this comes up in interviews
"After every deploy some of your users are logged out and lose their carts. The servers sit behind a sticky load balancer. What is going on and how do you fix it?"
Then they ask
- You move sessions to a shared store. What new failure modes have you added, and how do you limit them?
- What do signed tokens carried by the client give you compared with a store, and what do they make hard?
- Is there any case where you would still keep stickiness on? How do you tell that it is safe?
A strong answer covers
- Stickiness is a workaround for session state held in one server's memory: a cookie pins each user to the server that knows them.
- A restart, redeploy or crash wipes that memory, and no other server ever had those sessions, so the users are logged out.
- Pinned users cannot be moved, so one heavy user keeps one server busy while others sit idle.
- Fix it by keeping session state outside the servers, in a shared session store or a signed token, so any server can answer any request.
- The store is then on every request's path, and tokens are hard to revoke. Affinity is fine only where losing it costs speed and nothing else.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: AWS Application Load Balancer: sticky sessions (target group attributes) ↗ · nginx: Using nginx as HTTP load balancer (session persistence, ip_hash) ↗ · The Twelve-Factor App: VI. Processes ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.