Card 05/ 09
TCP vs UDP
Choose by what the application does when a packet is late: wait for it, or carry on without it.
TCP and UDP trade reliability against delay. TCP waits for every lost byte and delivers everything in order; UDP delivers what arrives, as it arrives, and lets the application decide what a missing piece means. The choice comes down to one question: when a packet is late, is the application better off waiting for it or carrying on without it?
| Property | TCP | UDP |
|---|---|---|
| Setup before data | A handshake, one round trip | None; the first datagram carries data |
| Lost data | Sent again automatically | Lost, unless the application resends it |
| Order | Always in order | As it arrives |
| Unit delivered | A stream of bytes | Separate messages, boundaries kept |
| When one packet is lost | Everything behind it waits | Only that packet is missing |
| Header size | 20 bytes or more | 8 bytes |
| Typical uses | Web pages, APIs, files, databases, email | DNS, voice and video calls, games, live streams |
When TCP wins. Every byte matters and a short wait is acceptable. A web page with a missing piece of HTML, a file with a gap or a database query with half a result is wrong, not degraded. TCP also handles the hard parts, retransmission, ordering and pacing, so the application does not have to.
When UDP wins. Fresh data replaces old, so waiting for an old packet is worse than skipping it. In a call, a sound sample that arrives 300 milliseconds late is useless; playing on with a tiny gap is better than freezing. UDP also wins when each exchange is a single small question, as in DNS, where a handshake would double the time.
The two are not the only options. An application that needs reliability without TCP's head-of-line blocking can build it on UDP: QUIC does exactly that, with many independent streams inside one connection, so a loss in one stream does not stall the others. That design is why HTTP/3 runs on UDP.
The rule for choosing. Ask what a late packet is worth. If the answer is "everything, wait for it", use TCP. If the answer is "nothing once newer data exists", use UDP, or a protocol built on it.
← → move through sections and cards