The idea: the server decides, the client guesses

In most online action games one machine, the authoritative server, runs the real game. Players' machines, the clients, only send their inputs (keys pressed) and draw what the server tells them. This stops cheating: a client cannot simply say "I am now at the exit". But every message takes time. Here every link takes 100 ms each way, so a round trip (RTT) is 200 ms. The page shows the tricks games use so the delay does not ruin the feel.

Client A on the left, the server in the middle, client B on the right. Numbered inputs flow from A to the server, snapshots with tick, x and ack flow back to A and on to B, each link 100 ms.
Inputs go up to the server, snapshots come back down; every arrow takes 100 ms.

The model runs in ticks of 50 ms (a server tick rate of 20 Hz). You are client A and hold the right arrow on ticks 1 to 8; each tick moves A one unit. Every input gets a sequence number, here the tick it was made on: #1, #2, and so on. The server applies input #k when it arrives, at tick k + 2. Every 2 ticks (10 Hz) it sends a snapshot: the tick, A's position x and the ack, the last input number it applied. Client B is another player who watches A. The table under the drawing lists where A is at each tick: on the server, on A's own screen and on B's screen. How a game splits time into fixed steps is on The Game Loop; what latency is, apart from bandwidth, is on Latency vs Throughput.

Without prediction, every move is late

The simple client draws only what the server says. A presses right at tick 1. Input #1 reaches the server at tick 3, the next snapshot leaves at tick 4 and arrives at tick 6. A sees itself move only at tick 6, 250 ms after the press: one RTT plus the wait for a snapshot. Then it moves in jumps of 2 units every 2 ticks, because snapshots come every 2 ticks (Demo 1). At 250 ms the game feels like walking through mud.

Client-side prediction

The client runs the same movement code as the server. So client-side prediction applies each input at once: A is at 1, 2, 3 on ticks 1, 2, 3, with no delay. The client still sends every input to the server, which stays in charge.

