TCP vs UDP for Games
Almost every fast-paced online game sends its gameplay traffic over UDP, while web pages, chat and file downloads use TCP. That is not because UDP is "faster" on the wire: both ride the same IP packets. It is because of what TCP does when a packet is lost. (The protocol details themselves are covered in Computer Networking; here we look at them from a game's point of view.)
What each one promises
| TCP | UDP | |
|---|---|---|
| Connection | handshake first, then a byte stream | no connection, separate datagrams |
| Delivery | every byte, eventually | maybe; each datagram arrives once, or not at all |
| Order | always in order | any order |
| Message boundaries | none: you get a stream, you split it yourself | kept: one send is one datagram |
| Congestion control | built in, slows down on loss | none, the app decides |
| Typical game use | login, lobby, chat, store, turn-based games, downloads | movement, shooting, snapshots, voice |
Head-of-line blocking
TCP guarantees order. If segment 5 is lost, segments 6, 7 and 8 may already have arrived, but the operating system holds them back from your game until segment 5 is retransmitted, at least one round trip later. For a file, that is exactly right. For a game sending the player's position 30 times a second, it is terrible: positions 6, 7 and 8 are newer and more useful than position 5, yet the game can't read any of them.
Here is a model: a server sends one position update every 33 ms over a link with 50 ms one-way latency, and update 3 is lost. With UDP the game simply skips it. With TCP the retransmission is sent after a timeout (here 200 ms, a typical minimum) and everything behind it waits:
const SEND_EVERY = 33, ONE_WAY = 50, RTO = 200;
const LOST = 3; // the first copy of update 3 is dropped
const sent = Array.from({ length: 8 }, (_, i) => ({ id: i, sentAt: i * SEND_EVERY }));
// UDP: each datagram is delivered on its own; the lost one never arrives.
const udp = sent.filter((p) => p.id !== LOST).map((p) => ({ id: p.id, readAt: p.sentAt + ONE_WAY }));
// TCP: the lost segment is resent after RTO; later ones arrive but are held until it is delivered.
const arrive = sent.map((p) => p.sentAt + ONE_WAY + (p.id === LOST ? RTO : 0));
const tcp: { id: number; readAt: number }[] = [];
let ready = 0;
for (const p of sent) {
ready = Math.max(ready, arrive[p.id]); // in-order delivery: never before the previous one
tcp.push({ id: p.id, readAt: ready });
}
console.log('update sent UDP read TCP read TCP age when read');
for (const p of sent) {
const u = udp.find((x) => x.id === p.id);
const t = tcp[p.id];
console.log(` ${p.id} ${String(p.sentAt).padStart(4)} ${u ? String(u.readAt).padStart(5) : ' lost'} ${String(t.readAt).padStart(5)} ${String(t.readAt - p.sentAt).padStart(4)} ms`);
}update sent UDP read TCP read TCP age when read
0 0 50 50 50 ms
1 33 83 83 50 ms
2 66 116 116 50 ms
3 99 lost 349 250 ms
4 132 182 349 217 ms
5 165 215 349 184 ms
6 198 248 349 151 ms
7 231 281 349 118 ms
With TCP, update 3 shows up 250 ms after it was sent, and updates 4–7 queue behind it and are released in one burst. The player sees the world freeze, then jump. With UDP the game reads updates 4 to 7 on time and never notices update 3 was missing, because update 4 replaces it anyway.
Disabling Nagle's algorithm (TCP_NODELAY) removes TCP's small-packet batching delay, which helps, but it does nothing for head-of-line blocking.
Most game data doesn't need reliability
Ask of every message: if it's lost, does the next message make it useless anyway?
| Data | Lost packet... | Needs |
|---|---|---|
| Player positions, snapshots | replaced by the next one 33 ms later | unreliable, newest wins |
| Player inputs | the server needs them all, but resending them in every packet is cheaper than waiting | redundant (send the last N inputs each time) |
| "Player 3 joined", "Round over", chat | must arrive, order matters | reliable, ordered |
| "Explosion at (x, y)" | must arrive, order rarely matters | reliable, unordered |
| Voice | late audio is useless | unreliable, sequenced |
So games build a thin layer on UDP that offers channels with different guarantees, which is exactly what game networking libraries do (we build one in Reliability over UDP):
Unity Transport / NGO: reliable, unreliable, unreliable-sequenced pipelines
Unreal: reliable and unreliable RPCs, unreliable property replication
Godot @rpc: "reliable", "unreliable", "unreliable_ordered" transfer modes
ENet, yojimbo, GameNetworkingSockets, LiteNetLib: reliable/unreliable channels over UDPWhen TCP (or WebSocket) is fine
- Turn-based and slow games: chess, card games, most strategy and idle games. A stall of 300 ms once in a while is invisible.
- Everything outside the match: login, matchmaking requests, store, leaderboards, chat services. These use HTTPS and WebSocket like any web app.
- Browser games: a web page can't open a raw UDP socket. Its options are WebSocket (TCP), WebRTC data channels (which can be unreliable and unordered) and WebTransport. See WebSocket, WebRTC & Colyseus.
Note: QUIC (the transport under HTTP/3) runs over UDP and has independent streams, so one lost packet only blocks its own stream, and it also supports unreliable datagrams. WebTransport builds on it. That's the direction the web is heading for real-time games.
Don't use TCP and UDP side by side for the same match
A classic mistake is "reliable stuff over TCP, fast stuff over UDP" on the same connection to the game server. When TCP sees loss it backs off and its retransmissions compete with your UDP traffic on the same congested link, making the UDP side worse exactly when you need it. Put reliable messages on a reliable channel of your UDP protocol instead.
Test yourself
Why is TCP a poor fit for streaming player positions in a shooter?
- TCP packets are larger than UDP packets on the wire
- A lost segment blocks delivery of all newer data until it is retransmitted
- TCP can't send more than 10 packets per second
- TCP doesn't support binary data
A position update is lost. With an unreliable channel, what is the usual right reaction?
- Resend it immediately
- Pause the game until it arrives
- Ignore it: the next update replaces it
- Disconnect the client
Which of these should be sent reliably?
- The player's position every tick
- Voice packets
- 'Round over, team blue wins'
- The aim direction every tick