Design Multiplayer Game State Sync
hard 50 minLet a hundred people share one moving world although their messages take 20 to 200 milliseconds.
Video lesson · Chapter 1 of 5
Your runner moves before the server replies
Theory
Read or listen first. About 1 min.
What it is
Game state sync keeps every player's picture of a game close to one true state. Inputs travel to a server, the server moves the world forward in small equal steps, and snapshots of the world travel back.
Messages take time, so no player ever sees the present of the others. The client hides that. It guesses its own movement ahead and shows everybody else a moment late.
Why it exists
Waiting for the server before moving makes a game feel like mud. Letting each client decide for itself makes it easy to cheat and impossible to agree.
The same ideas carry over to shared drawing, remote control and robots: act at once on a guess, then follow the one who decides.
A short history
August 1980
RFC 768 defines UDP, which sends single packets without order or repeats.
August 1996
In a public plan file, John Carmack announces QuakeWorld, a version of Quake made only for play over the internet.
March 2001
Paul Bettner publishes "1500 Archers on a 28.8", on Age of Empires: send commands, and let each machine simulate the same turns.
August 2020
Riot Games explains how VALORANT's servers run 128 ticks a second, and how a frame went from 50 ms to under 2 ms.
Words to know
- Tick
- One equal step of the simulation: 16.7 ms at 60 a second.
- Snapshot
- The state of the things a player needs, at one tick.
- Authority
- Who decides what is true. Here: the server.
- Prediction
- Applying your own input at once, before the answer.
- Reconciliation
- Going to the server's state and replaying later inputs.
- Interpolation
- Drawing others between two snapshots you already have.
- Lag compensation
- Judging a shot in the world the shooter saw.
- Interest management
- Sending only what matters to this player.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Input. A key moves your player at once and travels to the server
Understand it
Start here
Shadow puppets on three walls
What we build
A shooter for 100 players
The networking of one match: inputs, authority, snapshots, hit checks and recovery, plus running ten thousand such matches. Forming matches belongs to matchmaking. Rankings belong to the leaderboard.
Responsive
Your own move shows in the next frame.
Smooth
Others do not jump when a packet is lost.
Fair
What you aim at is what you hit, within a limit.
Honest
The client sends wishes, not facts.
The hard part
Nobody sees the present
A player in New York is 20 ms from the server and one in Sydney is 200 ms away. When the first fires, they aim at a place where the other was some time ago. We want four things: a quick feel, smooth movement, shots that land where they were aimed, and no cheating. They pull against each other, so each needs its own trick.
Four wishes, four tricks
The finished system
Where the design ends up
This is the last version. The Design tab starts with one service and one database.
The whole system
How kinds of game choose
| Game | Choice | Why |
|---|---|---|
| Shooter | Server authority, prediction, rewind | Aim and reaction |
| Strategy with thousands of units | Lockstep commands | State is too large to send |
| Car football | Prediction of the ball as well | Everyone touches one object |
| Fighting for two | Rollback between two machines | Every frame counts |
| Cards or chess | Plain requests | Nothing moves by itself |
| Battle royale | Interest management first | 100 players, few nearby |
Where to read more
The ideas behind it
Each idea has its own page.
- Pushing news: real-time APIs.
- What delay is made of: latency.
- Balancing packets: L4 and L7.
- Starting near players: global and local load balancing.
- Merging edits when nobody is the authority: google docs.
- Results after the match: log-based queues.
For the interview: Approach, Scope, Numbers, API and data, Deep dives and Mock.
Summary
The whole topic in one minute.
One process for each match simulates the world 60 times a second and is the only authority. Clients send inputs, several in each packet. They predict themselves and draw others about 100 ms late, between two snapshots.
When a guess was wrong the client takes the server's state and replays its later inputs. When a shot arrives the server looks at the world the shooter saw, at most 200 ms back. Deltas and interest cut the traffic by about 30 times.
- Send inputs up and state down. Never send results up.
- Predict yourself, draw others late.
- Old state is worthless, so do not wait for a lost packet.
- Rewind for shots, with a limit that protects the victim.
- Bandwidth is solved by who needs what, then by what changed.
Sources
- Riot Games: VALORANT's 128-tick servers
- Riot Games: peeking into VALORANT's netcode
- EA and Respawn: Apex Legends servers and netcode
- 1500 Archers on a 28.8
- Gabriel Gambetta: prediction and reconciliation
- Gabriel Gambetta: lag compensation
- Glenn Fiedler: snapshot compression
- RFC 9221: unreliable datagrams in QUIC
- RFC 8085: UDP usage guidelines
- RFC 9147: DTLS 1.3