TCP vs. UDP: The Core Protocols
Protocols are the sets of rules that govern how data is transmitted across a network. The two most common protocols at the Transport Layer are TCP and UDP.
What is Transmission Control Protocol (TCP)?
TCP is a connection-oriented protocol that ensures reliable, ordered, and error-checked delivery of a stream of bytes between applications.
- Connection-oriented: A connection must be established before data data is sent.
- Reliable & Ordered: Ensures every packet arrives in the correct sequence.
- Error-checked: Re-transmits lost packets and verifies data integrity.
The TCP Three-Way Handshake π€
TCP uses a specific sequence to establish a reliable connection.
What is User Datagram Protocol (UDP)?
UDP is a connectionless protocol that prioritizes speed and efficiency over reliability. It sends "datagrams" without needing to establish a formal connection.
- Connectionless: Data is sent immediately without a handshake.
- Faster: No overhead from connection management or error recovery.
- No Guarantees: Doesn't track whether packets arrive or if they arrive in order.
TCP vs. UDP Flow Comparison
Key Differences Between TCP & UDP
| Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
|---|---|---|
| Reliability | β Reliable (ensures delivery) | β Unreliable (no guarantee) |
| Speed | π’ Slower (due to handshake & overhead) | β‘ Faster (no retransmission overhead) |
| Connection Type | π Connection-oriented | π‘ Connectionless |
| Ordering | β Ensures packets arrive in order | β No guarantee of packet order |
| Error Handling | β Built-in error checking & retransmission | β Minimal error checking, no retransmission |
| Overhead | π High (due to handshake & sequencing) | π Low (minimal protocol overhead) |
When to Use TCP vs. UDP?
π‘οΈ Use TCP when data integrity is critical
- Web Browsing: HTTP/HTTPS require full data to render pages correctly.
- File Transfers: FTP/SFTP ensure files aren't corrupted during transit.
- Email: SMTP/IMAP require complete delivery of messages.
- Databases: SQL connections must be atomic and consistent.
π Use UDP when speed is more important than reliability
- Video Streaming: YouTube/Netflix can handle lost pixels for better speed.
- Online Gaming: Low latency is crucial for real-time interaction.
- VoIP calls: Skype/Zoom prioritize voice continuity over minor packet loss.
- DNS Lookups: Fast resolution is better for overall network performance.
Interview Questions: TCP & UDP
1. What is the primary difference between TCP and UDP?
The main difference is reliability vs. speed. TCP ensures that every packet arrives correctly and in order via a "handshake" and acknowledgments, while UDP just sends data as fast as possible without checking if it arrives.
2. Why is HTTP (Web) built on TCP?
HTTP requires the entire webpage's HTML, CSS, and JS to be complete. If packets were lost (as in UDP), the page would be broken, missing images, or incorrectly styled.
3. How does TCP handle congestion control?
TCP uses mechanisms like Window Scaling and Slow Start to monitor network congestion. If it detects packet loss, it slows down its transmission rate to avoid overwhelming the network.
4. Why would a multiplayer game use UDP instead of TCP?
Games (especially shooters or sports games) need sub-50ms latency. If one packet is lost, it's better to just drop it and show the latest player position rather than waiting for TCP to re-transmit the "old" position, which would cause "lag."
Summary & Key Takeaways
- TCP is reliable but slower; UDP is fast but unreliable.
- TCP is best for web pages, file transfers, and email.
- UDP is best for real-time applications like video calls and gaming.
- System design must balance speed, reliability, and user experience.
What's Next?: Next, we'll dive into HTTP - the protocol that powers the World Wide Web!