Case Study 25 β€” Gaming Leaderboard

Design a real-time gaming leaderboard: players earn scores; ranks update quickly and can be queried globally, per game, or among friends.

1. Problem

After each game session, a player's score should reflect on leaderboards within seconds. Users expect to see their rank, top N players, and neighbors around their position without slow full-table scans.

2. Requirements

Functional (MVP)

  • Submit score after game ends
  • Global leaderboard per game (top N)
  • Get rank for a specific player
  • Get players around a user (β€œrank window”: Β±5 neighbors)
  • Optional: weekly/monthly reset boards
  • Optional: friends-only leaderboard

Out of scope (initially)

  • Anti-cheat ML, replay verification
  • Prize fulfillment, tournaments with brackets
  • Cross-game unified ranking
  • Historical rank graphs

Non-functional

  • Rank reads low latency (< 50 ms P95)
  • Score updates high throughput (many concurrent games)
  • Approximate tie-breaking rules documented (same score β†’ earlier timestamp wins)
  • Available during game peaks; degrade gracefully (slightly stale OK for non-critical views)

3. Back-of-the-envelope estimates

These numbers are rough order-of-magnitude math β€” not a capacity plan. In interviews, the goal is to show you know what to count and which resource breaks first. A useful shortcut: 1 QPS sustained β‰ˆ 86,400 requests/day. Gaming peaks (evenings, new season launch) often hit 2–5Γ— average β€” reads spike harder than writes.

Why we estimate

Leaderboards are write-on-score, read-constantly. Players check rank after every game and browse top-10 obsessively. Estimates tell us:

  • Whether Redis sorted sets can hold hot boards in memory
  • If read QPS (not writes) drives infrastructure
  • When friend-scoped boards need a different query path than global

Assumptions

AssumptionValueWhy it matters
Daily active users (DAU)5MPlayer base
Games finished per user per day20Each game β†’ one score write
Leaderboard partitions (games/modes)10Separate ZSET per board
Read:write ratio (rank lookups vs score posts)50:1Defines cache strategy
Active players per board~1MZSET memory
Score + member size in Redis~40 BMemory per ranked entry

Step A β€” Traffic (QPS) with labeled arithmetic

Score submissions (writes):

Scores per day      = 5M users Γ— 20 games/user
                    = 100M score updates/day

Write QPS (avg)     = 100M Γ· 86,400
                    β‰ˆ 1,160 writes/second

Peak write QPS (5Γ— evening) β‰ˆ 1,160 Γ— 5
                            β‰ˆ 5,800 writes/second

Each write is a ZADD to one of 10 board keys β€” O(log N) per update.

Rank reads (lookups):

Read:write ratio      = 50:1

Read QPS (avg)        = 1,160 Γ— 50
                      β‰ˆ 58,000 reads/second

Peak read QPS (5Γ—)    β‰ˆ 58,000 Γ— 5
                      β‰ˆ 290,000 reads/second

Reads include: top-10, my rank, neighbors Β±5, friends board.

Top-N queries (subset of reads):

Assume 30% of reads are top-100 fetches
Top-N QPS (peak)    β‰ˆ 290,000 Γ— 30% β‰ˆ 87,000/s
β†’ Cache top-100 per board in Redis string or local cache (refresh every 1–2 s)

Step B β€” Storage

Redis sorted sets (hot in-memory):

Players per board   = 1M active scorers
Bytes per entry     β‰ˆ 40 B (member ID + score + skip list overhead)

Memory per board    = 1M Γ— 40 B β‰ˆ 40 MB
10 boards           β‰ˆ 400 MB total β€” fits one Redis cluster easily

At 10M players/board β†’ 400 MB each β†’ 4 GB for 10 boards β€” still Redis-friendly

Historical scores (optional archive):

If storing every game result in Postgres:
  100M rows/day Γ— 50 B β‰ˆ 5 GB/day β†’ partition or skip for MVP
MVP: only keep **best score per user per board** in Redis

Weekly reset boards:

Duplicate ZSET keys per week: `board:game1:2026-W32`
Old keys TTL after 7 days β€” bounded memory

Step C β€” Bandwidth and other resources

API response sizes:

Top-10 response     β‰ˆ 10 players Γ— 100 B β‰ˆ 1 KB
Peak top-N QPS      β‰ˆ 87,000/s (cached path)

Uncached egress     = 87,000 Γ— 1 KB β‰ˆ 87 MB/s β€” fine if cached; raw ZREVRANGE on every read is CPU-heavy not bandwidth-heavy

Rank + neighbors query:

ZRANK + ZREVRANGE window β‰ˆ 11 members Γ— 100 B β‰ˆ 1.1 KB
Peak rank reads       β‰ˆ 200,000/s Γ— 1 KB β‰ˆ 200 MB/s β€” Redis handles in-memory

Redis CPU at peak:

290,000 reads/s + 5,800 writes/s β‰ˆ 300K ops/s
Modern Redis cluster: 100K–500K ops/s per shard β†’ 1–3 shards sufficient

