← All questions

MediumJuniorDevOps
What is the main difference between the TCP and UDP protocols?
TL;DR
TCP is a connection-oriented, reliable protocol that guarantees delivery, ordering,
and error-checking at the cost of overhead. UDP is a simple connectionless protocol
with no delivery or ordering guarantees, but it's faster and lighter.
TCP and UDP are the two main transport-layer protocols that run on top of IP. The core distinction: TCP is connection-oriented and reliable, whereas UDP is connectionless and works on a "fire and forget" basis.
TCP (RFC 9293)
- Connection-oriented. Before sending data, endpoints perform a three-way handshake (3WHS): SYN → SYN-ACK → ACK, synchronizing initial sequence numbers.
- Reliable and ordered. Each byte gets a sequence number; the receiver sends acknowledgments (ACKs), lost segments are retransmitted, and data is reassembled in the original order. Integrity is verified with a checksum. RFC 9293 states TCP "provides a reliable, in-order, byte-stream service."
- Byte stream, no message boundaries. TCP delivers an unstructured byte stream: message boundaries are not preserved (the application must frame its own messages). UDP, by contrast, is message-oriented — each datagram is delivered as a discrete, complete unit or not at all (one sendto = one recvfrom).
- Flow and congestion control. TCP throttles its send rate so it overwhelms neither the receiver nor the network.
- Trade-off: more overhead and latency, and a larger header — 20–60 bytes (20 without options, extendable via options such as MSS, window scaling, SACK).
UDP (RFC 768)
- Connectionless. There is no handshake; each datagram is independent.
- No guarantees. The RFC explicitly says "delivery and duplicate protection are not guaranteed" — UDP does not recover lost, duplicated, or out-of-order packets.
- Fixed 8-byte header with just four fields: Source Port, Destination Port, Length, Checksum.
- Benefits: low latency and overhead, plus support for broadcast/multicast.
When to use which
- TCP: web (HTTP/HTTPS over TCP), email, file transfer, SSH — anywhere data integrity matters.
- UDP: DNS lookups, online gaming, VoIP, live video streaming — where speed matters more than a single lost packet.
Common follow-up points
- UDP itself doesn't order data, but an application can layer its own logic on top (QUIC does this over UDP).
- HTTP/3 runs on QUIC over UDP, while HTTP/1.1 and HTTP/2 run over TCP.
- The UDP checksum is technically optional on IPv4 (a zero value means "not computed") but mandatory on IPv6.