System design case study C19 · Redis · Ranking

Design a Real-Time Gaming Leaderboard

Fifty million players, scores changing every second, and everyone wants to know their exact rank right now: top 100 globally, among friends, and in this week's tournament.

What you'll be able to explain
  • Use sorted sets for ranking
  • Scale beyond one node for huge leaderboards
  • Compute approximate ranks for the long tail
  • Handle friends, regional and time-boxed boards
  • Validate scores and resist cheating

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

CLIENTGame serversSERVICEScore servicevalidate, dedupeCACHERedis sorted setsboard:season:42DATABASEScores DBhistory, source of truthQUEUE / LOGScore eventsSERVICELeaderboard APItop N, my rankmatch resultZADD GTZREVRANGE / ZREVRANK
Scores flow through validation into Redis sorted sets; history is kept durably.
  • 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

  1. How do you show a user's rank among 100M players?
  2. How do you handle ties?
  3. Daily and weekly leaderboards?
  4. How do you stop cheaters?

Model answers to each of these are in the book.