Step D β€” Read:write ratio table

OperationTypeAvg QPSPeak QPSNotes
Submit score (ZADD)Write~1,160~5,800One per game end
Get top NRead~17,400~87,000Cache aggressively
Get my rank (ZRANK)Read~29,000~145,000O(log N)
Get neighbors Β±5Read~11,600~58,000ZREVRANGE by rank
Friends boardRead~500~2,500Filter friend IDs after fetch

Ratio: ~50:1 reads to writes β€” optimize read path with cached top-N and pipelining.

What the numbers tell us

  • Redis ZSET is the right tool β€” 400 MB for 10 boards, O(log N) updates at 5,800/s peak
  • Reads (~290K/s peak) dominate β€” cache top-100 per board; refresh async every 1–2 s
  • Writes (~5,800/s peak) are easy for Redis β€” don’t shard until boards exceed single-node ops
  • Tie-break: same score β†’ earlier timestamp wins β€” encode as score.timestamp composite or secondary sort
  • Friends leaderboard β€” don’t build separate global sort; fetch friend IDs then ZSCORE batch or maintain friend-only ZSETs for whales
  • Weekly resets β€” new key per period with TTL; avoid deleting 1M members synchronously

Common mistake for this problem

Using Postgres ORDER BY score for live ranks at 5,800 writes/s and 290K reads/s β€” full table scans kill you. Another mistake: exact real-time top-10 on every read β€” precompute and cache; 1–2 s staleness is fine. Finally, ignoring tie-breaking rules β€” ambiguous ranks frustrate players and interviewers notice.

4. High-Level Design (HLD)

Components

ComponentRole
Leaderboard APISubmit scores, query ranks and top-N
Redis ClusterLive ZSET per board; primary rank store
Score Event QueueBuffer spikes; optional async path
Score WorkersConsume events, update Redis, archive to DB
PostgresHistorical scores, audits, cold storage
Friends ServiceResolve friend IDs for scoped boards
Local cacheCache top-100 snapshots per board

Flows

Submit score

  1. Game server POSTs { gameId, playerId, score } with service token
  2. API validates score bounds (sanity check)
  3. Compute composite score for tie-break (see LLD)
  4. ZADD Redis key lb:{gameId}
  5. Optionally enqueue event for archive/analytics
  6. Return { newRank, bestScore }

Get top 100

  1. Client GET /leaderboards/:gameId/top?limit=100
  2. Check local/Redis cache of snapshot (TTL 1–5 s)
  3. On miss: ZREVRANGE lb:{gameId} 0 99 WITHSCORES
  4. Hydrate player display names from profile cache
  5. Return ranked list

Get my rank + neighbors

  1. ZREVRANK lb:{gameId} playerId β†’ rank (0-based)
  2. ZREVRANGE lb:{gameId} rank-5 rank+5 WITHSCORES
  3. Return window centered on player

Weekly reset

  1. Cron copies current ZSET to archive key lb:{gameId}:2026-W29
  2. Delete or rotate live key
  3. Publish β€œseason ended” event

Trade-offs

  • Redis ZSET vs DB ORDER BY β€” ZSET scales to millions with fast rank; SQL scans are too slow at peak
  • Sync ZADD vs async queue β€” sync gives instant rank for MVP; queue smooths spikes but adds lag
  • Composite score encoding vs separate tie-break map β€” composite in one ZSET score is simpler
  • Exact friend board vs precomputed β€” compute on read with friend ID filter for MVP; precompute if hot

5. Low-Level Design (LLD)

APIs

POST /api/v1/leaderboards/:gameId/scores
Headers: Authorization: Bearer <service-or-user-jwt>
Body: { "playerId": "p_123", "score": 9840, "matchId": "m_456" }
β†’ 200 {
    "playerId": "p_123",
    "score": 9840,
    "rank": 42,
    "previousBest": 9100
  }

GET /api/v1/leaderboards/:gameId/top?limit=100
β†’ {
    "entries": [
      { "rank": 1, "playerId": "p_1", "displayName": "Neo", "score": 12000 },
      ...
    ]
  }

GET /api/v1/leaderboards/:gameId/players/:playerId/rank?window=5
β†’ {
    "rank": 42,
    "score": 9840,
    "window": [
      { "rank": 37, "playerId": "...", "score": 9900 },
      ...
    ]
  }

GET /api/v1/leaderboards/:gameId/friends/top?limit=50
β†’ { "entries": [...] }   -- only friend IDs

Schema

Redis (primary):

Key: lb:{gameId}                    -- ZSET member=playerId, score=composite
Key: lb:{gameId}:meta               -- HASH lastReset, seasonId
Key: profile:{playerId}             -- cached displayName (optional)

Postgres (archive / audit):

score_events (
  id            BIGSERIAL PRIMARY KEY,
  game_id       VARCHAR(64) NOT NULL,
  player_id     VARCHAR(64) NOT NULL,
  raw_score     INT NOT NULL,
  match_id      VARCHAR(64) NOT NULL,
  created_at    TIMESTAMPTZ NOT NULL
)

