Topic 025 / Core
TCP and UDP
Chapter 3.1 · Network Foundations
What and why
IP (→ 24) only promises best-effort delivery of individual packets to a host — nothing about order, reliability, or which process on that host should receive the data. TCP is a connection-oriented transport that adds reliable, ordered delivery and congestion control between two processes identified by port numbers; UDP is a connectionless transport that adds only port numbers, with no ordering or delivery guarantee and far less overhead. Builds on: → 24.
Diagram
TCP: three-way handshake, then a reliable ordered stream
A ──SYN────────► B
A ◄───SYN-ACK──── B
A ──ACK────────► B
A ◄══ data, acked, retransmitted on loss, in order ══► B
UDP: no handshake, no guarantee
A ──datagram──► B (delivered, reordered, or lost — A is never told which)
How it works
- TCP opens with a three-way handshake (SYN, SYN-ACK, ACK) before any application data flows, establishing sequence numbers both sides will use to track order.
- Every TCP byte is acknowledged; unacknowledged data is retransmitted, and out-of-order segments are reassembled before being handed to the application.
- TCP's congestion control slows the sending rate when loss is detected, trading throughput to avoid making a congested path worse.
- UDP sends each datagram independently with no handshake, no acknowledgment, and no reordering — a lost datagram is simply gone unless the application layer handles that itself.
Practical example
(illustrative). A REST API call (→ 28) needs every byte to arrive, in order, exactly once — TCP. A live video call tolerates an occasional dropped frame far better than it tolerates the delay TCP's retransmission-and-reordering would add — UDP, with the application deciding what to do about loss itself.
When to use it
- Use TCP when: correctness and order matter more than the last bit of latency — APIs, file transfer, anything where a missing byte must be recovered rather than skipped.
- Use UDP when: low, consistent latency matters more than completeness, and the application can tolerate or paper over loss — live media, DNS queries (→ 26), and as the substrate HTTP/3 builds its own reliability on top of (→ 28).
- Failure scenarios: TCP's reliable-ordered guarantee becomes a liability under packet loss — a single lost segment stalls everything behind it until it's retransmitted (the head-of-line blocking that resurfaces in → 28's HTTP/2 discussion); UDP silently losing datagrams if the application never checks.
- Simpler alternative: default to TCP unless you have a measured reason not to — most application traffic should not be reinventing reliability on top of UDP.
Practice
(mechanism kind — predict-then-verify; 20–30 min, no lab services). Predict
what happens to a large file transfer over a lossy link if sent via TCP versus UDP with no
application-level retry. Then verify: use a loopback tool (e.g. nc/netcat for TCP and
UDP modes, or a network-emulation tool if available) to send the same payload both ways
and observe what, if anything, arrives incomplete or out of order on the UDP side. Done
when your predicted TCP/UDP difference matches what you observed.
Key takeaway
TCP trades latency for guaranteed, ordered delivery; UDP trades guarantees for minimal overhead and lets the application decide what to do about loss. Done when you can: pick the right transport for a workload and name the specific guarantee you're trading away. Used next in → 27 (TLS wraps a TCP — or QUIC/UDP — connection), → 28 (HTTP's versions differ in which transport they ride on).
References
None — this topic makes no external claims.
Source directory