3. Client and Server
3.10 What does "stateless" mean, and why do we want it?
Remembering the user
In the restaurant: One waiter remembers you: "same as yesterday?". Lovely, but only that waiter can serve you. If they go on break, nobody knows your order. A better system: every order slip says everything: table number, your loyalty number, the full order. Then any waiter can take it.
In the system:
- A stateful server keeps information about you in its own memory between requests (for example, "this user is logged in").
- A stateless server keeps nothing about you between requests. Each request carries what's needed (a token, an ID), and anything else lives in a shared store every server can reach (a database or Redis).
Figure: with in-memory sessions, a load balancer sending Asha to server B logs her out. With a shared store, any server can answer.
Why stateless wins:
- Any server can handle any request, so a load balancer can spread traffic freely (Chapter 7).
- Adding servers is easy: new ones need no copy of anyone's memory (Chapter 6).
- A crashed server loses nothing important.
"Stateless" doesn't mean the system forgets you. It means the app servers don't hold the memory; it lives somewhere shared.
Memory code: "Every slip says it all." Keep servers forgetful; keep memory where everyone can reach it.