Design an online chess game with undo and leaderboard
MetaSoftware Engineer · OnsiteLast updated
This problem tests the ability to design a low-latency, stateful system for a turn-based game, focusing on synchronization, undo history management, and scalable real-time leaderboard updates.
Design an Online Chess System
Design a backend architecture for a real-time, online chess platform that supports multiplayer matches, an "undo" move feature, and a global leaderboard. The system needs to support millions of concurrent users.
Functional Requirements
- Game Play: Players can join a match and make legal moves in real-time.
- Undo/Redo: Players can request to undo their last move, which the opponent must accept or decline.
- Leaderboard: A global ranking system based on Elo ratings that updates after every completed game.
Non-Functional Requirements
- Low Latency: Move validation and propagation should feel instantaneous to both players.
- Consistency: Game states must be strictly consistent for both players in a match.
- Reliability: In the event of a server crash, the game state should be recoverable.
- Scalability: The system must handle high traffic during peak hours (e.g., tournaments).
Scope of Answer
- API Design: Define key endpoints or message types for moves and undo requests.
- Data Modeling: Propose a schema for storing game history, player rankings, and current board state.
- Architecture: Explain how you would handle real-time communication between players (e.g., WebSockets, pub/sub).
- State Management: Describe the strategy for implementing undo functionality while maintaining move history and enforcing game rules.
- Leaderboard: Explain how to update rankings efficiently without locking the entire leaderboard database.
Hints
Hint 1
Consider using an in-memory data store like Redis to hold active game states for low-latency access and fast board validation.
Hint 2
For the undo feature, represent the match as a stack of board states or a list of move deltas to allow for efficient reversal and re-validation.
Hint 3
Use a message queue or event streaming platform to decouple the completion of a game from the expensive operation of updating global leaderboard rankings.
Follow-up questions
- How would you handle a player disconnecting in the middle of a game and reconnecting later?
- If the leaderboard needs to show 'Top 100' players, how would you ensure the query performance remains constant as the number of users grows to millions?
- How would you prevent 'cheating' where a client sends a malicious move packet to the server?
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.