ComparisonDifficulty: Intermediate2 min

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?

TCP and UDP compared on the properties that decide between them
PropertyTCPUDP
Setup before dataA handshake, one round tripNone; the first datagram carries data
Lost dataSent again automaticallyLost, unless the application resends it
OrderAlways in orderAs it arrives
Unit deliveredA stream of bytesSeparate messages, boundaries kept
When one packet is lostEverything behind it waitsOnly that packet is missing
Header size20 bytes or more8 bytes
Typical usesWeb pages, APIs, files, databases, emailDNS, 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.