System Design Interview (Design a Chess Game)

System Design Interview for a Fullstack position


Author: Artyom T. Published on: September 11, 2024

Task

Interviewer: Design a chess game system using Java Spring Boot for the backend, React for the frontend, and AWS for cloud infrastructure. You have 4 weeks to deliver a proof of concept (PoC). Please include real-time gameplay, user management, and game history. You’ll need to scale for multiple players. After explaining your design, provide reasoning for your choices and include an architecture diagram.


System Design for Chess Game - PoC

Interviewer: Can you walk me through how you would approach this task?

Interviewee: Yes, I’ll break the design into several components: frontend, backend, cloud infrastructure, and database management. Here’s how I’d structure it.


1. Requirements Gathering

Functional Requirements:

  • User Management: Users can register, log in, and maintain a profile.
  • Game Creation & Play: Users can create games, join existing ones, and play in real time.
  • Game Logic: Enforce chess rules and validate moves.
  • Game History: Track and replay completed games.
  • Multiplayer Real-Time: Real-time gameplay with live updates for both players.
  • In-Game Chat: Allow players to chat during the game.

Non-Functional Requirements:

  • Scalability: Ensure the system handles numerous concurrent games and users.
  • Security: Secure data and communications with encrypted authentication.
  • Low Latency: Optimize for quick moves and updates.
  • High Availability: Ensure the service is reliable and resilient to failures.

2. Architecture Overview

Frontend:

  • React: The user interface will be implemented in React for an interactive experience. The main components will be the chessboard, user profile, and game list.
  • WebSockets: I’ll use WebSocket integration in React for real-time gameplay. This ensures moves are updated instantly for both players.

Backend:

  • Java Spring Boot: Spring Boot will manage the business logic, including user authentication, game validation, and game history.
  • REST API: We’ll expose user management and game history endpoints using REST. Real-time moves will be handled via WebSockets.
  • WebSockets: I’ll implement WebSocket services using Spring WebSockets for real-time updates like move validation and game state updates.

Cloud Infrastructure:

  • AWS: We’ll use AWS for infrastructure since it supports scalability and performance.
    • EC2: Hosts the backend (Spring Boot) and frontend (React).
    • RDS (PostgreSQL): A relational database for storing user data, game records, and move history.
    • ElastiCache (Redis): For storing real-time game states in memory, improving read/write speed for active games.
    • S3: Used for static asset storage (e.g., profile pictures, game replays).
    • API Gateway: To handle routing of API requests securely.
    • Elastic Load Balancer: For distributing traffic across backend instances.

3. Component Breakdown

Game Service (Backend - Spring Boot):

  • User Service: Handles user authentication and JWT-based authorization.
  • Game Logic Service: Validates moves, manages turns, and ensures game rules are followed.
  • Game State Management: Real-time game state updates are stored in Redis for low-latency access during active games.
  • Game History: Stores finished games in PostgreSQL for replay or analysis.

Frontend (React):

  • Game Board: Renders the chessboard and listens for WebSocket events.
  • User Interface: Manages profiles, user authentication, and game history.
  • Real-Time Communication: Uses WebSockets to send moves and receive real-time game updates.

Database:

  • PostgreSQL: Stores user profiles, game history, and moves for completed games.
  • Redis (ElastiCache): Caches real-time game states for fast access and updates.

AWS Components:

  • EC2: Hosts backend and frontend applications.
  • RDS (PostgreSQL): A relational database for persistent storage.
  • S3: For static assets and game replays.
  • CloudFront: Provides a CDN for frontend assets and delivers them globally.
  • ElastiCache (Redis): Low-latency storage for active game states.
  • Elastic Load Balancer (ELB): Load balances incoming traffic between multiple backend instances for scalability.
  • API Gateway: Securely exposes both REST APIs and WebSocket connections.

4. Why These Decisions?

Interviewer: Can you explain why you chose these technologies?

Interviewee: Sure!

  • React: For the frontend, I chose React because it allows for a responsive, component-based UI, making it easy to implement game-specific components like the chessboard and user profile.

  • Spring Boot: I went with Spring Boot for the backend due to its ease of integration with WebSocket protocols, REST APIs, and security mechanisms. It’s also well-suited for rapid development, which is crucial in a 4-week PoC.

  • AWS: I opted for AWS because it provides scalable services that are easy to set up, like EC2 for hosting, RDS for databases, and ElastiCache for caching. Its auto-scaling and ELB also provide high availability.

  • PostgreSQL: I chose PostgreSQL for its reliability and ability to handle relational data. It’s perfect for storing user profiles, game history, and detailed move records.

  • Redis (ElastiCache): Redis was chosen for caching real-time game states. It offers low-latency reads and writes, ensuring a smooth real-time gaming experience.


5. Architecture Diagram

Here is a component diagram to illustrate the system design:

                +---------------------------------+
                |            Frontend             |
                |   (React, WebSockets, Redux)    |
                +---------------------------------+
                            |
                            v
               +-----------------------------+
               |        API Gateway           |
               +-----------------------------+
                            |
                            v
     +-----------------------------------------------+
     |                    Backend                    |
     |       (Spring Boot, WebSockets, REST API)      |
     +-----------------------------------------------+
        |             |             |              |
        v             v             v              v
   +------------+ +------------+ +------------+ +----------------+
   | User Auth  | | Game Logic | | Game State | | Chat Service    |
   +------------+ +------------+ +------------+ +----------------+
        |                            |
        v                            v
   +------------+              +------------+
   | PostgreSQL |              | Redis      |
   +------------+              +------------+
        |                            |
        v                            v
+-----------------+        +--------------------+
| Persistent Data |        | Real-Time Game Data |
+-----------------+        +--------------------+

               +---------------------------+
               |        AWS Infrastructure |
               | (EC2, RDS, S3, ElastiCache)|
               +---------------------------+

6. PoC Timeline (4 Weeks)

Week 1: Requirements & Architecture

  • Finalize requirements and create a detailed system architecture.
  • Set up basic project structure (backend, frontend).

Week 2: Backend and Game Logic Development

  • Implement backend services: user authentication, game creation, and move validation.
  • Set up WebSockets for real-time communication.
  • Integrate PostgreSQL and Redis.

Week 3: Frontend Development

  • Build React components for the chessboard, user profile, and game list.
  • Implement WebSocket client for real-time updates.
  • Integrate Redux for state management.

Week 4: AWS Deployment & Final Testing

  • Deploy the system to AWS (EC2, RDS, S3, Redis).
  • Test end-to-end flow, including multiplayer games and game history.

7. Future Considerations

  • AI Opponent: Add support for playing against AI opponents.
  • Leaderboards: Implement a global leaderboard for ranking players.
  • Mobile Support: Expand the system to support mobile platforms for better accessibility.

This system ensures real-time play, low-latency performance, and scalability using the AWS cloud. The 4-week timeline focuses on delivering an MVP that covers core features like real-time gameplay, user management, and game history, all while leveraging the best-suited technologies.

Congrats, you’ve passed it! ;)