Shared state every server can see
Keep fast-changing state in one shared place instead of inside each server.
Servers must be stateless
With many servers behind a load balancer, the next request may land anywhere. State kept in one server's memory (a score, a session) is invisible to the others and lost when it restarts.
Put it in a shared in-memory store
Redis holds counters, sorted sets and sessions in memory and is shared by every server. A leaderboard is a sorted set: adding a score and reading the top 10 each take well under a millisecond.
score(player, points):
redis.zincrby("leaderboard", points, player)
top10():
return redis.zrevrange("leaderboard", 0, 9)
Durability
Memory can be lost. Keep the source of truth in a database too, or turn on the store's persistence, and rebuild the in-memory view if needed.
Key takeaways
- Servers should hold no state of their own.
- Shared in-memory stores give every server the same fast view.
- Back them with durable storage.
Try it in the simulator: Serve a live leaderboard from memory
Put this concept into practice on Design Real Time Leaderboard:
- Run the starting design, with scores in PostgreSQL. Watch the database saturate.
- Keep the scores in a Redis cache instead (in-memory sorted sets), with enough memory per node, and run again.
What to watch: Every score update and every top-10 read hits PostgreSQL in the first run, and it can't keep up. In memory, both are cheap: p99 drops well under the 80 ms target and the run passes.
Check your understanding
Answer these questions interactively to test what you learned and track progress on your skill map:
- Scores are kept in each server's memory behind a load balancer. What happens?
- Which structure makes a live top-10 leaderboard cheap?