TCP vs UDP
The transport layer's two main protocols: TCP chooses reliability, UDP chooses speed. Knowing which application uses which is half of reading network traffic.
01Core differences
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | No guarantee |
| Ordering | In-order delivery | Unordered |
| Speed | Slower | Fast |
| Overhead | High | Low |
02TCP — like a phone call
First a connection is established, you confirm the other side is listening, then you talk.
Its features
- 3-way handshake — connection setup
- Sequence numbers — pieces are put back in order
- Acknowledgments — every packet is confirmed
- Retransmission — lost packets are sent again
- Flow control — slows down to the receiver's pace
TCP connection setup
code
Client Server
|--- SYN --->|
|<-- SYN+ACK-|
|--- ACK --->|
[Connection established]
TCP data transfer
code
Data 1 →→→ ACK 1
Data 2 →→→ ACK 2
Data 3 →→→ LOST!
Data 3 →→→ ACK 3 (noticed, retransmitted)
03UDP — like a letter
Drop the envelope in the box and move on. Did it arrive? In order? UDP doesn't care.
code
Data 1 →→→ arrived
Data 2 →→→ arrived
Data 3 →→→ lost (nobody noticed)
Data 4 →→→ arrived
That indifference isn't a flaw — it's a deliberate trade: a protocol that never waits for confirmation keeps latency at a minimum.
04When to use which?
Use TCP — when every byte matters
- Websites (HTTP/HTTPS)
- Email (SMTP, IMAP)
- File transfer (FTP)
- SSH connections
Use UDP — when speed is everything
- Online games
- Video streaming and live broadcasts
- VoIP (voice calls)
- DNS queries
- DHCP
An online game uses both at once: your character's position flows over UDP (a lost frame is corrected by the next one), while a purchase goes over TCP (it absolutely must arrive).
05Try it yourself
See what latency and packet loss actually look like by measuring them:
Sandbox · CommandPing & TracerouteCompare latency, packet loss, and hop counts across different destinations.Open tool06Summary
- TCP = reliable but costly: web, email, files
- UDP = fast but no guarantees: games, video, DNS
- One application can use both at the same time
- HTTP/3 (QUIC) builds its own reliability on top of UDP