The network makes no promises
Data crosses the internet in small pieces called packets. Each packet is passed from router to router, and each router does its best. That is all it does. A packet can be:
- lost, if a router is too busy and drops it,
- late, if it takes a slower route, or
- out of order, arriving after a packet that was sent later.
Nothing below this layer fixes any of that. So programs choose one of two ways to send data, and the choice is about how much of the mess to clean up.
TCP: everything, in order
TCP gives a program a simple promise: the bytes you send are the bytes the other side receives. All of them, in the same order, with nothing repeated.
To keep that promise on an unreliable network, TCP does a lot of work behind the scenes.
It opens a connection first
Both sides exchange a few messages to agree they are ready. This is the handshake.
It numbers every piece
So the receiver knows the right order and can spot a gap.
The receiver acknowledges what arrives
"Got everything up to 2."
Anything not acknowledged is sent again
A lost packet is repaired automatically.
Data is only handed over in order
If packet 3 is missing, packets 4, 5 and 6 wait, however long it takes.
Sent, in this order
Reached the other machine, in this order
Held back, waiting for a gap to be filled
What the program receives
Start. TCP promises the receiving program every byte, in order, with nothing missing. Six packets are about to be sent across a network that keeps no such promise.
- Sent
- 0 / 6
- Sent again
- 0
- Handed to the program
- 0
The program received a perfect copy. Look at what it cost: three packets had arrived and sat there, held back until the missing one was resent. One lost packet delayed everything behind it. This is called head-of-line blocking.
UDP: send it and forget it
UDP does almost nothing. There is no connection, no numbering, no acknowledgements and no resending. A program hands UDP a packet, and UDP sends it. Whatever arrives at the other end is passed straight up.
Sent, in this order
Reached the other machine, in this order
What the program receives
Start. UDP promises nothing. Each packet is sent once, and whatever arrives is handed over as it comes.
- Sent
- 0 / 6
- Sent again
- 0
- Handed to the program
- 0
Packet 3 never came. Packets 5 and 6 swapped places. UDP neither noticed nor cared. But nothing waited either: every packet was handed over the instant it arrived.
Why would anyone want that?
Because sometimes late is worse than lost.
Example
A video call
You are on a call and one packet of sound, 20 milliseconds of it, is lost.
With TCP, the call would freeze until that packet was resent, then play everything that had piled up behind it. You would hear a stutter and fall behind the conversation.
With UDP, there is a tiny blip and the call carries on. By the time a resend could arrive, that moment of the conversation is over. Nobody wants it any more.
For live sound, video and games, an old packet is useless. For a file, a web page or a bank transfer, a single missing byte ruins everything.
| TCP | UDP | |
|---|---|---|
| Connection set up first | Yes | No |
| Lost data is resent | Yes | No |
| Arrives in order | Yes | Not guaranteed |
| Slows down when the network is busy | Yes | No |
| Extra delay | Some, and more after a loss | Almost none |
| Good for | Web pages, files, email, databases | Calls, live video, games, DNS lookups |
Sent, in this order
Reached the other machine, in this order
Held back, waiting for a gap to be filled
What the program receives
Start. TCP promises the receiving program every byte, in order, with nothing missing. Six packets are about to be sent across a network that keeps no such promise.
- Sent
- 0 / 6
- Sent again
- 0
- Handed to the program
- 0
Building on UDP
UDP being bare is also a strength: it is a blank slate. A program can add exactly the guarantees it needs and no others. A game might resend "player fired" messages and never resend positions, since a newer position is coming anyway.
The best-known example is QUIC, the protocol under HTTP/3. It runs over UDP and rebuilds reliability itself, in a way that avoids TCP's problem of one loss blocking everything. See HTTP/1.1, HTTP/2 and HTTP/3.
Key takeaways
- The internet can lose, delay and reorder packets.
- TCP numbers, acknowledges and resends, so the program gets every byte in order.
- The price is delay: one lost packet holds back everything behind it.
- UDP sends packets once and hands over whatever arrives, immediately.
- Use TCP when every byte matters, and UDP when being on time matters more than being complete.