TExcelTAD
Home › Multiplayer Game Networking › Rollback Netcode

Rollback Netcode

Deterministic lockstep waits for everyone's input, so every action feels D ticks late, and a late packet freezes the game. Fighting game players, who count individual frames, found both unacceptable. Rollback netcode (popularized by the GGPO library and now standard in fighting games) keeps the deterministic simulation but never waits:

  1. Apply your own input immediately (or with a tiny delay of 1–2 frames).
  2. Predict the opponent's input for frames you haven't received yet. The usual guess: they're still doing what they did last frame.
  3. Save the game state every frame.
  4. When the opponent's real input arrives and differs from the guess, roll back: restore the state from that frame, apply the real input, and re-simulate all frames up to now, within a single rendered frame.

Most of the time the guess is right (players hold buttons for many frames), so nothing visible happens. When it's wrong, the opponent's character snaps a few frames forward into the corrected pose.

Rollback, tick by tick

Two players, a tiny game where each player's position changes by their input every frame. The remote player's inputs arrive 3 frames late. Local frames are simulated immediately with a predicted remote input:

ts · try it yourself
type State = { a: number; b: number };
const step = (s: State, ia: number, ib: number): State => ({ a: s.a + ia, b: s.b + ib });

const LATENCY = 3;
const localInputs  = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];   // player A (us)
const remoteInputs = [0, 0, 1, 1, 1, -1, -1, 0, 0, 0, 0, 0]; // player B, known LATENCY frames later

const saved: State[] = [{ a: 0, b: 0 }];   // saved[f] = state at the START of frame f
const usedForB: number[] = [];              // remote input we used for each frame (real or guessed)
let confirmed = -1;                         // newest frame with B's real input
let rollbacks = 0, resimulated = 0;

for (let f = 0; f < localInputs.length; f++) {
  // 1. Receive B's input for frame f - LATENCY (if any) and check our guess.
  const r = f - LATENCY;
  let note = '';
  if (r >= 0) {
    confirmed = r;
    if (usedForB[r] !== remoteInputs[r]) {
      // 2. Misprediction: restore frame r and re-simulate r .. f-1 with corrected inputs.
      rollbacks++;
      for (let g = r; g < f; g++) {
        usedForB[g] = g <= confirmed ? remoteInputs[g] : remoteInputs[confirmed];   // re-predict from newest real input
        saved[g + 1] = step(saved[g], localInputs[g], usedForB[g]);
        resimulated++;
      }
      note = `ROLLBACK to ${r}, re-simulated ${f - r} frames`;
    }
  }
  // 3. Simulate this frame with our real input and a guess for B (repeat the newest real input).
  usedForB[f] = confirmed >= 0 ? remoteInputs[confirmed] : 0;
  saved[f + 1] = step(saved[f], localInputs[f], usedForB[f]);
  const s = saved[f + 1];
  console.log(`frame ${String(f).padStart(2)}: guess B=${String(usedForB[f]).padStart(2)}  state a=${s.a} b=${String(s.b).padStart(2)}  ${note}`);
}
const truth = localInputs.reduce((st, ia, i) => step(st, ia, remoteInputs[i]), { a: 0, b: 0 });
console.log(`rollbacks: ${rollbacks}, frames re-simulated: ${resimulated}`);
console.log(`true final state (all inputs known): a=${truth.a} b=${truth.b}`);
frame  0: guess B= 0  state a=1 b= 0
frame  1: guess B= 0  state a=2 b= 0
frame  2: guess B= 0  state a=3 b= 0
frame  3: guess B= 0  state a=3 b= 0
frame  4: guess B= 0  state a=3 b= 0
frame  5: guess B= 1  state a=3 b= 4  ROLLBACK to 2, re-simulated 3 frames
frame  6: guess B= 1  state a=3 b= 5
frame  7: guess B= 1  state a=3 b= 6
frame  8: guess B=-1  state a=3 b=-1  ROLLBACK to 5, re-simulated 3 frames
frame  9: guess B=-1  state a=3 b=-2
frame 10: guess B= 0  state a=3 b= 1  ROLLBACK to 7, re-simulated 3 frames
frame 11: guess B= 0  state a=3 b= 1
rollbacks: 3, frames re-simulated: 9
true final state (all inputs known): a=3 b=1

Look at B's position: at frame 5 it jumps from 0 to 4, because we learn in one go that B had started moving right three frames ago. At frame 10 it snaps from -2 back to 1, because B had stopped while we still predicted it moving left. Those snaps are what rollback looks like to a player. Every misprediction costs re-simulating a few frames, but player A never waits for anything. Once all real inputs are known, the state converges to exactly what lockstep would have produced, because the simulation is deterministic.

What rollback demands

  • Determinism, exactly as in lockstep: same inputs, same state, on every machine.
  • Cheap save and load of the whole game state, every frame. Fighting games keep their state small and in flat structures, so a snapshot is a memory copy.
  • Fast simulation: in the worst case the game re-simulates, say, 8 frames plus the current one within a 16.7 ms frame. A game whose tick costs 4 ms can't roll back 8 frames.
  • Side effects that can be undone: sounds, particles and rumble triggered during a frame that is later rolled back must not play twice, or must be cancellable. Usually only the confirmed frames trigger them, or effects are keyed by frame so a re-simulation recognizes them.
  • A cap: if the opponent's inputs are more than N frames late (say 8), stop and wait rather than predicting further. Rollback degrades into lockstep with stalls.

Input delay plus rollback

Pure rollback (0 frames of delay) mispredicts often on bad connections, causing teleporting opponents. Pure delay feels sluggish. Most games combine them: a small input delay (1–3 frames, sometimes chosen from the ping) covers part of the latency, and rollback covers the rest. Many fighting games let players choose the delay.

Approach Your own input Opponent Bad connection
Lockstep (delay only) always D frames late always correct freezes
Rollback only instant snaps when mispredicted lots of snapping
Delay + rollback 1–3 frames late snaps less degrades gracefully

Beyond fighting games

The same idea (predict, save state, rewind and replay on correction) appears in many places in this course:

  • Client-Side Prediction and Server Reconciliation roll back only the local player to the server's state and replay unacknowledged inputs. That's rollback limited to one entity, with a server as the authority.
  • Some engines' networking layers (Photon Fusion and Unity's Netcode for Entities, for example) re-simulate predicted ticks after each server update (Mirror, FishNet & Photon Fusion).

Test yourself

What does a rollback client do when the opponent's real input differs from its guess?

  1. Freezes until the next input
  2. Restores the saved state from that frame and re-simulates up to the present with the real input
  3. Ignores the real input
  4. Asks the server for a snapshot

What is the usual prediction for an opponent's missing input?

  1. No buttons pressed
  2. A random input
  3. The same input as their last known frame
  4. The input that would hurt us most

Why must sounds and particles be handled carefully under rollback?

  1. They use too much bandwidth
  2. A frame can be simulated several times, so its effects could trigger repeatedly or for events that were rolled back
  3. They aren't deterministic
  4. Audio needs TCP