0002 - TCP vs. UDP
Publish date: 2025-06-26
Tags:
Networking, Interview-Questions
Core Comparison
Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
---|---|---|
Connection | Connection-Oriented: Requires a “handshake” to establish a connection before data is sent. | Connectionless: Just sends packets (datagrams) without establishing a connection. Fire-and-forget. |
Reliability | Reliable: Guarantees that data is delivered in the order it was sent and is error-checked. It retransmits lost packets. | Unreliable: No guarantee of delivery, ordering, or error checking. Packets may be lost, duplicated, or arrive out of order. |
Ordering | Ordered: Packets are reassembled in the correct sequence. | Unordered: Datagrams are independent and may arrive in any order. |
Overhead | High: The TCP header is larger (20 bytes) due to sequence numbers, acknowledgments, etc. | Low: The UDP header is much smaller (8 bytes). |
Speed | Slower: Due to the overhead of reliability checks, handshakes, and congestion control. | Faster: No connection setup, no acknowledgments. |
Use Cases | Web Browse (HTTP/S), file transfers (FTP), email (SMTP), database connections. Anything where data integrity is paramount. | Video/audio streaming, online gaming, DNS, VoIP. Anything where speed is more critical than 100% reliability. |
How TCP Achieves Reliability
- The 3-Way Handshake: This is how a connection is established.
- SYN: The client sends a SYN (synchronize) packet to the server to initiate a connection.
- SYN-ACK: The server replies with a SYN-ACK (synchronize-acknowledgment) packet.
- ACK: The client responds with an ACK (acknowledgment) packet, and the connection is established. Data transfer can now begin.
- Sequence Numbers and Acknowledgments (ACKs): Each byte of data is given a sequence number. The receiver sends ACKs to confirm which bytes it has received. If the sender doesn’t receive an ACK for a packet within a certain time, it retransmits the packet.
- Flow Control: The receiver has a receive buffer. It uses a “window size” in its ACK packets to tell the sender how much buffer space is available. This prevents the sender from overwhelming the receiver. This is the sliding window mechanism.
- Congestion Control: This is different from flow control. Flow control is about the receiver; congestion control is about the network itself. TCP assumes that packet loss is due to network congestion. When a packet is lost, the sender slows down its transmission rate to reduce the load on the network. It does this by adjusting its congestion window using algorithms like “slow start” and “congestion avoidance.”
When to Choose UDP (and how to build reliability on top)
- Answer: “I’d choose UDP for latency-sensitive applications like real-time video streaming. In video, a dropped frame is better than a delayed frame that arrives late. While UDP itself is unreliable, we can build ‘pseudo-reliability’ at the application layer. For example, we might not need to retransmit every lost packet, but we could implement our own logic to request a keyframe if a critical video frame is lost. This gives us more control over the trade-off between latency and reliability than TCP allows. The QUIC protocol, which underlies HTTP/3, is a great example of this—it’s built on UDP but implements its own streams, reliability, and congestion control.”
Tags:
Networking, Interview-Questions