Full answer
Requirements
Functional:
- Update a player's score when they finish a game or solve a problem.
- Show the global top 100, a player's own rank and the players just above and below.
- Separate boards per contest, per day or per week.
Non-functional:
- Rank updates visible within a second or two.
- Reads are much more frequent than writes.
- Scores must not be lost.
Capacity estimates
Assumptions: 10M players on one board, 50M score updates per day, peak factor 5, about 100 bytes per sorted-set member including overhead.
- Updates: 50M / 86,400 = about 580 per second; about 2,900 per second at peak.
- Memory: 10M x 100 bytes = about 1 GB, which fits in one Redis instance with a replica.
- Each update or rank query is O(log n): about 23 steps for 10M members.
API
POST /v1/boards/{board_id}/scores {"player_id": "p_42", "delta": 30} (or "score" for absolute)
GET /v1/boards/{board_id}/top?limit=100
GET /v1/boards/{board_id}/players/{player_id} -> {"rank": 18234, "score": 1290, "neighbours": [...]}
Data model
- Redis sorted set
board:{board_id}: member = player_id, score = points. - Database table
score_events: event_id, board_id, player_id, delta, created_at (source of truth, append only). player_scores: board_id, player_id, total (for rebuilds and history).
High-level design
mermaid
flowchart LR
G[Game or judge service] --> API[Leaderboard API]
API --> DB[(Score events DB)]
API --> Z[(Redis sorted sets)]
C[Clients] --> API
RB[Rebuild job] --> DB
RB --> ZHow the operations work
- Update:
ZINCRBY board:contest-7 30 p_42after the event is stored. - Top 100:
ZREVRANGE board:contest-7 0 99 WITHSCORES. - Own rank:
ZREVRANK board:contest-7 p_42(0-based), then neighbours withZREVRANGEaround that rank. - Ties: sorted sets order equal scores by member name. To rank earlier achievers higher, encode the score as
points x 10^10 + (MAX_TS - finish_time), so the same points with an earlier time sort first.
Bottlenecks and trade-offs
- Single instance limits. One Redis node handles tens of thousands of operations per second, enough here. For boards of hundreds of millions, shard by score range (each shard knows how many players are above it) or compute exact ranks for the top and percentile ranks ("top 5%") for everyone else.
- Durability. Redis is a cache of the database; if it is lost, rebuild from
player_scores. - Time-based boards (daily, weekly) are separate keys with an expiry, so resets are just new keys.
- Hot reads of the top 100 can be cached for one second at the API layer, since everyone requests the same page.
- Cheating is outside the leaderboard: accept scores only from trusted game or judge services, never from clients.
By level
Fresher: Explain why a sorted set fits and show the top 100 and own-rank queries. A fresher should say why a SQL query that counts higher scores gets slow.
Mid: Do the memory math, persist events, rebuild the cache and handle ties. Add time-based boards with expiry.
Senior: Discuss sharding by score range, approximate ranks, friend boards, caching hot reads and anti-cheat boundaries. Expect a clear plan for growth beyond one node.
Key points
- Redis sorted set gives O(log n) updates and rank queries
- Top 100 is one range read
- Database events are the source of truth; rebuild Redis from them
- Encode tie-breaks into the score
- Shard by score range or approximate ranks for huge boards
Common mistakes
- Computing rank with ORDER BY and COUNT on every request
- Trusting score updates sent by clients
- Treating Redis as the only copy of scores
- Ignoring ties
Follow-ups
- How would you show a friends-only leaderboard?
- How would you support 500 million players?
- How do you reset weekly boards?
Tips
- Name the sorted-set commands
- Show the tie-break encoding
- Say how you rebuild after a cache loss
Evaluation rubric
- Correct use of a sorted structure for ranks
- Capacity math for memory and updates
- Durability and rebuild plan
- Handles ties and scaling limits