Part 2 of 8 · 2 min read
What state is, and who holds it
The difference between a server that remembers and one that does not.
Think of two shops. In the first, one assistant knows each regular customer and what they usually order. Customers must queue for that assistant, and if the assistant is ill, nobody is served properly. In the second, every assistant reads the customer's card at the till. Any assistant can serve any customer, so more assistants means more customers served.
The second shop is stateless.
State
State is information that a program keeps from one request to the next, and that changes what later requests do.
Example: the fact that user 7 has logged in, or that a cart holds two items, is state.
The book "System Design Interview" draws the line. A stateful server remembers client data from one request to the next. A stateless server keeps no state information between requests. The book "Understanding Distributed Systems" adds that HTTP itself is stateless: everything the server needs to process a request must be in the request, without context from earlier ones.
Stateless and stateful servers
A stateless server can answer any request using only the request and shared stores. A stateful server keeps data for a user in its own memory or disk, so later requests must come back to it.
Example: a web server that checks a login against a shared store is stateless. A server that keeps the user's login in its own memory is stateful.
State always lives somewhere. There are only three kinds of place.
| Place | Example | Good for | Weak at |
|---|---|---|---|
| The client | A cookie, a token, a browser's local storage | Small, per-user facts | Anything the user must not change |
| The server's own memory or disk | A session map in the process | Speed | Failure, scaling, and moving users between servers |
| A shared store | A database, or a fast memory store such as Redis | Anything many servers must see | Adds a network call, and the store must be kept alive |
A stateless design pushes state to the first and third rows and keeps the second row empty.
A worked example. A shop has 4 stateful servers, and each keeps the carts of its own users in memory.
- User A has her cart on server 1. A load balancer sends her next request to server 3.
- Server 3 has no cart for her. She sees an empty cart.
- To fix this, the balancer must send her to server 1 each time. That is a sticky session (section 4).
- If server 1 restarts, her cart is gone.
With a shared store, any server loads her cart by her identifier. A restart of server 1 loses nothing, and the balancer can send her anywhere.
Read it as
A stateless server is replaceable. Kill it at any time, start another one, and no user notices.
Check yourself:
- Name the three places where state can live.
- Why does a stateful server make a load balancer's job harder?