Forget the user between requests
Each server keeps its users' logins in its own memory. When one crashes, everyone on it is logged out, and a new server can't help anyone who logged in elsewhere. 5,000 req/s arrive.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
A stateless server keeps nothing about a user between requests. Any server can answer any request, so you can add, remove or lose servers freely.
The state still has to live somewhere. Put sessions in a shared cache such as Redis, or in a signed token the browser sends with every request.
Sticky sessions send each user back to the same server. They hide the problem until that server dies, and they spread load unevenly.
What you have to achieve
5,000 requests a second at the busiest hour, 10% of them writes.
- No broken or illegal connections
- Use a Cache (Redis)
- Use an App Server with at least 4 instances
- No component is overloaded
- After any one machine crashes and failover ends, under 1% of requests fail
What you learn
Your servers now forget everyone between requests, so losing one costs nothing but a little capacity. That's what makes adding and removing servers safe.
Check yourself
What makes a server stateless?
- It has no database
- It keeps nothing about a user between requests
- It never crashes
Show the answer
It keeps nothing about a user between requests. Stateless means any server can answer any request, because nothing it needs is stuck in one machine's memory.
Where should login sessions live so that any server can read them?
- In each server's memory
- In a shared cache, or a signed token the browser sends
- In a file on one server
Show the answer
In a shared cache, or a signed token the browser sends. A shared store or a signed token lets every server check the session, so losing a server logs nobody out.
What's the catch with sticky sessions?
- When that server dies, its users lose their session
- They make every request slower
- They need a second database
Show the answer
When that server dies, its users lose their session. Sticky sessions keep state on one server. They work until it dies, and they also leave some servers busier than others.