</> MAANG.io
system design · 301

Advanced

Master system design interviews with scalable architecture patterns, distributed systems, and real-world design challenges.

0/480 solved 0% complete

Section 7: Whose Clock Is It? - Deep Dive: The Live Game — Moves, Clocks and Lag Compensation

Executive Summary: Each live game is one single-threaded actor whose per-move pipeline ends in one conditional write, then a publish. Write before broadcast, or a crash can fork the game. Clocks are timed at the player's own gateway: arrived_at − sent_at on one machine's clock, minus a lag credit of min(sRTT, 200 ms). At ply 23, Priya's Re1 takes 4,180 ms, earns a 25 ms credit, is charged 4,155 ms, and her clock goes from 143,355 to 141,200 ms. The same gateway sends her flag, on the same ordered connection as her moves, so a move and a flag can never race.

The interviewer caps the marker. "You've got two deep dives. Where do you want to go?"

You: "The live game first, because it's the product: a lost move or an unfair clock is something every player feels. Tournaments second, in Section 8, for their load shape."

The Game Actor: One Owner, One Mailbox

Section 6 placed g_8Kd2 on an ap-south game server at epoch 1. There it lives as an actor: about 2 KB of state (board, ply counter, both clocks, draw offer, owner epoch), a mailbox, and its own timers. Moves, resignations, draw offers and timer firings all arrive as messages in that mailbox, handled one at a time. No locks, because nothing else touches its state.

The alternative, stateless servers doing a locked read-modify-write, pays a lock round trip per move and can't answer "who arms the flag timer?"

Priya's ply 23 arrives as {"t":"move","g":"g_8Kd2","ply":23,"uci":"f1e1"} and goes through the pipeline:

  1. Seat, turn and ply. Is this White's seat, is it White's turn, and is the stored ply 22? If not: NOT_YOUR_TURN, or STALE_PLY (that ply is already taken). A ply ahead of the server is a client bug, rejected; the client should sync.
  2. Clock. Charge her think time (below). If that exceeds what she had left, it's a timeout, whatever the move would have done.
  3. Legality. The move generator checks f1e1; if illegal, ILLEGAL_MOVE.
  4. Apply. Update the board and derive the SAN, Re1.
  5. End conditions. Checkmate, stalemate, threefold repetition, fifty-move rule, insufficient material.
  6. Conditional write to live_games, with the condition ply = 22 AND owner_epoch = 1.
  7. Publish the move event to both players' gateways, and once to pub/sub for spectators.
  8. Arm the backstop timer for Omar's clock.

Steps 1 to 5 take under 1 ms. The write costs p99 ≤ 10 ms; add four internal hops of ~1 ms and the relay lands at p50 < 15 ms, p99 < 50 ms.

Game actor state machine: WaitingForMove to Validating to Persisting to Broadcasting and back, with Validating rejecting bad moves, Validating or a fired flag or backstop moving to Flagged, and resignations, draws, checkmate or a decided flag ending the game


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account — no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.