Core idea
A stateless server remembers nothing between requests, so any server can answer any request. Move session data into a shared store and servers become interchangeable, which is what makes adding and removing them painless.
Mental model
A stateful server is a waiter who memorises your order and never writes it down. Only that waiter can serve you, and if they go home your order is lost. A stateless setup writes every order on a shared board; any waiter can pick up where another left off, so you can bring in extra waiters at lunch rush and send them home after.
Why it matters
- Auto-scaling needs servers you can add or remove at will
- Failures stop mattering: a replaced server loses no user state
- Sticky sessions pin users to servers, making load balancing uneven and failover painful
What to know
- Stateful: session or profile data lives in one server’s memory, so requests must return to that same server
- Stateless: session data lives in a shared store such as Redis or a NoSQL database
- Sticky sessions are a workaround, not a goal; they add overhead and complicate scaling in
- A shared session store should itself be replicated and scalable
- Auto-scaling adds or removes web servers automatically based on load such as CPU or request rate
- Tokens like JWTs can carry identity so even the session lookup disappears
Common traps
- Keeping uploaded files on a web server’s local disk, so other servers cannot see them
- Treating the session store as free: it becomes a new single point of failure if not replicated
- Scaling in aggressively and killing servers that are mid-request; drain connections first
Recognise it
Recognition trigger: Any design with many web servers → ask where session state lives. Answer ‘in a shared store, so the web tier is stateless and auto-scalable’.
Practice question
Your login sessions are stored in each web server’s memory and users keep getting logged out after deployments. Why, and how do you fix it?
Go deeper
- Load Balancing in the Datacenter (opens in a new tab) · Google SRE book