Peer-to-Peer vs Client-Server
Before any netcode, decide who talks to whom and who decides what is true. That choice, the network topology, shapes your cheating story, your server bill, your latency and how hard reconnects and host leaving will be.
The options
| Topology | Who simulates | Who is the authority | Seen in |
|---|---|---|---|
| Peer-to-peer (mesh) | every peer | every peer for its own stuff, or all of them together (lockstep) | fighting games, classic RTS, small co-op |
| Listen server (player-hosted) | one player's machine is also the server | the host | co-op and casual games, many console games |
| Dedicated server | a server machine with no player | the server | competitive shooters, MMOs, battle royales |
| Relay | (any of the above) | (unchanged) | packets go through a relay only to get past NAT or hide IPs |
Peer-to-peer mesh Client-server (listen or dedicated)
A ─── B A B
│ ╲ ╱ │ ╲ ╱
│ ╳ │ Server
│ ╱ ╲ │ ╱ ╲
C ─── D C D
n·(n−1)/2 links n linksBandwidth and latency
In a mesh every peer sends its updates to every other peer, so each one's upload grows with the player count. In client-server, a client uploads once; the server pays for fan-out. Latency differs too: in a mesh, A sees B after one hop (A→B); through a server it is two hops (A→server→B), but the server can be in a well-connected data centre while two home connections may be far apart or blocked by NAT.
using System;
int inputBytesPerSec = 3_000; // what one player's updates cost per receiver
Console.WriteLine("players | mesh: upload per peer | client-server: client upload | server download");
foreach (int n in new[] { 2, 4, 8, 16, 64 })
{
int meshUp = (n - 1) * inputBytesPerSec;
int csUp = inputBytesPerSec;
int serverDown = n * inputBytesPerSec;
Console.WriteLine(quot;{n,7} | {meshUp / 1000.0,16:F0} kB/s | {csUp / 1000.0,23:F0} kB/s | {serverDown / 1000.0,10:F0} kB/s");
}players | mesh: upload per peer | client-server: client upload | server download
2 | 3 kB/s | 3 kB/s | 6 kB/s
4 | 9 kB/s | 3 kB/s | 12 kB/s
8 | 21 kB/s | 3 kB/s | 24 kB/s
16 | 45 kB/s | 3 kB/s | 48 kB/s
64 | 189 kB/s | 3 kB/s | 192 kB/s
A mesh is great for 2 players and fine for 4–8; beyond that, home uploads give up. That is one reason peer-to-peer survives mostly in fighting games (2 players) and small RTS matches.
NAT traversal
Most players sit behind a home router doing NAT (Computer Networking covers NAT itself). A peer can't simply accept a packet from someone it hasn't contacted first. Peer-to-peer games therefore need:
- A rendezvous server that tells each peer the other's public address (as seen from outside).
- Hole punching: both peers send to each other at roughly the same time, so each router sees outgoing traffic and lets the replies in. This works for most home routers but not all (symmetric NATs, carrier-grade NAT on mobile networks).
- A relay fallback for the rest: packets go through a server, which adds latency and costs bandwidth, but always works.
WebRTC packages exactly this as ICE with STUN (address discovery) and TURN (relay) servers. Steam Datagram Relay, Epic Online Services P2P and Unity Relay are relay services for native games (Steamworks, EOS, Nakama & PlayFab). Relays also hide players' IP addresses from each other, which protects streamers and competitive players from denial-of-service attacks.
Trust
| Peer-to-peer | Listen server | Dedicated server | |
|---|---|---|---|
| Who can cheat by changing game state | anyone, for their own stuff (lockstep: detectable by desyncs) | the host, completely | nobody, if the server validates |
| Host advantage | none | host has 0 ms ping | none |
| Host quits | fine (lockstep) or migrate | match ends unless host migration works | not an issue |
| Cost to you | rendezvous + relay | rendezvous + relay | servers for every match |
Host migration (electing a new host and moving the authoritative state to it) is notoriously hard to get right. If your game is competitive or has an economy, the answer is almost always a dedicated server with an authoritative simulation, which is the model the rest of this course assumes (The Authoritative Server).
A decision guide
- 2-player fighting game, low budget: peer-to-peer with rollback (Rollback Netcode) and a relay fallback.
- RTS with hundreds of units: deterministic lockstep, peer-to-peer or through a relay server (Deterministic Lockstep).
- 4-player co-op: listen server with relay; cheating only hurts your friends.
- Competitive shooter, ranked modes, anything with trading or real money: dedicated authoritative servers.
- Browser game: client-server over WebSocket or WebRTC to your server (WebSocket, WebRTC & Colyseus).
Test yourself
In an 8-player full-mesh game, how many other peers must each peer send its updates to?
- 1
- 7
- 8
- 28
What is a relay server for in a peer-to-peer game?
- Running the authoritative simulation
- Forwarding packets when NAT traversal fails, and hiding players' IP addresses
- Matchmaking by skill
- Storing save games
Which topology gives the host player a latency advantage and full power to cheat?
- Dedicated server
- Listen server
- Relay
- Lockstep peer-to-peer