TCP vs UDP¶
TCP (Transmission Control Protocol) — reliable, ordered, connection-oriented. Uses 3-way handshake, retransmissions, flow control. Used for: HTTP, FTP, email. UDP (User Datagram Protocol) — unreliable, no ordering, connectionless. Lower latency, no overhead. Used for: DNS, streaming, gaming, VoIP. Choose TCP when data integrity matters, UDP when speed matters.
Key Concepts¶
Deep Dive: TCP vs UDP Comparison
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | Best-effort |
| Ordering | Maintained | Not guaranteed |
| Speed | Slower (overhead) | Faster |
| Header size | 20 bytes | 8 bytes |
| Flow control | Yes (sliding window) | No |
| Congestion control | Yes | No |
| Use case | Web, email, file transfer | Streaming, DNS, gaming |
Deep Dive: TCP 3-Way Handshake
Client Server
│ │
│── SYN ──────────→│ 1. Client requests connection
│ │
│←── SYN-ACK ──────│ 2. Server acknowledges + requests
│ │
│── ACK ──────────→│ 3. Client acknowledges
│ │
│ Connection Established │
TCP teardown (4-way):
Deep Dive: TCP Reliability Mechanisms
- Sequence numbers: Track byte order
- Acknowledgments: Confirm receipt
- Retransmission: Resend lost packets (timeout)
- Flow control: Receiver advertises window size
- Congestion control: Slow start, congestion avoidance
Common Interview Questions
- What is the difference between TCP and UDP?
- Explain the TCP 3-way handshake.
- Why is UDP faster than TCP?
- When would you use UDP over TCP?
- What is flow control? Congestion control?