Design a Leaderboard: The Junior, Senior and Staff Answer
A leaderboard question probes three things: how you think about scale, how you model mutable ranking data, and how you balance latency against consistency. The interviewer wants to see whether you can turn a vague product brief into a concrete request path, choose the right AWS building blocks, and anticipate failure modes.
What the interviewer is testing
- Scalability – can you handle millions of players and thousands of score updates per second?
- Data modeling – how do you store a mutable, ordered list efficiently?
- Latency vs consistency – when is it okay to show a slightly stale rank?
- Operational thinking – monitoring, cost, and failure recovery.
Requirements and driving numbers
| Requirement | Typical value (ballpark) |
|---|---|
| Users (players) | ~10 million total, active 10 % daily |
| Write rate (score submissions) | ~100 k updates per minute ≈ 1.7 k per second |
| Read rate (top-N fetches) | ~5 k reads per second for top 100, plus occasional per-user rank lookups |
| Latency target | < 200 ms for reads, < 500 ms for writes |
| Consistency | Eventual for global view, strong for per-user recent score |
These numbers drive the choice of storage and caching layers. The write rate of ~1.7 k /s is modest for DynamoDB, but the read pattern—many hot reads of the same top-N slice—calls for an in-memory cache.
Core design in prose
Request path, one hop at a time
- Client submits a new score – HTTP POST to an API Gateway endpoint.
- API Lambda validates the payload and forwards the record to an SQS queue (decouples spikes).
- Worker Lambda consumes the message and writes the new score to a DynamoDB table keyed by
(game_id, user_id). The table has a GSI onscoreto enable range queries. - After the write, the worker publishes an update event to an MSK Kafka topic.
- A separate consumer reads the event and updates a sorted set in ElastiCache (Redis). The sorted set stores
score:user_idfor fast top-N retrieval. - Read path – a GET request hits API Gateway → Lambda → checks ElastiCache for the requested slice. If missing, Lambda falls back to DynamoDB, writes the result to the cache, and returns the data.
- Audit – every write is also appended to an S3 bucket (partitioned by date) for compliance and replay.
Data model
- DynamoDB table
LeaderboardScores - Partition key:
game_id - Sort key:
user_id - Attributes:
score,last_updated - GSI:
score-indexwithgame_idas partition key,scoreas sort key, projected attributesscore, user_id. - Redis sorted set
game:{game_id}:rank - Member:
user_id - Score: numeric
score - Supports
ZRANGEfor top-N andZRANKfor per-user rank.
Key choice and alternatives
The design picks DynamoDB for its ability to handle high write throughput with low operational overhead. An alternative is Aurora MySQL with a composite primary key and an index on score. Aurora offers strong consistency but requires more tuning for scaling writes, and the read-heavy top-N queries would still need a cache layer. DynamoDB’s on-demand capacity lets us hedge against traffic spikes without pre-provisioning.
What a complete junior answer covers
- Lists the basic components (API Gateway, Lambda, DynamoDB, Redis).
- Draws a simple request flow: client → API → Lambda → DynamoDB → cache.
- Mentions latency target and basic read/write rates.
- Touches on data model (primary key, score attribute) but may not discuss GSIs.
- Accepts eventual consistency for the leaderboard.
What a senior answer adds
- Sharding – partitions the DynamoDB table by
game_idand possibly by a hash prefix to spread hot keys. - Cache warming – a background job keeps the top-N slice hot in Redis.
- Write-through vs write-behind – explains why the SQS decoupling is useful for bursty traffic.
- Consistency trade-off – uses DynamoDB’s
ConditionExpressionto prevent score regression, and explains when stale ranks are acceptable. - Metrics – CloudWatch dashboards for write latency, cache hit ratio, and queue depth.
What a staff answer adds
- Failure modes – describes how to handle cache miss storms (thundering herd) with exponential backoff and a fallback to DynamoDB, and how to recover from a Redis node loss using cluster replication.
- Operational concerns – discusses TTL on cache entries, DynamoDB autoscaling policies, and S3 lifecycle rules for audit logs.
- Cost analysis – rough estimate: DynamoDB on-demand writes for 100 k/min are about 144 million a day, on the order of $100 / day at published per-million write prices; Redis (cache-hit > 90 %) costs a few hundred dollars per month; SQS and MSK add modest overhead.
- Trade-off defense – argues why eventual consistency is acceptable for a global leaderboard but strong consistency is kept for a user’s personal best view.
- Testing & deployment – blue-green deployment of Lambda versions, canary rollout of schema changes, and chaos testing of the cache layer.
Mistakes that sink this question
| Mistake | Why it hurts |
|---|---|
| Ignoring read-heavy pattern | Leads to DynamoDB scans, which explode cost and latency. |
| Using a relational DB without cache | Cannot serve top-N queries at < 200 ms under load. |
| Over-engineering consistency | Adds latency and complexity for little gain. |
| Forgetting to handle hot keys | A single popular game can throttle the partition key. |
| No monitoring plan | Interviewer will ask how you know the system is healthy. |
How to practice this
- Study the worked leaderboard design in the System Design AI library. It shows the canvas with each component annotated.
- Write your own answer on a blank canvas: draw the request path, list the AWS services, and note the numbers you chose.
- Use the practice brief rubric to self-review: check that you covered requirements, data model, scaling, and failure modes.
Questions people ask
What is the simplest way to store a mutable ranking list?
Use a DynamoDB table for durable writes and a Redis sorted set for fast top-N reads. The sorted set gives O(log N) inserts and O(log N + M) range queries, where M is the number of members returned.
How do you prevent a user from submitting an older score?
In the write Lambda, include a conditional update that only overwrites if the incoming score is greater than the stored score attribute.
When should you choose Aurora over DynamoDB for a leaderboard?
Choose Aurora if you need strong transactional guarantees across multiple related tables, or if you already have a relational data model and can afford the operational overhead.
What operational metric matters most for the cache layer?
Cache hit ratio. A high hit ratio (> 90 %) means most reads stay in memory, keeping latency low and DynamoDB read cost minimal.