02. Scaling / Lesson 3 of 5
Stateless Services
Keep no user data on the server, so any server can answer any request.
- 8 min
- Beginner
- 9 steps
- Logged-out users0 (good)
- Servers up1 of 1 (good)
- Request
- Write
- Failed request
Step 1 of 9: The server remembers who is logged in
Read every step as text
Step 1: The server remembers who is logged in
When you log in, the server makes a session: a small record such as
ab12 → user 42. It sendsab12to your browser in a cookie, and on every later request it looks the record up.The easy place to keep it is the server's own memory. With 3,000 users logged in at about 2 KB each, that is only 6 MB, and it works well on one server.
State is anything the server must remember between two requests. A server that remembers it is called stateful.
Step 2: More servers, but each user must find the same one
One server is not enough, so we add two more behind a load balancer. But your session lives on only one of them. If the next request lands on another server, you look logged out.
The quick fix is a sticky session: the balancer always sends the same user to the same server, often by IP address. Now an office of 1,200 people behind one IP lands on server 1, on top of its 600 other users.
Sticky sessions hide the problem. The load gets uneven, and the sessions are still stuck on one machine.
Step 3: Restart server 1 and its users are logged out
We deploy a new version, so server 1 restarts. Memory is wiped, and all 1,800 sessions on it are gone. That is 60% of the 3,000 logged-in users.
The same happens when a server crashes, or when autoscaling removes it. Every user pinned to it must log in again, and anything in their cart may be lost.
You cannot deploy, scale in or lose a machine without hurting real users. That is the cost of state in memory.
Step 4: Make the servers stateless
A stateless server keeps nothing about a user between requests. Everything it needs arrives with the request, or sits in a place that all servers share.
So we move the state out of the app servers. There are two common ways: a shared store that every server asks, or a token that the user carries. The next two steps show both.
Stateless does not mean the system has no state. It means the app servers do not hold it.
Step 5: Keep sessions in a shared store
Sessions now live in a fast store such as Redis, with a time to live of 30 minutes. On every request, the server reads the session by its id: about 1 ms. The 6 MB of sessions fits in memory many times over.
Whichever server gets the request asks the same store, so the balancer can use plain round robin and the sticky rule is gone.
The store is now a shared part. If it dies, everyone is logged out, so run it with a replica and failover.
Step 6: Or let the user carry the state in a signed token
In the other approach, the login gives the browser a signed token, for example a JWT. It holds the user id and an expiry time, and the server signs it with a secret key. On each request the server checks the signature. No lookup is needed.
A token is signed, not hidden: anyone can read it, so keep secrets out. And you cannot easily cancel one before it expires, so use short lifetimes (such as 15 minutes) and a refresh token.
Store: easy to log someone out, one extra lookup per request. Token: no lookup, but hard to revoke early.
Step 7: Now any server can answer any request
Take the deploy again. Server 1 restarts and its memory is wiped, but it held no sessions. The balancer sends the user's next request to server 2, which finds the session in the store and carries on.
The user sees nothing. The restart that logged out 60% of users before now logs out nobody.
Rolling deploys, crashes and replacements all become routine, because no single server is special.
Step 8: Add and remove servers whenever you like
At peak time the platform starts servers 4 and 5 in a minute. They need no warm-up and no copied sessions. The 3,000 users spread over 5 servers, about 600 each.
At night it removes them again, and nobody is logged out. This is why stateless servers are the base of horizontal scaling.
Check for hidden state too: files saved on the local disk, in-process caches that must agree, and open WebSocket connections.
Step 9: Stateless services in one picture
A stateful server pins users to itself, so a restart logs them out and scaling is awkward. A stateless server keeps its state in a shared store or in the user's token.
Then any server can serve any request. Deploys, crashes and autoscaling cost no one a login, and 1,000 users on each of three servers can become 600 on each of five.