Why is TCP Reliable but UDP Isn't?
π― The Questionβ
"Both TCP and UDP run on top of IP (which is inherently unreliable and lossy). How does TCP guarantee 100% reliable, in-order delivery while UDP provides no guarantees?"
β‘ 30-Second Elevator Pitchβ
The underlying IP layer only provides Best-Effort Delivery (packets can be dropped, delayed, duplicated, or reordered).
TCP achieves reliability through 4 core mechanisms:
- Sequence Numbers & ACKs: Every byte is numbered; the receiver explicitly confirms received bytes.
- Retransmission Timers (RTO): If an ACK is not received before timeout, TCP automatically resends the lost segment.
- Flow & Congestion Control: TCP throttles transmission to match receiver buffer capacity and network congestion.
UDP does none of this: It simply wraps payload data with port numbers and a checksum, transmitting raw datagrams without tracking, retransmissions, or connections.
π§ Under-the-Hood: Reliability Mechanismsβ
π¬ Core Pillars of TCP Reliabilityβ
- Cumulative Acknowledgments:
- Receiver sends
ACK N, confirming it has received all bytes up to .
- Receiver sends
- Fast Retransmit:
- If the sender receives 3 duplicate ACKs for the same sequence number, it retransmits the missing segment immediately without waiting for RTO timeout.
- Reordering Buffer:
- Out-of-order packets are held in receiver memory buffers until missing segments arrive, ensuring the application receives a continuous stream.
π Comparison Matrix: TCP vs. UDPβ
| Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
|---|---|---|
| Connection Type | Connection-oriented (3-Way Handshake) | Connectionless (No handshake) |
| Reliability | 100% Guaranteed (No loss, no duplicates) | Best effort (Packets may drop/duplicate) |
| Ordering | Strict sequential ordering preserved | Independent datagrams (May arrive out of order) |
| Header Size | 20β60 bytes (Large stateful header) | 8 bytes (Minimal stateless header) |
| Speed / Overhead | Higher latency (ACK round trips & backoff) | Blazing fast (Fire and forget) |
| Primary Use Cases | Web (HTTP/HTTPS), File Transfer (FTP), DBs | DNS, Live Video Streaming, Online Gaming, VoIP |
π‘ What Interviewers Ask Next (Follow-Up Traps)β
-
"Does UDP have any error checking at all?"
- Answer: Yes. UDP contains a 16-bit Checksum field in its header to detect corrupted bits in the packet payload. If corruption is detected, the OS quietly drops the packet, but UDP provides no mechanism to request retransmission.
-
"Why does HTTP/3 (QUIC) use UDP as its underlying transport instead of TCP?"
- Answer: TCP suffers from Head-of-Line (HoL) Blocking at the transport layerβif a single packet is lost, all multiplexed HTTP streams stall until retransmitted. HTTP/3 builds independent stream reliability on top of UDP in user space to eliminate HoL blocking and achieve 0-RTT handshakes.
Interview Answer: IP is unreliable by design. TCP builds reliability in software using Sequence Numbers, ACKs, Retransmission Timers, and Congestion Control algorithms. UDP omits these overheads, providing raw speed and lower latency for applications that prioritize real-time delivery over 100% packet arrival.