How to design online chess
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
The problem
An online chess service pairs two players of similar strength, runs their three-minute game move by move with the server as referee and timekeeper, and folds the result into a rating and a global leaderboard. The design turns on where a live game lives: it has to answer moves fast enough that the clock feels fair to both players, yet survive the loss of the machine running it. Our answer keeps each game hot in the memory of one game server and writes each accepted move to a durable record before anyone sees it, so speed and recovery come from different places.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Which time controls do we support, and is there an increment? | The clock decides how much latency the design can tolerate. A three-minute game with no increment means a few hundred milliseconds of delay per move is a real cost to a player, which is why clock fairness gets its own discussion instead of a footnote. | Three minutes for each player, no increment. The clock runs on a player's turn and stops on the opponent's. |
How many players, and how many games run at the same time? | The number of concurrent games sets the number of open sockets and the size of the game server fleet, and the pace at which games start sets the load on matchmaking. | About 10 million registered players, 500,000 games at once at peak, so about 1 million open connections, two per game. |
Do people watch games, and how large can one crowd get? | Two players per game is a bounded fan-out. A popular game with a crowd is not, and it changes which servers may hold sockets to the game. | Yes. A typical game may have no watchers at all, but a top game can draw a crowd far larger than its two players. Design the players' path first and treat watchers as a separate tier. |
How fresh does the leaderboard and a player's own rank have to be? | A rank that must be exact the instant a game ends needs a different store from one that may lag by a few seconds. | Updated shortly after each game ends. A few seconds of lag is fine; a page that recounts 10 million rows on each request is not. |
Can a player be in more than one live game at a time? | If not, matchmaking has to check and pair in one step, and the design needs a place that says which game a player is in. | No. A player is in at most one live game, and clicking play twice should land them in that one game. |
Is cheating detection part of this round? | Engine detection is a pipeline of its own. Knowing it is out lets you spend the time on the live path. | Out of scope, along with chat, puzzles and tournaments. Keep a record of each move and the time spent on it, since a later detector would want both. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
10 million registered players | Ratings and ranks for 10 million players have to be served without scanning. A rank query is a lookup in an ordered structure, and a players table of 10 million rows is one we'd keep in a single relational database with a replica. |
500,000 concurrent games at peak, so 1 million open connections | A fleet of stateful game servers, each holding a slice of the games and the sockets of their players, with a lookup that tells a client which server owns its game. Each game stays in memory on its server so a move is answered without a database round trip. |
Clocks stay fair when the two players have different network delays | Time is measured on the game server, not on either phone. For each move the server charges the interval from dispatching the opponent's move until this player's answer lands, then trims a bounded amount for latency it has measured itself. Timestamps supplied by a client carry no weight, and the server's view of both clocks rides along in each message so the two screens agree. |
A player is never in two games at once | Matchmaking has to find a partner and pair in a single atomic step keyed by player id, rather than checking first and pairing second. Each player has one active-game entry, and a second click, even from another tab, is routed to the game that already exists. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Moves per second at peak | Assume a three-minute game lasts about 80 half-moves (40 by each side) within at most 360 seconds of combined clock. 500,000 games × 80 half-moves ÷ 360 seconds ≈ 111,000 moves a second; call it about 110,000. Each move is one message in and two out (both players), so about 330,000 socket messages a second. | About 110,000 moves a second, about 330,000 messages a second over the sockets | The write rate the live path must take, and why a database write per move is the wrong default: this is tens of thousands of row rewrites a second for state that changes again within seconds. |
Game servers for 1 million sockets | Phoenix reported about 2 million idle WebSocket connections on one 128 GB, 40-core box, and about 450,000 on a 16 GB box before memory ran out. Our servers also hold game state and run clocks, so take a conservative target of 50,000 sockets a server: 1,000,000 ÷ 50,000 = 20 servers, each with 25,000 games and about 5,500 moves a second (110,000 ÷ 20). Game state at about 2 KB per game (board, move list, clocks) is 25,000 × 2 KB ≈ 50 MB a server. | About 20 servers as a floor; memory for game state is tiny, so sockets and per-move CPU set the count, and the crash blast radius argues for more, smaller servers (100 servers of 10,000 sockets hold 5,000 games each) | The fleet size and what limits each server, which is what the interviewer asks when they ask you to size this. |
Game starts and pairings a second | 500,000 games ÷ at most 360 seconds of combined clock ≈ 1,400 games starting a second at a steady peak. Each pairing consumes two tickets, so about 2,800 ticket inserts and 2,800 ticket removals a second. | About 1,400 pairings a second, about 5,600 ticket writes a second | The load on the matchmaking pool, and why a single relational table with a lock per pairing is where the design strains first. |
Delay allowance per move, using Chess.com's blitz figures | The published blitz allowance is 300 ms a move plus a 400 ms bank refilled after each pair of moves. On a 180-second clock, 300 ms is about 0.17% of a player's time. Over 40 moves by one player the automatic part adds up to at most 40 × 300 ms = 12 s, and the bank to at most 20 × 400 ms = 8 s, so at most about 20 s, about 11% of the clock, if a player lagged on each move. | A cap in the low hundreds of milliseconds a move, bounded to about a tenth of the clock over a whole game | How big a delay correction can be before it becomes a way to steal time, and why the cap is per move with a small bank rather than unlimited. |
Leaderboard memory for 10 million players | Assume about 100 bytes a member for the id, the score and the ordered-index overhead: 10,000,000 × 100 bytes ≈ 1 GB. | About 1 GB for one sorted set holding all rated players | Why one Redis instance with a replica holds the whole leaderboard, and sharding it is unnecessary. |
Rating and leaderboard writes a second | At a steady peak, games end about as fast as they start: about 1,400 a second, two players each, so about 2,800 rating updates a second. A sorted set update costs O(log N); log2(10,000,000) ≈ 23 steps. | About 2,800 small ordered-index updates a second | The write load on the leaderboard store, which is modest next to the live path. |
Move record storage | At about 50 bytes a move (game id, move number, move, time used) and 80 half-moves, a game is about 4 KB. Assume the daily average start rate is a quarter of the 1,400-a-second peak: 350 × 86,400 ≈ 30 million games a day, 30,000,000 × 4 KB ≈ 121 GB a day, about 44 TB a year. | Roughly 120 GB of moves a day, tens of terabytes a year | Why the live move log keeps a short window and finished games move to a cheaper archive store. |
Spectator servers for one big crowd | Example: a top game draws 200,000 watchers. At the same 50,000 sockets a server, that crowd needs 4 spectator servers, each receiving one copy of each move from the game's channel and pushing 50,000 copies out. | One published move per game, a handful of servers pushing it on | The fan-out tier for a popular game, and why the game server itself sends each move once however many people watch. |
Core entities
| Entity | What it holds |
|---|---|
Player | One row per account, with the rating that matchmaking and the leaderboard read. |
Match ticket | A player waiting for an opponent, kept in the matchmaking pool until paired or withdrawn. |
Game | A live or finished game: who plays, the position so far, both clocks and the result. |
Move | One accepted half-move, the unit the move log stores and the clients replay. |
Game ownership | Which game server currently runs a game, read on connect and rewritten on failover. |
The API
| Endpoint | Notes |
|---|---|
| Puts a ticket for the caller into the pool for that control and returns right away; the client keeps its socket open and the game id arrives on it. If the caller already has an active game, respond with that game id instead of a ticket, which is how a second tab is kept out of a second game. A repeat request from the same player replaces the ticket rather than adding one. |
| The socket goes to the server that owns the game, found through the ownership lookup. The client includes |
| Used on reconnect and by a watcher joining late: returns players, the move list, both clocks and the status from the owning server, falling back to the move log if the owner is being replaced. The client then reopens the socket and sends the last move number it has, and gets what it missed. |
| The page is a range read from the sorted set in rank order; the second call is a rank lookup for the caller's id in O(log N); the range read costs O(log N) plus the page size. Serve from a replica, and tell the caller the result may trail the latest games by a few seconds. |
The high-level design
Step 1 of 4
Matchmaking and a game server
Start with the two things a player does: ask for a game and play it. A matchmaking service takes POST /matchmaking, puts a ticket in a waiting pool ordered by rating, and pairs the caller with the nearest rating in range. It then asks a game server to start the game, and both players open a WebSocket to that server. The game server keeps the board, the move list and both clocks in memory; on each move it checks legality and turn, charges the clock, and sends the move and both clocks to both players. We'd keep the pool in Redis rather than a relational table: about 1,400 pairings a second, each a find-and-remove of two tickets under a lock, is contention we'd rather not put on one table.
What breaks first
The deep dives
Running 500,000 live games. Half a million games are live at once, two sockets each. Where does a game live while it is being played, how does a move get from one player to the other, and what happens to the thousands of games on a server that dies mid-move?
Fair clocks. One player is on a slow mobile connection and each of their moves arrives later than the opponent's. What decides when a player's time is up, how is each move charged, and how do both screens show the same remaining time?
Spectators. A top game draws a crowd many times larger than its two players. How does a move get from the board to each watcher, what fails first on the game server if that crowd grows 100x, and what does a watcher who joins halfway through see?