The Authoritative Server
In an authoritative server model, the server runs the real game. Clients send what the player wants to do (inputs: "move left, aim here, fire"), the server decides what happens and tells everyone the result. A client's own copy of the world is just a view, which the server corrects whenever they disagree. This is the model behind almost every competitive online game, and the one the rest of this course builds on.
Send inputs, not state
Compare two ways to move a player:
| Client sends | Server does | A cheater can |
|---|---|---|
| "I am now at (250, 80)" | believes it | teleport, fly, walk through walls |
| "Right is held, aim 37°" (tick 1042) | moves the player by the game's rules, collides with walls | at most press keys very precisely |
With inputs, the worst a modified client can do is play perfectly; the rules stay on the server. Inputs are also smaller than state and they are the same size no matter how complex the world gets, which matters for upload bandwidth (Bandwidth Budgets).
The server loop
every tick (fixed rate, e.g. 30 Hz):
1. read all packets that arrived; put each input into that client's input buffer, by tick number
2. for each player: take the input for THIS tick (or repeat the last one if it is missing)
3. step the simulation once: movement, collisions, projectiles, damage, rules
4. every N ticks: build a snapshot per client and send it (with the last input tick processed)The input buffer absorbs jitter: inputs arrive early, late and out of order, but the server consumes exactly one per player per tick. If an input arrives after its tick has been simulated, it is too late and is dropped; if none has arrived when its tick comes, the server repeats the player's previous input (the player most likely kept holding the same keys).
function mulberry32(seed: number) {
return () => {
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const rnd = mulberry32(5);
type Input = { tick: number; move: -1 | 0 | 1 };
// The client stamps inputs 3 ticks ahead of the server; each packet takes 1..4 ticks to arrive.
const clientMoves: (-1 | 0 | 1)[] = [1, 1, 1, 1, 0, 0, -1, -1, -1, 1];
const inFlight: { arrive: number; input: Input }[] = [];
clientMoves.forEach((move, i) => {
const sentAt = i; // sent during server tick i
inFlight.push({ arrive: sentAt + 1 + Math.floor(rnd() * 4), input: { tick: i + 3, move } });
});
const buffer = new Map<number, Input>();
let x = 0, last: -1 | 0 | 1 = 0;
for (let tick = 3; tick < 13; tick++) {
const arrived = inFlight.filter((p) => p.arrive === tick).map((p) => p.input);
const notes: string[] = [];
for (const inp of arrived) {
if (inp.tick < tick) notes.push(`late input for ${inp.tick} dropped`);
else buffer.set(inp.tick, inp);
}
const inp = buffer.get(tick);
buffer.delete(tick);
const used: -1 | 0 | 1 = inp ? inp.move : last;
if (!inp) notes.push('missing, repeating last');
last = used;
x += used * 5;
const buffered = [...buffer.keys()].sort((a, b) => a - b).join(',');
console.log(`tick ${String(tick).padStart(2)}: arrived [${arrived.map((a) => a.tick).join(',')}] buffer {${buffered}} use ${String(used).padStart(2)} x=${String(x).padStart(3)} ${notes.join('; ')}`);
}tick 3: arrived [3,5] buffer {5} use 1 x= 5
tick 4: arrived [] buffer {5} use 1 x= 10 missing, repeating last
tick 5: arrived [4,7] buffer {7} use 1 x= 15 late input for 4 dropped
tick 6: arrived [6] buffer {7} use 1 x= 20
tick 7: arrived [] buffer {} use 0 x= 20
tick 8: arrived [8] buffer {} use 0 x= 20
tick 9: arrived [9,10] buffer {10} use -1 x= 15
tick 10: arrived [12] buffer {12} use -1 x= 10
tick 11: arrived [] buffer {12} use -1 x= 5 missing, repeating last
tick 12: arrived [11] buffer {} use 1 x= 10 late input for 11 dropped
Inputs arrive in bursts and out of order, two arrive too late, and the inputs for ticks 4 and 11 are missing when needed. Repeating the last input guessed right both times here (the player was still holding the same key), so the server ends at x = 10, exactly where the client's own inputs say it should be. When an input is repeated by mistake (the player had actually released the key), the player's own prediction and the server diverge for a moment; Server Reconciliation is how the client recovers. The bigger the input buffer (how far ahead the client stamps its inputs), the fewer late inputs, at the cost of more delay between pressing and the server acting. The client adjusts that lead from the server's feedback (Tick Rate & Clock Sync).
What goes in the snapshot
Each snapshot tells a client the state of the world at a tick, plus one extra number that makes prediction possible: the last input tick the server applied for this client. With it, the client knows which of its own inputs the server has already accounted for (Client-Side Prediction).
Snapshot (server tick 1042, for client 3):
lastProcessedInput = 1041
entities: [ {id 1, x, y, yaw, anim}, {id 3, x, y, yaw, anim, hp, ammo}, ... ]
events since last ack: [ "player 5 hit player 1 for 25" ]Note that the client's own entity can carry more fields (ammo, cooldowns) than others see. Sending other players' private data (their ammo, their position behind a wall) is how wallhacks get their information (Cheat Types & Anti-Cheat).
The cost of authority
- Latency: without tricks, everything the player does waits a round trip. Prediction fixes that for the local player; interpolation smooths everyone else; lag compensation makes shooting fair.
- Servers: someone has to run them, for every match (Dedicated Servers & Hosting).
- Shared code: the client must run the same movement code as the server to predict well. Keep the simulation in a module both can use: one C# assembly for a Unity client and a .NET server, one TypeScript package for a browser client and a Node server.
Tip: Start a multiplayer prototype with the server authoritative and no prediction at all, at 150 ms of simulated lag. It feels bad, but you see clearly what needs hiding, and every later technique has a correct baseline to compare against.
Test yourself
Why do authoritative designs send inputs rather than positions?
- Inputs are always reliable
- The server applies the game's rules to inputs, so a modified client can't teleport or walk through walls
- Positions can't be serialized
- Inputs need no tick number
An input for tick 50 arrives while the server is simulating tick 52. What happens?
- The server rewinds to tick 50
- It is too late and dropped; tick 50 already used the repeated previous input
- The server pauses
- It is applied twice
Which extra number in each snapshot lets the client reconcile its prediction?
- The server's FPS
- The last input tick the server processed for this client
- The client's IP address
- The number of players