Design a game leaderboard service
MetaSoftware Engineer · OnsiteLast updated
This question tests the ability to design a high-throughput, low-latency leaderboard system, focusing on data structures for ranking, storage trade-offs, and scalability under heavy write and read loads.
Problem Statement
Design a global leaderboard service for a massive multiplayer mobile game. The game has 50 million daily active users (DAU) and millions of updates to player scores per second.
Functional Requirements
updateScore(userId, score): Update a player's score.getTopPlayers(k): Retrieve the topkplayers globally.getRank(userId): Retrieve the rank of a specific player.getNearbyPlayers(userId, k): Retrieve thekplayers ranked just above and below the given user.
Non-Functional Requirements
- Scalability: Handle high write throughput (millions of updates/sec).
- Latency:
getTopPlayersshould be extremely fast, ideally under 100ms. - Consistency: Users expect near-real-time updates, though minor eventual consistency is acceptable.
Deliverables
- Describe the high-level system architecture.
- Choose a data store and explain why it suits this use case.
- Detail the API design.
- Explain how to handle partitioning/sharding to support the scale.
Hints
Hint 1
Consider using a data structure that keeps elements sorted by score while maintaining logarithmic time complexity for insertions and lookups.
Hint 2
A standard relational database might struggle with frequent updates and range queries on a massive scale; explore in-memory stores that support specialized sorted sets.
Hint 3
Think about how you would shard the data; would you shard by userId or by score range, and what are the trade-offs for the requested operations?
Follow-up questions
- How would you handle a 'hot key' problem if the top 100 players are being queried millions of times per second?
- How would you adapt the system to handle 'seasonal' leaderboards (e.g., scores reset every week)?
- If the leaderboard needs to be accurate to the millisecond across multiple geographic regions, what consistency challenges arise and how would you mitigate them?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.