diagramkit

TCP vs UDP: the difference, drawn step by step

TCP and UDP are the two ways most internet traffic gets from one program to another. TCP sets up a connection, numbers every piece of data, and resends anything that goes missing, so the receiver gets every byte in order. UDP sends each message on its own and never checks whether it arrived. TCP trades speed for reliability. UDP trades reliability for speed.

Both sit on top of IP, which moves packets between computers but promises nothing about whether they arrive. TCP and UDP decide what to do about that.

How TCP delivers data

Before any data moves, TCP opens a connection with a three-way handshake. The client sends SYN ("I want to talk"), the server answers SYN-ACK ("me too"), and the client sends ACK ("let's go"). That costs one round trip before the first byte of data.

Then TCP numbers the data it sends, and the receiver acknowledges what it gets. If an acknowledgment doesn't come back in time, the sender resends that piece. The receiver holds back later pieces until the missing one arrives, so the application always reads the data in the order it was sent (RFC 9293).

How UDP delivers data

UDP has no handshake and no acknowledgments. The program hands UDP a message, UDP adds a small header and sends it. If the message is lost, nothing resends it. If two messages arrive out of order, the program sees them out of order (RFC 768).

TCP's handshake, acknowledgments and resend next to UDP's send-and-forget, with one lost packet each

In the figure, the same packet goes missing on both sides. TCP notices the missing acknowledgment and sends it again, and the app gets 1, 2, 3. UDP never knows, and the app gets 1 and 3.

Why a TCP header is bigger than a UDP header

Every packet carries a header with the information the protocol needs. UDP needs very little: which port it came from, which port it is going to, its length and a checksum. That is 8 bytes.

TCP needs fields for everything it promises. A sequence number says where this data sits in the stream. An acknowledgment number says what has been received. Flags such as SYN, ACK and FIN open and close the connection. A window size tells the sender how much more the receiver can take. That makes at least 20 bytes.

The UDP header's 8 bytes next to the TCP header's 20, with the reliability fields highlighted

Which apps use TCP and which use UDP

Use TCP when every byte matters. A web page with a missing chunk is broken, and a file with a missing byte is corrupt. Web pages over HTTP/1.1 and HTTP/2, email, SSH and file downloads all run on TCP.

Use UDP when late data is worse than lost data. In a video call, a frame that arrives half a second late is useless, so it is better to skip it and show the next one. Video calls, online games, live streams and DNS lookups run on UDP. A DNS lookup is one small question and one small answer, so a handshake would double the time it takes.

Everyday apps grouped by whether they run on TCP or UDP, with QUIC on the UDP side

QUIC: reliability built on top of UDP

The line is less clean than it used to be. QUIC is a newer protocol that runs on UDP and does its own reliability and encryption on top (RFC 9000). HTTP/3, the latest version of the protocol web pages load over, runs on QUIC (RFC 9114).

Building on UDP lets QUIC fix two TCP problems. It combines the connection and encryption setup into fewer round trips. And it keeps separate streams independent, so one lost packet only holds up the stream it belongs to, not everything on the connection.

TCP vs UDP at a glance

TCP UDP
Connection Three-way handshake first None
Delivery Every byte, resent if lost Best effort, never resent
Order Always in order Whatever order they arrive
Header 20 bytes minimum 8 bytes
Used for Web pages, email, SSH, file downloads Video calls, games, live streams, DNS, QUIC

FAQ

Is UDP faster than TCP?

UDP starts sooner, because it skips the handshake, and it never waits for a lost packet to be resent. On a good network with no losses, the difference in raw throughput is small. The gap shows on short exchanges and on lossy networks.

Why does DNS use UDP?

A DNS lookup is one short question and one short answer. Over UDP that is one round trip. Over TCP it would be two, one for the handshake and one for the lookup. DNS falls back to TCP when an answer is too large for one UDP message.

Does HTTP/3 use UDP?

Yes. HTTP/3 runs on QUIC, and QUIC runs on UDP. QUIC adds the reliability that UDP lacks, so web pages still arrive complete.

Filed under How software works