Course contents · Lesson 5 of 22

TCP vs UDP: Registered Post or Live Radio?

How the Internet Works · Lesson 5 of 22 · 8 min Foundations

On a video call, a word sometimes goes missing and the call simply carries on. A download never arrives with a page missing: it completes or fails. Both cross the same internet as packets, under different delivery rules. This lesson explains TCP vs UDP: what each one promises, and how to tell which one a job needs.

Two delivery rules on top of IP

As lesson 1 showed, the network promises nothing: packets can be lost, duplicated or reordered. Behind each packet's IP (Internet Protocol) header sits a second header, where lesson 2 put the ports. It belongs to a transport protocol, an agreed set of rules for carrying data between two programs: almost always TCP (Transmission Control Protocol) or UDP (User Datagram Protocol).

TCP promises that every byte arrives, once and in order, or the program hears that the connection failed. UDP adds little beyond the ports. In the diagram, each TCP row is a kind of checking; each UDP row is its absence.

Two panels side by side. Left, registered post, meaning receipt, order and resend, maps to TCP: the recipient signs for each letter, so an ACK confirms data arrived; letters carry page numbers, so sequence numbers fix the order; no receipt means post that letter again, so lost segments are resent; you wait for receipts as you go, so TCP is reliable but adds delay. Right, live radio commentary, meaning broadcast and no receipt, maps to UDP: it broadcasts without checking who listens, so there is no handshake and no connection; listeners never confirm they heard, so there are no acknowledgements; a missed word stays missed, so lost datagrams are not resent; it is always about the ball just bowled, so UDP carries fresh data with low delay. A note says that unlike radio, UDP usually has one receiver and its data may arrive out of order
TCP checks and repeats; UDP just keeps talking.

The analogy has limits: radio reaches every listener, while most UDP traffic goes to one receiver, and UDP messages can arrive out of order or twice.

How TCP makes delivery reliable

The three-way handshake

Before any data moves, TCP sets up a connection with three messages, the three-way handshake, in which each side says where its byte count starts. The client sends a SYN (synchronise): "let's talk, my count starts here". The server replies with a SYN-ACK, a synchronise plus an acknowledgement: "got it, here's my count". The client answers with an ACK (acknowledgement): "got yours too".

That costs one round trip, the time for a message to go and its reply to return, before any data flows. For a server 10,000 km away, that is at least a tenth of a second (lesson 1).

Numbered bytes, ACKs and resends

TCP cuts data into pieces called segments and numbers every byte. A segment's sequence number is the number of its first byte: 1, 1001 and 2001 in the flowchart. Real counts start from a random-looking number, not 1.

Each ACK names the next byte the receiver expects: "ACK 1001" means "I have everything up to byte 1,000". The sender keeps a copy of all data until it is acknowledged.

When a segment is lost, later ones still arrive, but the ACKs keep repeating the missing number. The sender resends when a timer runs out, or sooner when several repeated ACKs point at the gap. Only then does the receiver hand the held-back bytes to the program, in order. In the flowchart, the ACK stays at 1001 until the resend, then jumps to 3001.

Flowchart with the client on the left and the server on the right, time running downwards. Part 1, the three-way handshake: step 1, the client sends SYN, let's talk, my count starts here; step 2, the server replies SYN-ACK, got it, here's my count; step 3, the client sends ACK, got yours too. Part 2, data with acknowledgements: step 4, the client sends bytes 1 to 1000; step 5, the server replies ACK 1001, send byte 1001 next; step 6, bytes 1001 to 2000 are lost on the way; step 7, bytes 2001 to 3000 arrive but are held, waiting for the gap; step 8, the server replies ACK 1001 again, still missing 1001; step 9, because a timer ran out or ACKs repeated, the client resends bytes 1001 to 2000; step 10, the server replies ACK 3001, all 3,000 bytes arrived, and the app gets them all in order. Byte numbers are illustrative, and the picture is simplified to one ACK per segment
Lost bytes are never skipped: later bytes wait for the resend.

