Netcode
Your character moves on the next frame, even online
Your keypress is played at once. Your friend's is predicted, and if the prediction was wrong the game rewinds and replays the few frames between, faster than one frame takes to draw. Below is the real game engine doing it, at a quarter speed: your screen as Fireboy, while sam_k plays Watergirl somewhere else.
What to watch for
Three moments, in order
f104 to f111: the guess
sam_k's frames past the dashed line are outlined: guesses, "whatever sam_k did last". On your screen Watergirl runs on toward the lava while the ring shows her really jumping.
f112: the correction
The input for f108 arrives: jump, not run. The engine restores the snapshot at f104 and replays 8 frames in 0.32 ms. She is in the air, where she has been all along.
f114: the second one
Releasing jump is also a change, so it is also mispredicted. Every change of input costs one correction; holding a key costs none, which is why the guess is so often right.
Why rollback
Rewinding is cheaper than waiting
Lockstep waits until both players' input has arrived before it draws a frame. Rollback draws yours immediately and fixes the rare frames it guessed wrong.
- 0.04 ms
- to re-simulate one frame, against a 38.5 ms frame budget.
- 4.7 ms
- to capture a snapshot: a hundred times dearer, so there is one, not one per frame.
- 1.4 MB
- of memory for that single snapshot, refreshed every ~8 settled frames, instead of 17 MB.
Time from keypress to movement
Measured in this build, at the game's 26 frames per second.
The wire
No acknowledgements, no retransmission, no ordering
- Three bits per player per frame. Left, right, up. Nothing else crosses the network during play: the world is recomputed on each side from the inputs alone.
- Every packet repeats the last 16 frames. A packet is about 34 bytes and a session about 700 bytes a second, and a lost packet costs nothing because the next one carries the same frames.
- An unreliable, unordered data channel. Retransmission would hold newer input behind older input that no longer matters; head-of-line blocking is the one thing this cannot afford.
- An epoch on every packet. A retry restarts the frame count at zero; without a generation number, in-flight input from the old level would be replayed as real input in the new one.
Connection
Direct when it can be, relayed when it can't
- DirectA WebRTC data channel between the two browsers. Most pairs play here, at no server cost.
- RelayIf a network refuses direct connections, the same channel goes through a TURN relay, with short-lived credentials from a signed-in session.
- FallbackIf neither connects, gameplay continues over the realtime channel that already carries invitations and presence, batched every 230 ms.