Field Notes: Designing for Latency
These are the lessons multiplayer programmers learn from cancelled prototypes, launch-week patches and angry forum threads. Most of them come down to one sentence: the network is part of the game design, not a feature you add later.
1. Design for latency from day 1
Retrofitting multiplayer onto a finished single-player game is one of the most expensive mistakes in game development. Single-player code reads input and changes state anywhere: in UI callbacks, in animation events, in coroutines, in physics callbacks. A networked game needs every gameplay change to flow through one path: input → simulation tick → state → presentation. If you might ever want multiplayer:
- Run gameplay in a fixed-tick simulation that only reads inputs, from the first prototype.
- Keep simulation and presentation separate: the simulation never touches audio, particles, UI or the camera; those read the state and react.
- Give every gameplay object a stable id that could be sent over a network.
- Make the movement code a pure function of
(state, input, tickLength).
Even if the game never ships online, this structure makes replays, save games, bots and automated tests easier.
2. Test at 150 ms and 2% loss, every day
Developers play on localhost or the office LAN, where everything works. Players don't. Make 150 ms RTT, ±20 ms jitter and 2% loss the default in development builds, with a hotkey to switch it off, and add bursts of 5–10 lost packets now and then (Network Conditioners, Wireshark & Profilers). Bugs that only appear under latency (double pickups, doors that open twice, hits that don't count) are found weeks earlier.
Here is the classic one: two players run for the same health pack. At 0 ms it's obvious who got there first. At 150 ms, with clients that apply pickups locally, both win:
type Pickup = { id: string; takenBy: string | null };
function race(clientsTrustThemselves: boolean) {
const server: Pickup = { id: 'medkit', takenBy: null };
// Both players touch the medkit on their own screens at about the same time.
const requests = [{ player: 'ana', arriveMs: 95 }, { player: 'ben', arriveMs: 140 }];
const shown: Record<string, string> = {};
for (const r of requests) {
if (clientsTrustThemselves) shown[r.player] = 'healed (decided locally)';
else shown[r.player] = 'pickup predicted, waiting for server...';
}
for (const r of [...requests].sort((a, b) => a.arriveMs - b.arriveMs)) {
if (server.takenBy === null) server.takenBy = r.player;
if (!clientsTrustThemselves) {
shown[r.player] = server.takenBy === r.player ? 'healed (confirmed)' : 'pickup rolled back: ben was too late';
}
}
const healed = Object.values(shown).filter((s) => s.startsWith('healed')).length;
console.log(`${clientsTrustThemselves ? 'client decides' : 'server decides'}: server says ${server.takenBy}; ana: ${shown.ana}; ben: ${shown.ben}; players healed: ${healed}`);
}
race(true);
race(false);client decides: server says ana; ana: healed (decided locally); ben: healed (decided locally); players healed: 2
server decides: server says ana; ana: healed (confirmed); ben: pickup rolled back: ben was too late; players healed: 1
3. Send inputs, not state
The client sends what the player wants, never what happened: "fire, aiming at 37°" rather than "I hit player 4 for 30". It keeps the rules on the server, keeps packets small, and makes prediction and replays possible (The Authoritative Server). Every time you are tempted to add an RPC like SetHealth, SetPosition or GiveItem callable by the client, stop.
4. Never trust the client
Assume every packet was written by an attacker with a hex editor, because some will be. Validate ranges, rates, cooldowns, distances and line of sight on the server; reject what doesn't fit and log it (Never Trust the Client). That includes values that "can't" be wrong because your UI doesn't allow them.
5. Keep the tick rate fixed
Prediction, reconciliation, lockstep, rollback and lag compensation all assume that the client and server run the same steps. If the server's tick length varies with load, or the client simulates with its frame's deltaTime, the same inputs produce different results and you get corrections forever:
const ACCEL = 30;
// Simulate 0.5 s of accelerating from rest, with semi-implicit Euler.
function simulate(steps: number[]) {
let v = 0, x = 0;
for (const dt of steps) { v += ACCEL * dt; x += v * dt; }
return x;
}
const fixed = Array(30).fill(1 / 60); // server: 30 fixed ticks
const frames = [0.016, 0.017, 0.033, 0.016, 0.050, 0.017, 0.016, 0.033, 0.016, 0.017, 0.050, 0.016, 0.017, 0.033, 0.016, 0.017, 0.016, 0.017, 0.016, 0.071]; // client: variable frames
console.log(`fixed ticks: ${fixed.length} steps, total ${fixed.reduce((a, b) => a + b, 0).toFixed(4)} s, x = ${simulate(fixed).toFixed(4)}`);
console.log(`variable frames: ${frames.length} steps, total ${frames.reduce((a, b) => a + b, 0).toFixed(4)} s, x = ${simulate(frames).toFixed(4)}`);
console.log(`difference after half a second: ${(simulate(frames) - simulate(fixed)).toFixed(4)} units, and it grows`);fixed ticks: 30 steps, total 0.5000 s, x = 3.8750
variable frames: 20 steps, total 0.5000 s, x = 4.0064
difference after half a second: 0.1313 units, and it grows
Same duration, same acceleration, different answer, because integration error depends on the step size. Simulate in fixed ticks everywhere and interpolate only the rendering (Tick Rate & Clock Sync).
6. Use a real clock-sync model
"Server time" on the client should be an estimate with a known error, maintained NTP-style from round-trip samples, preferring the fastest ones, and slewed smoothly rather than jumped. Never use the client's wall clock for game logic, and never trust a timestamp the client sends ("I fired at 12:00:00.000") without bounding it by what the server already knows about that client's latency.
7. Number everything, log everything
Put the tick number in every log line on both sides, plus match id and player id. When a player reports "I shot him and nothing happened", you need to find tick 48213 in both the client's and the server's logs. Better still, record the inputs of every match (they're tiny) so you can replay it in the editor and step through the exact tick.
8. Show the network to the player
A small overlay with ping, packet loss and a "connection problem" icon turns "this game is broken" into "my Wi-Fi is bad". Show "reconnecting…" on frozen players. Make high-ping matches visible in the lobby (Lobbies & Matchmaking).
9. Hide latency with design, not only code
- Start actions with an instant local animation (wind-up, muzzle flash, footstep) while the server confirms.
- Give slow projectiles travel time; players accept that rockets are slow, and the server can simulate them honestly.
- Prefer generous hitboxes and abilities with a short wind-up over frame-perfect mechanics, unless frame-perfect is the point of the game.
- Avoid mechanics where two players race for the same thing within a few hundred milliseconds, or make the tie visible and fair (both get it, or the server picks and the loser gets clear feedback).
10. Start smaller than you think
The roadmap's warning bears repeating: don't start with an MMO. A 2–4 player arena exercises every technique in this course; persistence, sharding and economy come later, if ever.
Test yourself
Two clients each apply a pickup locally before the server confirms. What can happen under latency?
- Nothing; the first one always wins
- Both players believe they got the item
- The item is deleted
- The server crashes
Why must the client and server simulate with the same fixed tick length?
- To save bandwidth
- Integration results depend on the step size, so different steps give different results and constant corrections
- Because UDP requires it
- To make rendering faster
What should developers' builds use as the default network condition?
- Localhost, no latency
- About 150 ms RTT with jitter and 2% loss
- 1 second of latency
- 100% loss