How UDP keeps it small and fast

UDP has no connection and no handshake. A program sends a datagram, one self-contained message, to an IP address and port, so the first message already carries data.

The UDP header is 8 bytes: the two ports, the length and a checksum, a small sum that lets the receiver spot a damaged datagram and discard it. A TCP header is at least 20 bytes.

There are no acknowledgements, resends or reordering: a lost datagram is gone, and the sender is never told. Apps add only the checking they need. When a DNS (Domain Name System) lookup gets no reply, your device simply asks again.

Which apps use which

Jobs where every byte must arrive use TCP: web pages over HTTP/1.1 and HTTP/2 (the Hypertext Transfer Protocol, lesson 6), email, file transfers and SSH (Secure Shell) logins. One missing byte would break the page, message or file.

Jobs where fresh data beats complete data use UDP. A DNS query is one small question and answer, so a handshake would double the wait; an answer too big for one datagram is fetched again over TCP. Video calls send voice and video over UDP when the network allows, and fast games send positions many times a second, so the next update replaces a lost one.

Live streams vary: many sites send chunks over ordinary web connections, a few seconds behind; live video that must keep up with real time uses UDP.

HTTP/3, the newest web version, runs on QUIC (a name, not an abbreviation), which is built on UDP and adds its own numbering, acknowledgements and resends, so pages arrive complete. It uses UDP because routers and firewalls (traffic filters) already let UDP through, while many would block a new protocol. It also sets up encryption, scrambling data so only the other side can read it (lesson 8), in the same round trip.

In the table, notice that QUIC is reliable although UDP is not: reliability is work a protocol adds.

Table with five columns: protocol, reliable?, ordered?, speed and delay, and typical uses. TCP, on IP: reliable yes, with ACKs and resends; ordered yes, by sequence number; handshake first, and a loss holds up all later data; used for the web over HTTP/1.1 and HTTP/2, email, file transfer and SSH logins. UDP, on IP: reliable no, lost data stays lost; ordered no, the app must cope; no setup, and nothing waits for lost data; used for DNS queries, video calls, games and low-delay live video. QUIC, on UDP: reliable yes, with its own ACKs and resends; ordered yes, within each stream; setup and encryption in one round trip, and a loss stalls only one stream; used for HTTP/3, the newest version of the web. A note says speed here means delay, not a faster link
TCP builds reliability on IP; QUIC builds it on UDP.

TCP vs UDP: reliability or low delay

Reliability costs time. TCP delivers bytes only in order, so one lost segment holds up everything behind it for at least a round trip: this is head-of-line blocking. A file can wait. In a live conversation, a word arriving half a second late is worse than a skipped one.

UDP never waits; the app copes with gaps, smoothing over lost sound or drawing the newest position. QUIC sits between: it carries separate streams, such as one per file, so a loss holds up only its own stream.

To choose, ask two questions. Must every byte arrive? A web page, a payment or a photo upload breaks with a hole, so use TCP, or QUIC. Is old data useless? A player's position or a word from a second ago is already stale, so use UDP. One app can use both: a game might send chat over TCP and positions over UDP.

Check yourself

A TCP receiver holds bytes 1 to 1,000 and 2,001 to 3,000, but not 1,001 to 2,000. What does its next ACK say?

ACK 1001. An ACK names the next byte needed in order, so it keeps asking for 1001 until the resend fills the gap, then jumps to 3001.

A smart doorbell streams a live picture and also uploads recorded clips. Which protocol suits each?

UDP for the live picture, because a late frame is useless. TCP for the clips, because a recording with missing bytes may not play.

HTTP/3 travels over UDP, which never resends. Why don't web pages arrive with holes?

QUIC, between HTTP/3 and UDP, numbers, acknowledges and resends the data itself, so every byte still arrives.