What is the difference between TCP and UDP?
TCP and UDP are both transport layer protocols that sit on top of IP, but they make fundamentally different trade-offs between reliability and performance.
TCP is a connection-oriented protocol. Before any data is sent, the client and server perform a three-way handshake (SYN, SYN-ACK, ACK) to establish a connection. TCP guarantees reliable, ordered delivery of data through sequence numbers on every segment, acknowledgments from the receiver, and automatic retransmission of lost packets. TCP also provides flow control via a sliding window mechanism and implements congestion control algorithms that adapt the sending rate to network conditions.
The cost of these guarantees is overhead and latency. The three-way handshake adds a round trip before data transfer begins. Every segment must be acknowledged, and if a packet is lost, subsequent data is held until the missing segment is retransmitted (head-of-line blocking). TCP headers are 20-60 bytes.
UDP is a connectionless protocol. There is no handshake — the sender simply fires packets at the destination. UDP provides no guarantee of delivery, no ordering, no duplicate detection, and no flow control. Each datagram is independent. This makes UDP extremely lightweight and fast, with a minimal 8-byte header.
The choice depends on requirements. TCP is right when data integrity is critical: HTTP, HTTPS, SSH, FTP, SMTP, and database connections. UDP is preferred when low latency matters more than perfect delivery: VoIP, video streaming, online gaming, DNS queries.
Modern protocols often build on UDP to get the best of both worlds. QUIC, which powers HTTP/3, runs over UDP but implements its own reliability and congestion control in user space, avoiding TCP's head-of-line blocking while still providing reliable delivery.
| Aspect | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (3-way handshake) | Connectionless (no setup) |
| Reliability | Guaranteed delivery with retransmission | Best-effort, no delivery guarantee |
| Ordering | Segments delivered in order via sequence numbers | No ordering guarantee |
| Flow control | Sliding window + congestion control | None |
| Header size | 20-60 bytes | 8 bytes |
| Latency | Higher (handshake, retransmit waits) | Lower (fire and forget) |
| Use cases | HTTP, SSH, FTP, databases | DNS, VoIP, gaming, video streaming |
Candidate correctly states TCP is reliable and ordered while UDP is fast and unreliable, and gives use cases, but lacks detail on mechanisms.
Candidate explains connection-oriented vs connectionless, describes TCP's reliability mechanisms (seq numbers, ACKs, retransmission, flow/congestion control), contrasts UDP's minimal overhead, gives appropriate use cases, and ideally mentions QUIC.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment