Part 5 of 8 · 2 min read
Sticky sessions
Keeping a user on one server, why it works, and why it hurts.
Think of a doctor who knows the patient's history by heart. Booking the same doctor each time saves reading the file, but if that doctor is on holiday, nobody else knows the history.
A load balancer can be told to send all of a user's requests to the same server. This is called sticky sessions, or session affinity.
Sticky session
A sticky session is a rule in the load balancer that keeps sending a user to the same server, usually by a cookie or by a hash of the user or session id.
Example: the balancer adds server=3 to a cookie. Later requests with that cookie go to server 3.
The book "System Design Interview" says sticky sessions are possible in most balancers, but add overhead. Adding or removing servers becomes much harder, and server failures are hard to handle. The book "Understanding Distributed Systems" adds that they can cause hotspots, since some sessions cost far more than others.
A worked example. Four servers share 8,000 users with sticky sessions, 2,000 each.
- A server dies. Its 2,000 users are sent elsewhere and each loses what was in that server's memory: carts, drafts, logins. That is 25 percent of all users hit by one failure.
- The new servers receive them. If one server is chosen for all of them, it suddenly carries 4,000 users, twice its share.
- Add a fifth server. The balancer does not move existing users. The new server stays almost idle until new users arrive, so the load stays uneven.
- A scale-down must wait until users leave, or break their sessions.
And the unevenness: if 50 users are heavy, say a company's bulk importer, 10 times the average, and 30 of them hash to the same server, that server is a hotspot.
When is a sticky session still right?
- A short, temporary speed-up. A server may keep a warm cache for a user, but is allowed to lose it. Losing it costs time, not correctness.
- Long-lived connections. A chat connection is tied to one server for its life (section 6).
- An old application that cannot be changed, as a stop-gap during a move to a stateless design.
The rule: stickiness may be an optimisation, but correctness must not depend on it.
Common mistake
Treating sticky sessions as a way to avoid a session store. The first server failure, deploy or scale-down will log users out or lose their data.
Check yourself:
- In the worked example, what share of users is hurt by one server failing?
- Name one case where stickiness is acceptable.