Why does login break the moment you add a second server?
Alice logs into FotoFlow and adds two photos to a collection. Her request happened to land on Server A, which keeps a session in memory: who she is and what is in her cart. Her browser holds a cookie with the session id. Then the load balancer does its job and her next requests land on B and C, which have never heard of her. One page shows her cart, the next asks her to sign in again. State is anything a server remembers between requests: logins, carts, half-finished uploads.
Fix 1: sticky sessions
Tell the load balancer to remember where each user started, usually with a cookie, and keep sending them there. Alice always reaches A, so she stays signed in. Quick to switch on, no code changes.
But sticky users cannot be moved. If many pile onto one server, it stays overloaded while others idle. And when Server A crashes, the balancer moves Alice to B, but her session died with A's memory: signed out, cart empty, for every user pinned to A. Sticky sessions turn every server into a small single point of failure for its users.
Fix 2: move the state out
Keep sessions in a shared, fast session store, often Redis. Every server reads Alice's session from it, so any server can handle any request. The servers are now stateless: they remember nothing between requests. The cost is one extra hop of about a millisecond per request, which is cheap for servers you can add, remove, or lose freely.
This is what makes autoscaling real. A server dying is a non-event: the balancer routes around it and the next server reads the same session. Stateless servers are cattle, not pets: any one of them can be replaced without anyone noticing.
Or: let the request carry the state
Another option is a signed token such as a JWT that says "this is Alice". Any server verifies the signature without looking anything up: no store, no extra hop. The tradeoffs: a token is hard to cancel before it expires, and it travels with every request, so keep it small. A common split is tokens for identity and a shared store for anything big or that must change instantly, like carts or revoked logins.
Files are state too
Alice uploads a new avatar. Server A saves avatar.png to its own disk and displays it fine. Her next page loads from Server B, which has no such file: a broken image. The fix is shared object storage such as S3 or Google Cloud Storage: effectively unlimited, durable, and reachable from every server, with browsers fetching images straight from it through a CDN. Servers keep nothing that must outlive the request. Sessions go to a store or a token, files go to object storage, data goes to the database.
What this does not solve
Stateless servers shift every read and write onto something else, and the first something else is the database. With FotoFlow at a few thousand users, every page now hits the database for photo metadata, and it is about to become the bottleneck of the whole system. That is the next post.
Where does your current project store sessions, and what happens the day you add a second server?
If a user uploaded a file through server one and downloaded it through server two, would it work today?
Interchangeable servers are the whole game. Statelessness is what makes them interchangeable.
Next in the series: The database is hot: where should 90 percent of reads go?
Sources
Martin Fowler: Microservices
System Design Primer
undefined
Redis Docs
Recommended for you
- Craft & Skills
One server, 10 million users: how does a photo app survive that?
A 40-post series that follows FotoFlow, a photo app, from one server and 100 users to 10 million users with direct messages. One problem, one fix, one honest cost at a time.
- Craft & Skills
Ten million users want direct messages: could you design the chat system?
The final design: 20 million open sockets and a promise that every message arrives, in order, exactly once. Dumb gateways, a connection registry, store-first durability and sync by cursor.
- Craft & Skills
Five million home feeds: how do you build the page everyone opens?
The home feed must load in 200 ms from two hundred followed accounts. Fan-out on read melts at 4.6 million lookups a second; fan-out on write melts on celebrities; the hybrid survives both.
- Craft & Skills
FotoFlow wants short share links: could you design one today?
The first full design walkthrough: turn a one-line brief into numbers, the simplest system, then fix codes, caching, storage and abuse one bottleneck at a time. The method is the point.
Enjoyed this article?
Subscribe for new articles. No spam. Unsubscribe anytime.
Typed live, never pasted.