# Source Multiplayer Networking
Source: https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking
Mirror used: https://web.archive.org/web/20200910022723/developer.valvesoftware.com/wiki/Source_Multiplayer_Networking
Author: Valve Developer Community

## Architecture
Client–server. Dedicated server authoritative over simulation, rules, input processing. Clients send input samples; receive world snapshots (~20–30 pps typical). Clients do not talk P2P. Issues: limited bandwidth, latency (client behind server), jitter, packet loss. Server: compression + lag compensation. Client: prediction + interpolation.

## Ticks
Server simulates discrete **ticks** (default timestep 15ms ≈ 66.67 Hz). Each tick: process usercmds, physics, rules, update entities; maybe snapshot. Higher tickrate → precision + CPU/bandwidth. `-tickrate` override not recommended for many titles (CSS/DoD/TF2 fixed 66; L4D/L4D2 fixed 30).

## Rates
Client `rate` (bytes/s) declares inbound capacity — most important client net var. `cl_updaterate` (default 20) requests snapshot rate; server never exceeds tickrate or client rate. Server clamps: `sv_minrate`/`sv_maxrate`, `sv_minupdaterate`/`sv_maxupdaterate`. Client builds usercmds at tickrate but sends at `cl_cmdrate` (often 30), packing multiple cmds per packet.

## Delta compression
Server sends changes since last **acked** update. Full snapshots on join or heavy loss (`cl_fullupdate`).

## Entity interpolation
Default ~20 snapshots/s. Render in the past so always interpolate between two snapshots. Default lerp 100ms (`cl_interp 0.1`) tolerates one lost snapshot. Formula: `interpolation period = max(cl_interp, cl_interp_ratio / cl_updaterate)`. If history exhausted → linear extrapolate up to `cl_extrapolate_amount` (0.25s). Interp lag is known to server lag compensation — do not disable.

## Input prediction
Client runs same movement code as server (`cl_predict`). On server snapshot: compare predicted vs authoritative; correct errors; optionally smooth (`cl_smooth`, `cl_smoothtime`). Only local player (and entities solely affected by them) can be predicted.

## Lag compensation
History of player positions (~1s). On usercmd:
`Command Execution Time = Current Server Time - Packet Latency - Client View Interpolation`
Rewind **other players only** to that time; execute cmd (hit detection); restore. Client trust of "I hit" messages rejected (cheat / MITM). Paradox: can be hit "around a corner" from low-ping victim POV. Hit detection not pixel-perfect (tickrate + speed).

## Net graph / tips
`net_graph` shows loss, choke, lerp, rates. Prefer default settings; only tune `rate` for bandwidth. Do not turn off interpolation or lag compensation expecting better aim. Higher tickrate (e.g. 100) ~1.5× CPU vs 66 — Valve suggests not exceeding 66 for headroom.