TExcelTAD
Home › Multiplayer Game Networking › Tick Rate & Clock Sync

Tick Rate & Clock Sync

A networked game needs everyone to agree on when things happen. "The player jumped" is useless unless the server knows at which step of the simulation it happened. So multiplayer games run their simulation in fixed steps called ticks, number them, and keep every machine's idea of the current tick in sync.

A fixed tick, always

The server simulates at a fixed rate, for example 30 or 60 ticks per second, with the same accumulator loop you know from The Fixed-Step Loop and Fixed Timestep. Every input, snapshot and event is stamped with a tick number, not a time:

Rate Tick length Used by
10–20 Hz 50–100 ms MMOs, survival games, many co-op games
30 Hz 33.3 ms many action games, mobile shooters
60–64 Hz 15.6–16.7 ms competitive shooters, fighting games
128 Hz 7.8 ms some competitive shooter servers

Higher rates mean less waiting and finer hit registration, but cost CPU on the server (per match!) and bandwidth. A variable tick rate ("as fast as the server can") makes every later technique harder: prediction and reconciliation only work if the client and server run the same steps.

Tick rate and send rate are different things. A server can simulate at 60 Hz and send snapshots at 20 or 30 Hz; clients can sample input every tick and send a packet every second tick with the last few inputs inside.

Why the clocks disagree

The client and server start at different moments, and each one's clock (performance.now(), Stopwatch, Time.realtimeSinceStartup) counts from its own zero. Even if they started together, clocks drift by tens of parts per million. To agree on the tick, the client must estimate the offset between its clock and the server's.

NTP-style synchronization

The classic method comes from NTP. The client sends a request stamped with its time t0. The server notes when it received it (t1) and when it replied (t2), both in server time. The client notes when the reply arrived (t3):

text
client  t0 ----request----> t1  server
client  t3 <----reply------ t2  server

round trip = (t3 - t0) - (t2 - t1)
offset     = ((t1 - t0) + (t2 - t3)) / 2      (server clock minus client clock)

The formula assumes the trip there takes as long as the trip back. When it doesn't (jitter, asymmetric routes), the estimate is off by half the difference. The fix is to take several samples and trust the one with the smallest round trip, because it suffered the least queueing:

ts · try it yourself
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(3);
const TRUE_OFFSET = 5234;                              // server clock = client clock + 5234 ms
const oneWay = () => 40 + Math.round(rnd() * 60);      // 40..100 ms, different each way

const samples: { rtt: number; offset: number }[] = [];
let clientNow = 1000;
for (let i = 0; i < 6; i++) {
  const t0 = clientNow;
  const t1 = t0 + oneWay() + TRUE_OFFSET;              // server receives (server time)
  const t2 = t1 + 1;                                   // server replies 1 ms later
  const t3 = t2 - TRUE_OFFSET + oneWay();              // client receives (client time)
  const rtt = (t3 - t0) - (t2 - t1);
  const offset = ((t1 - t0) + (t2 - t3)) / 2;
  samples.push({ rtt, offset });
  console.log(`sample ${i}: rtt ${rtt} ms, offset ${offset} ms (error ${offset - TRUE_OFFSET})`);
  clientNow = t3 + 200;
}
const best = samples.reduce((a, b) => (b.rtt < a.rtt ? b : a));
const mean = samples.reduce((s, x) => s + x.offset, 0) / samples.length;
console.log(`lowest-RTT sample: offset ${best.offset} (error ${best.offset - TRUE_OFFSET})`);
console.log(`plain average:     offset ${mean.toFixed(1)} (error ${(mean - TRUE_OFFSET).toFixed(1)})`);
sample 0: rtt 125 ms, offset 5254.5 ms (error 20.5)
sample 1: rtt 111 ms, offset 5245.5 ms (error 11.5)
sample 2: rtt 155 ms, offset 5242.5 ms (error 8.5)
sample 3: rtt 119 ms, offset 5243.5 ms (error 9.5)
sample 4: rtt 129 ms, offset 5225.5 ms (error -8.5)
sample 5: rtt 97 ms, offset 5237.5 ms (error 3.5)
lowest-RTT sample: offset 5237.5 (error 3.5)
plain average:     offset 5241.5 (error 7.5)

Keep re-syncing every few seconds (the drift is slow), and when the estimate changes, slew the clock gently (run slightly faster or slower for a while) rather than jumping: a sudden jump of 20 ms makes everything on screen hitch.

Which tick should the client be on?

Once the client knows the server's clock, it can decide what tick to run. Two different answers are useful:

  • Remote entities (other players) are shown slightly in the past: server tick minus an interpolation delay, so there are always two snapshots to blend between (Snapshot Interpolation).
  • The local player's inputs must arrive before the server simulates that tick. So the client runs ahead of the server: its current tick is the server's tick plus half the RTT plus a small safety buffer for jitter.
csharp · try it yourself
using System;

const double TickMs = 1000.0 / 30;    // 30 Hz
double serverTimeMs = 120_010;        // estimated server clock right now
double rttMs = 150, jitterMs = 20, interpDelayMs = 100;

int serverTick = (int)(serverTimeMs / TickMs);
int inputTick = (int)Math.Ceiling((serverTimeMs + rttMs / 2 + 2 * jitterMs) / TickMs);
int viewTick = (int)((serverTimeMs - rttMs / 2 - interpDelayMs) / TickMs);

Console.WriteLine(
quot;server is simulating tick {serverTick}"); Console.WriteLine(
quot;client stamps its input as {inputTick} (+{inputTick - serverTick} ticks ahead)"); Console.WriteLine(
quot;client renders others at {viewTick} ({serverTick - viewTick} ticks behind)");
server is simulating tick  3600
client stamps its input as 3604  (+4 ticks ahead)
client renders others at   3595  (5 ticks behind)

So a player at 150 ms sees the present for themselves (predicted), the past for everyone else, and the server lives in between. Keeping those three timelines straight is most of the work in Client-Side Prediction and Lag Compensation.

Adjusting on the fly

Instead of computing the lead from estimates alone, many engines let the server tell the client how early its inputs arrive. If inputs arrive with less than one tick to spare (or late), the client speeds up its simulation slightly; if they arrive many ticks early, it slows down. That feedback loop automatically adapts to the player's real RTT and jitter, and it is what "time dilation" in some netcode libraries refers to.

Test yourself

In NTP-style sync, why prefer the sample with the smallest round trip?

  1. It was sent most recently
  2. It suffered the least queueing delay, so its two directions were most likely symmetric
  3. It has the largest offset
  4. The server marks it as authoritative

Why does a predicting client run its own tick counter ahead of the server's?

  1. To cheat
  2. So its inputs for tick N reach the server before the server simulates tick N
  3. Because client clocks are faster
  4. To reduce bandwidth

Your server simulates at 60 Hz. Must it also send snapshots at 60 Hz?

  1. Yes, always
  2. No: send rate and tick rate are independent; sending at 20–30 Hz is common
  3. No: it should send at 120 Hz
  4. Only on LAN