Design a Live Multiplayer Typing Race with Spectator Support
System DesignModel answerLast updated
Design a real-time platform where players matched by language race to type the same passage, see live progress and errors, serve error-freeze penalties, and can be watched by spectators.
Live Multiplayer Typing Race
You are designing a platform where users compete in real-time typing races. A player picks a language, gets matched with others into a race, and everyone types the same passage from start to finish. The race is live: each racer sees every other racer's cursor position and mistakes as they happen. Making a mistake locks that racer's keyboard for one second. Anyone who wants to watch a race in progress can join as a spectator.
What the system must do
- Match players by chosen language into races of 2 to 8 participants, all typing the identical passage.
- Validate keystrokes on the server and enforce a 1-second input freeze whenever a racer makes an error.
- Broadcast each racer's progress and errors to all other racers and to any spectators watching that race, in real time.
- Allow spectators to watch without affecting the race itself.
What is intentionally left open — state your assumptions or ask first
- How many concurrent racers and spectators does the system need to handle?
- What latency is acceptable for the live view — 100 ms? 500 ms?
- How long can a player wait in the matchmaking queue before a race starts with fewer than 8 players?
- How does a race end and how is the winner determined — first to finish, time limit, partial-progress ranking?
- Does "language" mean the natural language of the passage (English, French, …), a programming language for code snippets, or both?
- Are keypresses that arrive during the freeze period silently dropped or queued?
- Must the system defend against cheating such as automated input or clipboard paste?
- Do race results feed a persistent leaderboard or player ranking?
What is expected
Walk through your design end-to-end. Cover matchmaking, the race loop, real-time state distribution to racers and spectators, fairness under network jitter, and failure handling. Include a rough data model, the key APIs or message types, and capacity estimates tied to your stated assumptions.
Hints
Hint 1
Start by pinning down your assumptions on scale and latency — every downstream choice (transport, topology, fan-out strategy) depends on them. A matchmaking queue per language is the natural unit; think about what triggers a race to start early when the queue is short.
Hint 2
The server must be the authority on keystroke validity and freeze timing, not the client. Sketch the per-race state the server maintains (cursor position, freeze-until timestamp, finish time) and the exact message the server emits after each valid keystroke so all viewers stay in sync.
Hint 3
Spectator fan-out is the scalability pressure point. Spectators should receive the same event stream as racers but through a path that cannot block or slow the race loop — think about separating the write path (racer → race server) from the read/broadcast path (race server → everyone watching).
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions (state these up front)
- 50 000 concurrent racers, up to 10× that many spectators (500 000 viewers).
- Target latency for the live view: ≤ 200 ms end-to-end.
- Matchmaking timeout: 10 seconds; a race starts with as few as 2 players if the queue hasn't filled to 8 by then.
- Race ends when the first racer finishes; remaining racers have 30 more seconds before the race is force-closed. Ranking is by finish time, with partial progress as the tiebreak.
- "Language" means natural language of the passage; the character set and keyboard layout are communicated to the client at race setup.
- Keypresses arriving during a freeze are silently dropped.
- Anti-cheat is out of scope for this design but the server-authoritative model makes it easy to layer in.
Clarifying questions worth asking
Before drawing boxes, confirm: Is this global or single-region? (Affects whether race servers are colocated with matched players.) Should spectators see a slight delay (e.g., 2–3 s) to allow for moderation? These don't change the core architecture but do change capacity numbers.
Matchmaking
A matchmaking service maintains one in-memory queue per language. When a player connects, the gateway authenticates them and adds them to the right queue. A background worker polls each queue every 500 ms:
- If ≥ 8 players are waiting → start a race immediately.
- If ≥ 2 players have been waiting ≥ 10 s → start a race with however many are present.
When a race fires, the worker atomically removes those players from the queue (using a compare-and-swap or a Redis transaction to prevent double-placement), selects a passage from the passage store for that language, and emits a race_created event containing the race ID, passage, player list, and a starts_at UTC timestamp set 3 seconds in the future. That 3-second countdown gives clients time to connect to the assigned race server before the clock starts — this is the synchronized start.
Passages are pre-generated and stored in a database keyed by language and difficulty tier. Serving a cached passage adds no latency to race startup.
Transport
Both racers and spectators connect via WebSocket. The race server is the single authority for a race; clients do not talk to each other. HTTP/2 server-sent events are a valid alternative for the spectator read path, but WebSocket keeps the implementation uniform.
Race server and the authoritative loop
Each race runs on one race server process (or goroutine/thread). In-memory state per race:
race_id, passage (string), started_at, status
per player: player_id, cursor (int), errors (int), freeze_until (timestamp), finished_at
When the server receives a keystroke message {race_id, player_id, char, client_ts}:
- Check
freeze_until— ifnow < freeze_until, drop the keystroke and reply with{type: "frozen", until: freeze_until}. - Compare
chartopassage[cursor].- Correct: increment cursor; if cursor == len(passage), record
finished_at. - Incorrect: set
freeze_until = now + 1s, increment errors.
- Correct: increment cursor; if cursor == len(passage), record
- Emit a
progressevent to all subscribers of this race:
{
"type": "progress",
"player_id": "p42",
"cursor": 87,
"errors": 3,
"freeze_until": null,
"ts": 1718000000123
}
The server never trusts client_ts for game logic — it uses server time for freeze deadlines and finish ordering. This closes the obvious latency-gaming exploit.
Fan-out to spectators
Racers are few (≤ 8); broadcasting to them inline is fine. Spectators are unbounded and must not sit in the hot path.
After writing the progress event to the in-memory race state, the race server publishes it to a pub/sub topic (e.g., a Redis channel or a Kafka topic keyed by race ID). A separate pool of spectator relay servers subscribes to those topics and pushes events over WebSocket to all connected spectators. This decouples spectator scale from race server capacity: you can scale relay servers independently, and a slow or crashing relay cannot stall the race loop.
Spectators joining mid-race receive a snapshot of the current race state (all cursors, errors, freeze statuses) on connect, then start receiving live events.
Fairness under network latency
- Synchronized start: all clients receive
starts_atand begin their countdown locally. The server rejects any keystroke with a server timestamp beforestarts_at. - Freeze enforcement: freeze deadlines are server-side timestamps, so a high-latency client's freeze lasts the same wall-clock second as a low-latency client's.
- Finish ordering:
finished_atis recorded by the server when it processes the final correct keystroke, not when the client claims to have finished. This prevents a fast network from manufacturing an earlier finish time.
Disconnects and failures
- Player disconnects: the player's slot is marked
disconnected; their last cursor position is frozen in the broadcast. If they reconnect within 15 s and provide the same race ID, they resume from their last server-acknowledged cursor. After 15 s the slot is retired and the race continues. - Race server crash: a watchdog detects the missing heartbeat and marks the race
failed. Players get a "race ended" notification. Because passages and per-player state can be checkpointed to Redis every few seconds, a recovery path is possible, but for a first version, abandoning the race and returning players to matchmaking is simpler.
Data model (abbreviated)
| Table | Key columns |
|---|---|
races | race_id, language, passage_id, status, started_at, ended_at |
race_players | race_id, player_id, cursor, errors, finished_at, rank |
passages | passage_id, language, text, difficulty |
Active race state lives in memory on the race server with a Redis mirror for recovery. Completed races are flushed to the database.
Capacity check
At 50 000 concurrent racers in races of average 5 players, there are ~10 000 active races. At 10 keystrokes/s per racer, the system handles ~500 000 keystroke events/s. Each event fan-outs to ≤ 7 other racers plus N spectators. With 500 000 spectators spread across races, average spectator load per race is ~50. The relay layer needs to push ~500 000 × 50 = 25 million messages/s total — achievable with a horizontally scaled relay tier and efficient binary framing (MessagePack or protobuf over WebSocket).
Common mistakes to avoid
- Trusting the client for freeze timing or finish detection — the server must own all game state.
- Putting spectator delivery in the race server's hot loop — one slow connection can introduce head-of-line blocking.
- Using a single global matchmaking queue instead of per-language queues — players get mismatched passages.
- Forgetting the synchronized start countdown — without it, players with faster connections get a real head start.