TCP vs UDP
The Responsibility of the Transport Layer
The Transport Layer provides end-to-end (process-to-process) communication services. While the Network Layer only delivers packets to a destination host, the Transport Layer uses Port Numbers to deliver that data to the specific application process.
Core Differences
| Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
|---|---|---|
| Connection | Connection-oriented (Handshake) | Connectionless (Fire and forget) |
| Reliability | Reliable (ACKs, Retransmission, Order) | Best-effort (Unreliable) |
| Ordering | Guaranteed ordered delivery | No ordering guarantees |
| Transmission | Byte Stream (Continuous data) | Datagram (Preserves message boundaries) |
| Flow Control | Yes (Sliding Window) | No |
| Congestion Control | Yes | No |
| Header Size | 20~60 Bytes | 8 Bytes |
| Efficiency | Lower (Trade-off for reliability) | Maximum efficiency |
| Use Cases | HTTP, File Transfers, Emails, DBs | Video streaming, Gaming, DNS, VoIP |
TCP Segment Structure
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
├─────────────────────┼─────────────────────┤
│ Source Port │ Destination Port │
├─────────────────────┴─────────────────────┤
│ Sequence Number │
├───────────────────────────────────────────┤
│ Acknowledgment Number │
├────┼──────┼─┼─┼─┼─┼─┼─┼─────────────────┤
│Offs│Reserv│U│A│P│R│S│F│ Window │
├────┴──────┴─┴─┴─┴─┴─┴─┼─────────────────┤
│ Checksum │ Urgent Pointer │
├───────────────────────┴───────────────────┤
│ Payload Data │
└───────────────────────────────────────────┘
Flag Breakdown:
SYN - Synchronize (Establish connection)
ACK - Acknowledgment
FIN - Finish (Terminate connection)
RST - Reset (Abort connection)
PSH - Push (Pass to app immediately)
UDP Header Structure
┌─────────────────┬─────────────────┐
│ Source Port │ Dest Port │ (2 Bytes each)
├─────────────────┼─────────────────┤
│ Length │ Checksum │ (2 Bytes each)
├─────────────────┴─────────────────┤
│ Data Payload │
└───────────────────────────────────┘
Minimal overhead of only 8 bytes. Fast but raw.
Engineering Decisions: TCP or UDP?
- Does the data have to arrive intact and in order?
- YES $\rightarrow$ Use TCP (Web browsing, SSH, APIs).
- Is speed/latency more important than occasional data loss?
- YES $\rightarrow$ Use UDP (FPS Games, Voice calls, Discovery protocols).
- Can you handle reliability in the Application Layer?
- YES $\rightarrow$ Custom protocols like QUIC (HTTP/3) use UDP as a base but add selective reliability and congestion control on top.
Deep Technical Insights
Why 20 Bytes vs 8 Bytes?
The 20-byte TCP header is designed for state management. It needs "Sequence Numbers" to reorder scrambled packets and "Acknowledgment Numbers" to confirm receipt. UDP skips all of this; it doesn't care if the packet arrived or if it was the third one sent. This lack of state makes UDP ideal for stateless services like DNS.
Connection Multiplexing
Both protocols use Port Numbers to allow multiple applications to share a single IP address. A unique connection (Socket Pair) is defined by five values: (Source IP, Source Port, Destination IP, Destination Port, Protocol). This "5-tuple" is how a server handles 1,000 different browser tabs connecting to it simultaneously.