AlgoViz

Design Multiplayer Game State Sync

hard 50 min

Let 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

0:00 / 4:52

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

  1. August 1980

    RFC 768 defines UDP, which sends single packets without order or repeats.

  2. August 1996

    In a public plan file, John Carmack announces QuakeWorld, a version of Quake made only for play over the internet.

  3. 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.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Input. A key moves your player at once and travels to the server

Understand it

Start here

Shadow puppets on three walls

One hand, three walls
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

A client machine holds prediction and a snapshot buffer. It sends inputs over UDP to a match process, which holds the simulation, a history ring of one second, and interest and delta coding per player. Snapshots come back over UDP. The match process writes results to a log and then PostgreSQL, and replays to object storage. A fleet manager starts match processes.
Pink is inputs, blue is snapshots, orange is work after or around the match.

How kinds of game choose

GameChoiceWhy
ShooterServer authority, prediction, rewindAim and reaction
Strategy with thousands of unitsLockstep commandsState is too large to send
Car footballPrediction of the ball as wellEveryone touches one object
Fighting for twoRollback between two machinesEvery frame counts
Cards or chessPlain requestsNothing moves by itself
Battle royaleInterest management first100 players, few nearby

Where to read more

The ideas behind it

Each idea has its own page.

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