CMD Guide
HomeSystem DesignAPI Gateway

TCP vs UDP

Both protocols ride on top of IP, which delivers individual packets best-effort with no promises. The difference is what each layer adds on top: TCP stamps every byte with a sequence number and makes the receiver return acknowledgements, so the sender can detect gaps and retransmit — turning an unreliable packet network into an ordered, lossless byte stream. UDP adds almost nothing — just ports and a checksum — and hands each datagram to the application exactly as it arrives, or not at all. Everything else (handshake, ordering, congestion control, head-of-line blocking) follows from that one choice: track-and-retransmit versus fire-and-forget.

How TCP builds a reliable stream

TCP is not magic on the wire — the wire is still lossy IP. Reliability is a bookkeeping protocol layered on top:

None of this exists until a connection is established, which is what the three-way handshake is for.

diagram
diagram

Note the handshake costs a full round trip before any data moves — the client cannot send its request until the third packet. On a link with 50 ms one-way latency, that is 100 ms of pure setup, plus another round trip for TLS on top if this is HTTPS. This is exactly the tax UDP-based protocols try to avoid.

Worked trace: one lost segment, and how TCP recovers

The client sends a 600-byte body split into six 100-byte segments (bytes 1001–1600). Segment 3 (bytes 1201–1300) is dropped by a congested router. Watch how sequence numbers and duplicate ACKs recover it — each duplicate ACK below is generated by a named arriving segment, and note what happens to segments 4–6 that did arrive.

StepSender → (seq, bytes)Receiver has← ACK (next expected)What it means
1seq 1001 (1001–1100)1001–1100ack 1101in order, delivered to app
2seq 1101 (1101–1200)…–1200ack 1201in order, delivered to app
3seq 1201 (1201–1300)LOSTnever arrives
4seq 1301 (1301–1400)buffered, gap at 1201ack 1201 (dup #1)"still need 1201" — held, NOT delivered
5seq 1401 (1401–1500)buffered, gap at 1201ack 1201 (dup #2)"still need 1201" — held, NOT delivered
6seq 1501 (1501–1600)buffered, gap at 1201ack 1201 (dup #3)"still need 1201" — 3rd dup ACK: triggers fast retransmit
7seq 1201 (retransmit)gap filled: 1001–1600 completeack 16011201–1600 all delivered to app at once

The key observation is steps 4–6: segments 4, 5, and 6 arrived intact but the receiver could not hand them to the application, because delivering byte 1301 before byte 1201 would violate ordering. They sat in a kernel buffer until the retransmit of segment 3 landed. That stall is head-of-line (HOL) blocking: one lost packet freezes every byte behind it. UDP has no such rule — a lost datagram is simply gone, and the next one is delivered immediately.

One more thing the trace shows by construction: a duplicate ACK is only generated when an out-of-order segment arrives, so fast retransmit needs at least three segments to land after the lost one. Had segment 3 been among the last segments of the transfer — fewer than three following it — no third dup ACK would ever arrive and the sender would sit waiting for the RTO timer. Tail losses are recovered by timeout, which is why they dominate tail latency, and why mechanisms like RACK-TLP (RFC 8985) exist to probe for them sooner.

What SACK changes

Now rerun the same trace with two losses — segment 3 (seq 1201) and segment 5 (seq 1401). With cumulative ACKs alone, every dup ACK still just says "ack 1201"; after the sender retransmits 1201, the next ACK jumps only to 1401 — revealing the second hole one full RTT later, so recovery costs 2 RTTs (one extra RTT per additional hole). With SACK (Selective Acknowledgment, RFC 2018), each dup ACK also carries a map of what the receiver does hold — e.g. ack 1201, SACK 1301–1400, then ack 1201, SACK 1301–1400, 1501–1600 — so the sender sees both holes immediately and retransmits 1201 and 1401 in the first recovery RTT. SACK is negotiated in the SYN and is on by default in every mainstream OS; the dup-ACK counting above still triggers recovery, but SACK tells the sender exactly which holes to fill.

diagram
diagram

What UDP actually is

A UDP datagram is an 8-byte header (source port, destination port, length, checksum) wrapped around your payload, dropped onto IP, and forgotten. There is no connection state, no sequence number, no ACK, no window, no congestion control. If a datagram is lost, duplicated, or reordered, UDP will not tell you and will not fix it. That sounds like a weakness, and for file transfer it is — but it is precisely what you want when stale data is worthless: in a voice call, a 40 ms audio frame that arrives 300 ms late is useless, so retransmitting it (TCP's instinct) only adds jitter. UDP lets you skip it and play the next frame. Modern UDP applications typically build their own lightweight reliability on top — QUIC, RTP, and game netcode all do — but they get to choose exactly which packets are worth resending, which the kernel's one-size-fits-all TCP cannot.

TCPUDP
Header size20 bytes (min)8 bytes
Connection3-way handshake firstnone — send immediately
Orderingguaranteed (seq numbers)none
Loss recoveryACK + retransmitnone (app's job)
Congestion controlyes (backs off on loss)none
HOL blockingyes — one loss stalls the streamno
Multicast/broadcastno (point-to-point only)yes

Pitfalls

When to use which — and the trade-off

Ask one question first: is a late byte still valuable, or is it garbage?

The modern alternative — QUIC (HTTP/3). QUIC runs on UDP but adds back TCP-grade reliability, encryption, and congestion control in user space, with two decisive wins: connection setup folds the transport and TLS handshakes into a single round trip (0-RTT on resumption), and each stream has independent ordering, so one lost packet stalls only its own stream, not the whole connection. Prefer QUIC over raw TCP when you multiplex many streams over one connection and tail latency matters (web at scale, mobile with frequent network changes); prefer plain TCP when you want kernel-level maturity, simple tooling, and firewalls you don't control; prefer raw UDP only when you need the absolute thinnest layer or multicast and are prepared to own reliability yourself. In short: UDP is the raw material, TCP is the general-purpose reliable stream, QUIC is the tailored reliable stream that fixes TCP's head-of-line and handshake costs.

Takeaways


Re-authored and deepened for this guide. Mechanism, sequence-number/ACK semantics, and fast-retransmit behavior follow RFC 9293 (TCP) and RFC 768 (UDP); congestion-control and loss-recovery details from RFC 5681. Head-of-line blocking and the QUIC/HTTP-3 comparison draw on RFC 9000 (QUIC) and RFC 9114 (HTTP/3). Cross-checked against W. Richard Stevens, TCP/IP Illustrated, Vol. 1, and Kurose & Ross, Computer Networking: A Top-Down Approach.

🤖 Don't fully get this? Learn it with Claude

Stuck on TCP vs UDP? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **TCP vs UDP** (System Design) and want to truly understand it. Explain TCP vs UDP from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **TCP vs UDP** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **TCP vs UDP** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **TCP vs UDP** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes