Sticky Sessions
Session affinity: a load balancer sends each client back to the same server, by cookie, source address or a hashed key, which saves work but costs balance, scaling and resilience.
Overview
A load balancer normally chooses a server for every request on its own. Sticky sessions, also called session affinity, change that: after the first request, the load balancer keeps sending the same client to the same server, usually until a cookie expires.
People turn it on for two different reasons. The first is correctness: the application keeps a user's session (login, cart, a multi-step form) in one server's memory, so any other server would not know the user. The second is efficiency: a server that has already loaded a user's data into a local cache answers that user faster. Only the second is a safe reason, because it still works, just more slowly, when the user lands somewhere else.
Affinity is easy to switch on and quietly expensive. It spreads load less evenly, makes new servers slow to fill, and turns every server restart into lost sessions if state lives only in memory. The Stateful vs Stateless topic covers where to keep session state instead; this topic covers how affinity works and how to live with it.
What you will be able to do
- Explain the three ways a load balancer pins a client: a cookie, the source address, or a hash of a key.
- Describe how AWS load balancers and Kubernetes implement affinity, including their defaults and limits.
- Explain why sticky load is uneven and why new servers fill slowly, with numbers.
- Say what happens to a pinned user during a failure, a deploy and a scale-out, and how to reduce the damage.
- Decide when affinity is justified and when to remove the need for it.
Key concepts
- Session affinity
- Load balancer cookie
- Application cookie
- Source IP affinity
- Hashing on a key
- Uneven load
- Draining and deregistration
- WebSockets
Interview questions
- What are sticky sessions, and when would you use them?
- Compare cookie-based and IP-based affinity.
- Why do sticky sessions cause uneven load, and how bad is it?
- How do you deploy a service that uses sticky sessions without losing users?
- How does an AWS Application Load Balancer implement stickiness?
- What is the difference between session affinity and consistent hashing on a key?
- Your WebSocket chat service runs on 20 servers. Do you need sticky sessions?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio