Game leaderboard: 2 million players
A mobile game has 2 million players online. They send 20,000 req/s, and half are score updates. A famous streamer just started a tournament, and a third of all updates now hit that one tournament's leaderboard.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Every player keeps a live connection, so the leaderboard can move on their screen. That's a Realtime Gateway.
Scores are written all the time and read even more. A cache keeps the top of each board in memory, and a NoSQL store keeps every player's score, split by key.
Here's the trap: NoSQL splits data by key, and one partition only takes so much. If a third of all writes share one key, one partition takes them all. That's a hot key.
The fix is to split the hot key: write each score to one of many sub-keys, like tournament#1 to tournament#20, and add them up when you read. Writes spread out, and reads pay a little extra.
What you have to achieve
20,000 requests a second at the busiest hour, 50% of them writes.
- No broken or illegal connections
- Hold 2M open connections
- Use a NoSQL DB
- No component is overloaded
- Workers clear the queue within the day
- Fewer than 0.1% of requests fail
- p99 response time under 200 ms
What you learn
The same database that choked on one tournament handles it easily once the key is split. Hot keys catch big companies too: a celebrity's post or a viral product. Always ask what happens when one key gets famous.
Check yourself
A third of all score updates hit one tournament. What's that called?
- A hot key
- A cache miss
- A cold start
Show the answer
A hot key. NoSQL splits data by key, so one busy key lands on one partition, however big the table is.
How do you spread a hot key?
- Buy a bigger database
- Write to many sub-keys and add them up on read
- Cache the writes
Show the answer
Write to many sub-keys and add them up on read. tournament#1 to tournament#20 spreads writes over many partitions. Reads pay a little to add them up.