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:
- 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, orSTALE_PLY(that ply is already taken). A ply ahead of the server is a client bug, rejected; the client shouldsync. - Clock. Charge her think time (below). If that exceeds what she had left, it's a timeout, whatever the move would have done.
- Legality. The move generator checks
f1e1; if illegal,ILLEGAL_MOVE. - Apply. Update the board and derive the SAN,
Re1. - End conditions. Checkmate, stalemate, threefold repetition, fifty-move rule, insufficient material.
- Conditional write to
live_games, with the conditionply = 22 AND owner_epoch = 1. - Publish the
moveevent to both players' gateways, and once to pub/sub for spectators. - 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.
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