SuryanandHome

Real-Time Leaderboard (Gaming / Contests)

Problem statement

Maintain global ranking with frequent score updates, top N queries, and user rank around me with low latency during live events.

How it works

  • Redis Sorted Set ZINCRBY leaderboard {delta} {userId}O(log N) updates.
  • Periodic snapshot to S3/warehouse for historical analytics; sharding by contest id.

Analogy: Live sports scoreboard — digits flip fast (increment), fans ask “what place is my team” (rank query).

High-level design

Rendering diagram…

Components explained — this design

ComponentWhat it isWhy we use it here
Game serversAuthoritative score mutations in trusted server.Never trust client-reported scores in competitive modes.
Kafka score eventsDurable stream of score changes.Optional audit + offline analytics + replay for disputes.
Redis ZSET per contestSorted set for O(log N) rank updates.Industry standard for live leaderboards under high write QPS.
Leaderboard APIReads rank, top-N, neighbors around user.Composes multiple Redis calls (ZRANK, ZREVRANGE) into stable API contract.
S3 Parquet snapshotsPeriodic historical leaderboard dumps.Warehouse analytics without hammering Redis for big scans.

Shared definitions: 00-glossary-common-services.md

Low-level design

Sharding

  • Key per contest: ZSET contest:123:lb avoids single planet-scale set.
  • Global all-time may still be huge — approximate top using sketches or tiered boards.

Tie-breaking

  • Composite score in Redis as float loses precision — encode as 64-bit integer (score << 32) | (MAX_TIMESTAMP - time) for same score earlier submission ranks higher.

ZRANK cost

  • O(log N) acceptable to millions; beyond that shard + merge approximate top-K.

E2E: increment score

Rendering diagram…

Tricky parts

ProblemSolution
Hot contest keyRedis Cluster hash tag {contest9} slot
Cheat engineServer authoritative scoring only
Cross-regionActive contest pinned region or CRDT approx

Caveats

  • Redis durability — enable AOF every second; accept tiny loss vs full sync disk latency tradeoff.
  • Fair play: rollback tool for exploit-discovered scores batch adjust.

Azure

  • Azure Cache for Redis Enterprise RediSearch optional richer queries.