TExcelTAD
Home › Multiplayer Game Networking › Latency, Jitter & Packet Loss

Latency, Jitter & Packet Loss

Three numbers describe how bad a player's connection is, and every netcode decision depends on them:

  • Latency: how long a packet takes to arrive. Usually quoted as RTT (round-trip time, "ping"), there and back.
  • Jitter: how much that delay varies from packet to packet.
  • Packet loss: the fraction of packets that never arrive.

A connection with a steady 120 ms is easier to play on than one that swings between 30 and 200 ms, so jitter matters as much as the average.

Where latency comes from

Part Typical size Can you fix it?
Distance (light in fibre ≈ 200 km per ms) 1 ms per 100 km one way; Europe ↔ US East ≈ 35–45 ms one way only by placing servers near players
Last mile: Wi-Fi, cable, mobile radio 1–30 ms, spiky on Wi-Fi and 4G/5G wired connections, better Wi-Fi
Routers and queues ("bufferbloat") 0–200 ms when the line is busy players' routers, not you
Your tick rate and send rate up to one tick of waiting at each end yes: 60 Hz waits less than 20 Hz
Your interpolation buffer 50–100 ms on purpose yes, it's a trade-off (Snapshot Interpolation)
Rendering and the display 1–3 frames yes, partly

Players feel the total from pressing a key to seeing the result, not just the ping, so the parts you control add up quickly.

A simulated link

All the models in this course use a small simulated link: every packet gets the base latency plus a random jitter, and some are dropped. The random generator is seeded, so every run drops the same packets. Here are 12 packets sent every 50 ms at 80 ms ± 30 ms with 10% loss:

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;
  };
}

class Link {
  private rnd: () => number;
  constructor(public latency: number, public jitter: number, public loss: number, seed: number) {
    this.rnd = mulberry32(seed);
  }
  /** Returns the arrival time, or null when the packet is lost. */
  send(now: number): number | null {
    if (this.rnd() < this.loss) return null;
    const j = (this.rnd() * 2 - 1) * this.jitter;          // uniform in [-jitter, +jitter]
    return Math.round(now + this.latency + j);
  }
}

const link = new Link(80, 30, 0.10, 51);
const arrivals: { seq: number; at: number }[] = [];
for (let seq = 0; seq < 12; seq++) {
  const sentAt = seq * 50;
  const at = link.send(sentAt);
  console.log(`#${String(seq).padStart(2)} sent ${String(sentAt).padStart(3)} ms -> ${at === null ? 'LOST' : `arrives ${at} ms (delay ${at - sentAt})`}`);
  if (at !== null) arrivals.push({ seq, at });
}
arrivals.sort((a, b) => a.at - b.at);
console.log('arrival order:', arrivals.map((a) => a.seq).join(' '));
# 0 sent   0 ms -> arrives 54 ms (delay 54)
# 1 sent  50 ms -> arrives 105 ms (delay 55)
# 2 sent 100 ms -> arrives 167 ms (delay 67)
# 3 sent 150 ms -> arrives 219 ms (delay 69)
# 4 sent 200 ms -> arrives 308 ms (delay 108)
# 5 sent 250 ms -> arrives 359 ms (delay 109)
# 6 sent 300 ms -> arrives 355 ms (delay 55)
# 7 sent 350 ms -> LOST
# 8 sent 400 ms -> arrives 501 ms (delay 101)
# 9 sent 450 ms -> arrives 506 ms (delay 56)
#10 sent 500 ms -> arrives 588 ms (delay 88)
#11 sent 550 ms -> arrives 623 ms (delay 73)
arrival order: 0 1 2 3 4 6 5 8 9 10 11

Look at the arrival order: #6 arrives before #5, and #7 never arrives. With jitter larger than the gap between packets, packets overtake each other. Any code that assumes "the last packet I received is the newest" breaks. That is why every game packet carries a sequence number and receivers drop anything older than what they already have.

Measuring RTT and jitter

Clients measure RTT by echoing: the client puts its send time in a ping, the server echoes it, and the client subtracts. Raw samples are noisy, so keep a smoothed RTT with an exponential moving average, and a jitter estimate as the smoothed absolute difference between consecutive samples (the same idea TCP and RTP use):

csharp · try it yourself
using System;

int[] samples = { 102, 98, 140, 101, 97, 180, 99, 103, 95, 100 };  // measured RTTs in ms
double srtt = samples[0], jitter = 0;
int prev = samples[0];

Console.WriteLine("sample  smoothed RTT  jitter");
foreach (int s in samples)
{
    srtt += (s - srtt) / 8.0;                       // alpha = 1/8, as TCP uses
    jitter += (Math.Abs(s - prev) - jitter) / 16.0; // RFC 3550 style
    prev = s;
    Console.WriteLine(
quot;{s,5} {srtt,10:F1} {jitter,6:F1}"); }
sample  smoothed RTT  jitter
  102        102.0      0.0
   98        101.5      0.2
  140        106.3      2.9
  101        105.6      5.1
   97        104.6      5.0
  180        114.0      9.9
   99        112.1     14.4
  103        111.0     13.7
   95        109.0     13.4
  100        107.9     12.8

The spikes to 140 and 180 ms move the smoothed RTT by 5 to 10 ms, and it keeps drifting back towards 100; the jitter estimate, though, jumps from 0 to over 12 ms and stays there for a while. Systems that need a safety margin (the interpolation buffer, input delay in lockstep, the server's rewind limit) size it from RTT plus a multiple of jitter, not from the average alone.

Loss is bursty

Real loss is rarely an even 2%. It comes in bursts: a Wi-Fi hiccup drops 5 packets in a row, then nothing for a minute. Design for both:

  • Single losses: send inputs redundantly (the last few inputs in every packet) so one loss costs nothing.
  • Bursts: the interpolation buffer runs dry and the client must extrapolate or freeze briefly; reliable messages need retransmission.

What "good" looks like

RTT Feels like Typical approach
< 50 ms LAN-like almost anything works
50–100 ms normal online play prediction + interpolation
100–200 ms playable if the netcode is good + lag compensation, careful input delay
> 200 ms sluggish; fighting and rhythm games suffer region-locked matchmaking

Throughout this course we test at 150 ms RTT, ±20 ms jitter, 2% loss. If your game feels fine there, it will feel great for most players. Tools that create these conditions on a real machine are covered in Network Conditioners, Wireshark & Profilers.

Test yourself

Packets are sent every 50 ms with 80 ms ± 30 ms latency. Why can packet #5 arrive before packet #4?

  1. UDP sorts packets by size
  2. Jitter is larger than the 50 ms gap, so a later packet can take a faster path or queue
  3. The server resends #4
  4. This can't happen

Why do netcode systems size buffers from RTT plus a multiple of jitter?

  1. Because RTT is always an overestimate
  2. Because a buffer sized only for the average runs dry whenever a packet is later than average
  3. Because jitter is constant
  4. To save bandwidth