Scope
Functional
- Submit a score after a match; show top N globally; show my rank and nearby players.
- Friends leaderboard; daily/weekly/season boards; regional boards.
Non-functional
- Rank updates visible within ~1 s. Top-N and my-rank queries < 50 ms.
- 50M players in a season board; 20k score updates/s at peak.
Size
- Sorted-set entry ≈ 50–100 bytes → 50M players ≈ 5 GB: fits in one Redis node's memory, but throughput and availability argue for more.
- Reads: top-100 is extremely hot; cache it for 1 s.
The basic design
ZADD board:season:42 GT <score> <player>keeps the best score (GT = only if greater).- Top N:
ZREVRANGE board 0 99 WITHSCORES. My rank:ZREVRANK board player(O(log n)). Neighbours: range around my rank. - Ties: encode a tiebreaker in the score (e.g. score × 10¹⁰ + (MAX_TS − achieved_at)) so earlier achievers rank higher.
The 30-second summary
Say it in the interview
"Scores arrive from trusted game servers, are validated and deduplicated by match ID, stored durably, and applied to Redis sorted sets with ZADD GT. Top-N and my-rank are O(log n) queries, with the top 100 cached for a second. For scale I'd shard by player hash with scatter-gather for top N, and give exact ranks only to the head while the long tail gets percentile ranks from a histogram. Friends boards are computed on the fly."
Follow-up questions the interviewer may ask
- How do you show a user's rank among 100M players?
- How do you handle ties?
- Daily and weekly leaderboards?
- How do you stop cheaters?
Model answers to each of these are in the book.