Multiplayer & Netcode
Why the server owns the truth, and the prediction tricks that hide a hundred milliseconds of it from the player.
Latency Is The Whole Problem
There is no way to make the network faster, so multiplayer engineering is the craft of showing each player a plausible present built out of stale information — while keeping every client's version of events close enough to agree on who won.
Retrofitting this is famously painful. Prediction, authority and reconciliation reach into movement, combat, animation and input, so a single-player codebase rarely survives the conversion intact.
Authority & Topology
Someone has to be right when two clients disagree. That choice is the first architectural decision and it decides how cheatable the game is.
| Model | Trade-off |
|---|---|
| Authoritative server | Cheat-resistant, costs hosting, adds a round trip |
| Listen server (a player hosts) | Free, but the host has zero latency and can cheat |
| Peer-to-peer lockstep | No server, but one slow peer stalls everyone |
What travels matters as much as who decides. Sending full state to everyone every tick does not scale, so engines send deltas against the last acknowledged state, prioritise by relevance, and cull anything a player cannot see. Most game traffic is UDP: a late packet is worthless, and TCP's insistence on redelivering it stalls everything behind it.
Prediction, Reconciliation & Interpolation
Three techniques do nearly all the work of hiding latency, and they apply to different things.
Client prediction covers your own character. The client applies your input immediately instead of waiting, so movement feels instant, and simultaneously sends the input to the server.
Reconciliation handles being wrong. Each input carries a sequence number; when the server's authoritative state arrives, the client rewinds to it and replays every input since. If the prediction was right — the usual case — nothing visibly changes. If it was wrong, the correction is applied and smoothed rather than snapped.
Entity interpolation covers everyone else. Other players are rendered slightly in the past, between the last two received snapshots, so their movement stays smooth despite packets arriving irregularly. The cost is that you are aiming at where someone was ~100 ms ago.
| Technique | Applies to | Cost |
|---|---|---|
| Prediction | Your own character | Occasional visible corrections |
| Reconciliation | Fixing mispredictions | Replaying inputs each update |
| Interpolation | Remote entities | You see them slightly in the past |
| Lag compensation | Hit detection | The shooter's advantage over the target |
Lag compensation resolves what interpolation costs you: the server keeps a short history of past positions and rewinds the world to what the shooter actually saw when validating a shot. This is why you occasionally take damage after reaching cover — on the shooter's screen, you had not reached it yet. That is not a bug; it is the deliberate choice to make shooting feel fair to the person shooting.
Interview Questions
Why is the server authoritative?
Clients can be modified, so anything they assert can be forged. The client sends intent and the server decides outcomes — the only structure that resists cheating.
Why UDP rather than TCP?
A game update that arrives late is worthless. TCP guarantees ordered delivery, so one lost packet stalls everything behind it while it is retransmitted — exactly the wrong trade for state that is about to be replaced anyway.
How does client prediction work?
The client applies your input locally straight away rather than waiting for the round trip, so control feels instant while the authoritative result is still in flight.
What is reconciliation?
Inputs are numbered. When authoritative state arrives, the client rewinds to it and replays every input made since, so a correct prediction changes nothing visible and a wrong one is corrected smoothly.
Why render other players in the past?
Snapshots arrive irregularly, so interpolating between the last two known positions is what keeps remote movement smooth instead of teleporting each update.
Why do you get shot after reaching cover?
Lag compensation rewinds the server to what the shooter saw. On their screen the shot connected, and the design favours the shooter over the target.