The trouble comes when a snapshot arrives. It is old: the snapshot that arrives at tick 4 was made at tick 2, before the server had seen any input. If the client simply copies its x = 0, A jumps back from 3 to 1 (0 plus input #4). It happens again at every snapshot: A shows 1, 2, 3, 1, 2, 3, 4, 5, 5, 6, 6, 8. This jitter is called rubber-banding (Demo 2).

Server reconciliation: replay the unacked inputs

The fix uses the ack. The client keeps every input the server has not acked yet. When a snapshot arrives it does three things:

  1. Reset A to the snapshot's x. That position is the truth for the acked input.
  2. Drop the inputs up to the ack.
  3. Replay the inputs after the ack, in order, then apply this tick's input.
on snapshot (x, ack):
    pending = [i for i in pending if i.seq > ack]
    pos = x
    for i in pending:  pos = move(pos, i)
pos = move(pos, input_now);  pending.append(input_now);  send(input_now)
At tick 6 the snapshot from tick 4 says x = 2 and ack 2. Copying it gives x = 3 after input 6. Reconciling replays inputs 3, 4 and 5 from x = 2 to reach 5, then input 6 gives 6, the same as the prediction.
The snapshot says where A was after input #2; replaying #3 to #5 brings it back to now.

At tick 6 the snapshot from tick 4 arrives: x = 2, ack #2. The client replays #3, #4 and #5 (2 → 5) and adds #6: x = 6, the same as its prediction. With nothing unexpected, the result is always the prediction, so A moves 1, 2, 3, … 8 with no delay and no jump (Demo 3). The list of unacked inputs is never longer than 5 here: one RTT of inputs (4) plus at most one tick of waiting for a snapshot.

A misprediction

Prediction is a guess. In Demo 4 an explosion on the server at tick 5 pushes A back 2 units; client A could not know. A keeps predicting 1 … 7. At tick 8 the snapshot from tick 6 arrives: x = 2, ack #4. The client had stored that after #4 it would be at 4. The server says 2, so it knows it guessed wrong. Reconciliation fixes it the same way: from 2, replay #5, #6, #7 and add #8: x = 6, one unit less than the 7 it showed a tick ago. A ends at 6, the same as the server. Real games hide such a correction by blending it in over a few frames, instead of snapping.

tick1234567891012
server x00121234566
A, reconcile12345676666

Other players: entity interpolation

Client B cannot predict A, because it does not know A's keys. If it draws A at the newest snapshot, A stands still for a tick and then jumps 2 units, every time (Demo 5). So B draws A a little in the past instead: 100 ms of interpolation delay behind the newest snapshot. At tick t, B draws tick t − 4 (100 ms of latency plus the 100 ms delay). It always has a snapshot before and one after that time, and it draws A part of the way between them. At tick 9 B draws tick 5, halfway between snapshot t4 (x = 2) and t6 (x = 4): x = 3.

Now A moves one unit per tick on B's screen too, but late: B sees A at max(0, t − 6), 300 ms behind A's own screen, and reaches 8 at tick 14 (Demo 6). The delay also covers a late or lost snapshot, like the jitter buffer of a voice call.

Lag compensation: the server rewinds

Interpolation makes shooting hard. In Demo 7 B fires at tick 8 at A where B sees it, x = 2. That picture is from tick 4. The shot reaches the server at tick 10, when A is at 8. If the server checks A's position now, it is a miss by 6 units, though B aimed perfectly.

Lag compensation fixes this. The server keeps a short history of every player's position, one entry per tick. When a shot arrives, it rewinds the target to the tick the shooter saw: B's latency (100 ms) plus the interpolation delay (100 ms) plus the shot's own trip (100 ms), 6 ticks or 300 ms here. At tick 4 A was at 2: a hit (Demo 8). Then it puts everyone back. The price is paid by the target: on A's own screen it was already at 8, far from where it was hit. "I was shot behind the wall" is this effect. Servers cap how far they rewind (Source: about 1 s) so a very slow player cannot shoot far into the past.

Time goes down. Server tick 4: A at x 2, snapshot sent. Tick 6: B receives it. Tick 8: B draws tick 4 and fires at x 2. Tick 10: the shot arrives; A is at 8 now, so the server rewinds 6 ticks to tick 4 and finds A at 2: hit.
The shot is checked against where A was when B saw it, 300 ms before the shot arrived.

In real engines

ideaSource engine (Valve)Unreal Engine
tick rateserver tick, for example 64 HzNetServerMaxTickRate
snapshot ratecl_updaterate (snapshots per second asked for)NetUpdateFrequency per actor
input numbering, replayuser commands (usercmd) with a command numbersaved moves in CharacterMovementComponent
interpolation delaycl_interp (default 0.1 s)smoothing of simulated proxies
lag compensationon by default; rewinds hitboxes, at most sv_maxunlag = 1 sleft to the game code

Snapshots and inputs go over UDP: a newer snapshot makes an older lost one useless, so waiting for TCP's retransmission would only add delay (TCP vs UDP). Clients also need a rough idea of the server's clock to know which tick to draw; how clocks are matched over a network is on NTP.

See also Behaviour Trees for how the server decides what a computer-controlled character does on each tick.

See also Behaviour Trees for how the server decides what a computer-controlled character does on each tick.

What the page leaves out

The page uses one fixed latency with no jitter or loss, one dimension, one moving player and whole-unit moves. Real games blend corrections over several frames, extrapolate briefly when snapshots stop, send delta-compressed snapshots, resend each input a few times in case one is lost, adapt the interpolation delay to the measured jitter, and cap or slow down clients whose clocks drift. Fighting games often use rollback netcode instead, which predicts the other player too and re-simulates whole game states. Peer-to-peer lockstep, interest management (sending only nearby players) and anti-cheat checks are also left out.

On the mind map

This page on the Geometric Algorithms mind map · 28 pages in 7 branches · open it to see the whole map and test yourself in recall mode.