KatInterview

Building a Resilient Multiplayer Chess Backend

System DesignModel answer
MediumModel answerSystem Designsystem-designwebsocketsfault-tolerance

Last updated

Architect a real-time chess platform where each game has one authoritative owner, exactly-once move application, server-driven clocks, fast crash recovery, and low-latency spectator streaming.

Scenario

You are building the backend for an online chess platform. At peak it must host a large number of simultaneous matches (start from thousands, then reason about what changes at roughly 100k). Players connect over persistent sockets, and a match must stay correct even when machines die, networks split, or players drop off mid-game.

Behavior the system must provide

  • Rule enforcement: each submitted move is checked against the current position and against whose turn it is.
  • Authoritative time: the server owns the clocks. Clients only display what they are told. If a player's connection drops, their clock keeps running.
  • Durable history: every game is recorded as an append-only sequence of events that can be audited later.
  • Rejoining: a player who loses connection can come back and resume the same game without confusion.

Quality attributes

  • Crash recovery: if the process running a game dies, the game must be back in service within a few seconds, with no state lost.
  • Safe retries: if a client resends a move after a timeout, it must never be applied twice.
  • Agreement: both players must observe the identical sequence of moves regardless of differing network latencies.

What to deliver

  1. An overall architecture covering real-time connection handling and where durable game state lives.
  2. The structure of the move log, showing how it guarantees ordering and duplicate protection.
  3. A walkthrough of what happens when the server that owns a game crashes, including how you prevent two servers from acting on the same game.
  4. A strategy for letting a large audience watch games live with low delay.

State your assumptions about scale and latency up front, and call out the trade-offs behind your choices.

Hints

Hint 1

Give each game exactly one active owner at a time, and have that owner process events for the game one by one. Think about how a lease with a monotonically increasing number (an epoch) lets the storage layer reject a stale owner.

Hint 2

Let the database enforce correctness: a composite key on (game, move number) makes it impossible for two writers to both commit the same move, and a uniqueness rule on a client-supplied request id turns retries into lookups.

Hint 3

Do not store a ticking counter for the clock. Store the remaining time at the start of the current turn plus the timestamp, and derive the live value; this makes recovery after a crash and behavior during disconnects trivial.

Hint 4

Separate players from spectators: spectators should read from a broadcast channel fed by the committed log, not from the owner process, so audience size never slows down the game itself.