leaderboard_snapshots (
  id            BIGSERIAL PRIMARY KEY,
  game_id       VARCHAR(64) NOT NULL,
  season_id     VARCHAR(32) NOT NULL,
  snapshot_ref  VARCHAR(512) NOT NULL,  -- S3 or JSON blob
  created_at    TIMESTAMPTZ NOT NULL
)

player_best (
  game_id       VARCHAR(64),
  player_id     VARCHAR(64),
  best_score    INT NOT NULL,
  updated_at    TIMESTAMPTZ NOT NULL,
  PRIMARY KEY (game_id, player_id)
)

Modules

LeaderboardController
ScoreService
RankQueryService
CompositeScoreEncoder
RedisLeaderboardStore
ScoreEventProducer
SnapshotJob
FriendsClient
TopNCache

Algorithm β€” composite score (tie-break)

Store integer in Redis ZSET such that higher raw score wins; ties broken by earlier achievement ranks higher (lower timestamp β†’ higher composite).

MAX_RAW = 1_000_000_000
MAX_TIME = 4_102_444_800  -- unix seconds far future anchor

function encodeComposite(rawScore, achievedAtUnix):
  -- invert time so older timestamps produce larger composite tail
  timeComponent = MAX_TIME - achievedAtUnix
  return rawScore * MAX_TIME + timeComponent

function decodeRaw(composite):
  return floor(composite / MAX_TIME)

Only increase a player's best score (typical game rule):

function submitScore(gameId, playerId, rawScore, achievedAt):
  key = "lb:" + gameId
  composite = encodeComposite(rawScore, achievedAt)

  currentComposite = redis.zscore(key, playerId)
  if currentComposite is not null:
    if rawScore <= decodeRaw(currentComposite):
      rank = redis.zrevrank(key, playerId)
      return { rank: rank + 1, bestScore: decodeRaw(currentComposite), improved: false }

  redis.zadd(key, composite, playerId)
  rank = redis.zrevrank(key, playerId)

  scoreEvents.archive({ gameId, playerId, rawScore, achievedAt })
  topNCache.invalidate(gameId)

  return { rank: rank + 1, bestScore: rawScore, improved: true }

Algorithm β€” top N with cache

function getTop(gameId, limit):
  cacheKey = "top:" + gameId + ":" + limit
  cached = topNCache.get(cacheKey)
  if cached: return cached

  rows = redis.zrevrange("lb:" + gameId, 0, limit - 1, withScores=true)
  entries = []
  for i, (playerId, composite) in enumerate(rows):
    entries.append({
      rank: i + 1,
      playerId,
      score: decodeRaw(composite),
      displayName: profileCache.getName(playerId)
    })

  topNCache.set(cacheKey, entries, ttl=2 seconds)
  return entries

Algorithm β€” rank window

function getRankWindow(gameId, playerId, windowSize):
  key = "lb:" + gameId
  rank0 = redis.zrevrank(key, playerId)
  if rank0 is null: return 404

  start = max(0, rank0 - windowSize)
  end = rank0 + windowSize
  rows = redis.zrevrange(key, start, end, withScores=true)

  return {
    rank: rank0 + 1,
    score: decodeRaw(redis.zscore(key, playerId)),
    window: buildEntries(rows, startRank=start + 1)
  }

Algorithm β€” friends leaderboard (MVP)

function getFriendsTop(gameId, userId, limit):
  friendIds = friendsClient.listFriendIds(userId)
  friendIds.append(userId)

  pipe = redis.pipeline()
  for fid in friendIds:
    pipe.zscore("lb:" + gameId, fid)
  scores = pipe.execute()

  entries = sortByScoreDesc(friendIds, scores).take(limit)
  return hydrateNames(entries)

For large friend lists, use ZMSCORE (Redis 6.2+) in one round trip.

Concurrency & correctness

  • ZADD is atomic per member β€” concurrent submits last-write-wins on composite; use β€œonly if better” logic above
  • Rank is dynamic β€” always computed from ZSET, never stored separately
  • Weekly reset must be atomic: RENAME live key β†’ archive, then create fresh key
  • Service-to-service auth on score POST prevents client tampering

6. Scale evolution

StageChange
MVPSingle Redis primary; sync ZADD; top-100 cache
Peak writesAsync queue + worker batch ZADD; coalesce multiple updates/player
Huge boardsRedis Cluster hash-tag {gameId}; shard by game
GlobalRegional boards + optional global merge job (not real-time)
Anti-cheatValidate scores in game server; anomaly detection async

7. Recap

  • Redis ZSET is the core β€” O(log N) updates and rank queries
  • Encode tie-break into composite score to keep one sorted structure
  • Cache top-N snapshots; personalize rank windows with ZREVRANK + range
  • Archive to Postgres for history; Redis stays lean and fast

Practice: redraw HLD from memory, then write submitScore + composite encoding without looking.