TCP and UDP are transport layer protocols that applications use to communicate. TCP is reliable, ordered, handshaked; UDP is fast, unreliable, connectionless. TCP is used for most applications (HTTP, email, file transfer). UDP is used for speed-critical applications (video, gaming, voice). Understanding both is essential for network programming and system design.
TCP vs UDP
Side-by-Side Comparison
| Aspect | TCP | UDP |
|---|---|---|
| Connection Model | Connection-oriented: establish connection (3-way handshake), send data, close connection. Stateful. | Connectionless: send data immediately without setup. No handshake. Stateless. |
| Reliability | Guaranteed delivery in order. Lost packets retransmitted automatically. Application never loses data. | No delivery guarantee. Packets may arrive out of order or not at all. Application handles retries. |
| Speed | Slower due to handshake, acknowledgments, retransmission. 10-50ms overhead per connection. | Very fast. Single packet delivery. No acknowledgment overhead. Minimal latency. |
| Handshake Process | Three-way handshake: SYN, SYN-ACK, ACK. 1-3 round trips before data flows. | No handshake. Send first packet immediately. |
| Bandwidth Overhead | Larger headers and acknowledgment packets. ~15% more bandwidth for reliability. | Minimal overhead. Only application data + tiny header (8 bytes). Bandwidth efficient. |
| Packet Ordering | Packets arrive in order. Sequence numbers ensure ordering. Application gets ordered data. | No ordering guarantee. Packet 3 may arrive before packet 1. Application must handle out-of-order. |
| Common Use Cases | HTTP/HTTPS (web), SMTP (email), SFTP (file transfer), SSH (remote access), FTP. Anything requiring data integrity. | Online gaming, video streaming, VoIP, live broadcasts, DNS, SNMP. Latency-sensitive applications. |
| Real-Time Applications | Not suitable for real-time (retransmissions cause jitter). Would add unacceptable latency. | Perfect for real-time. One lost video frame acceptable. Resend would be worse than loss. |
When to Use Each
[object Object]
Verdict
Verdict: Use TCP as default (web, email, databases, everything important). Use UDP only when latency is critical and occasional packet loss is acceptable (video, gaming, voice). HTTP/3 is evolving with QUIC (UDP-based, but adds reliability) as a hybrid approach. For interviews and learning, remember: TCP = reliable/slow, UDP = fast/unreliable. Most internet traffic is still TCP-based.