TCP vs UDP: The Two Ways to Move Data, and Why Neither Is "Better"
Back in the OSI post I mentioned that Layer 4's whole job is service-to-service delivery — making sure data reaches the right application on a host, not just the right machine — and that TCP and UDP are the two strategies for doing it. This post is that comparison properly unpacked, because "TCP is reliable, UDP is fast" is the kind of half-explanation that sounds fine until someone asks you what…
TCP and UDP are two strategies for moving data between applications on hosts, according to Layer 4 of the OSI model. TCP is reliable, while UDP is fast.
First, connection-oriented vs. connectionless: TCP establishes a connection before data transmission begins with a three-way handshake (SYN, SYN-ACK, ACK). This creates an official start and end to the conversation. UDP lacks this formal connection process, treating each packet individually. Engineers often define a UDP connection based on a timeout window, where packets sharing the same five-tuple (source IP, source port, destination IP, destination port, and protocol) within a certain timeframe are considered part of the same logical connection. This timeout isn't standardized, with two minutes being a common default.
Second, TCP's reliability consists of confirmation of delivery, in-order delivery, and awareness of errors. Every time TCP data arrives at the receiving host, an acknowledgment is sent back, confirming delivery. This allows the sender to recognize errors and surface them as application-level errors. TCP also ensures in-order delivery by labeling each chunk of data with a sequence number and reassembling them correctly before sending them to the application.
If errors occur, TCP is aware and can notify the application. In contrast, UDP lacks any built-in reliability, acknowledgments, or error detection mechanisms at the Layer 4 level. A UDP sender doesn't know whether its data actually arrived at the destination.
Third, TCP employs flow control to adjust its transmission rate dynamically, using as much of the available bandwidth as it can safely handle. It starts conservatively, gradually increasing its sending rate, and backs off when packet loss is detected. This allows TCP to optimize its transmission based on the available bandwidth throughout the entire path.
UDP, on the other hand, sends data at the rate allowed by the local connection without considering the available bandwidth on other parts of the path. If the sender's local link has more bandwidth than a narrower link further along the route, UDP can send data at a rate the far end can't sustain, causing excess data to get dropped.
Lastly, header overhead: TCP's extra features, such as sequencing, acknowledgment, flow control, and more, require tracking within each segment's header. A UDP header is minimal, consisting of only eight bytes (source port, destination port, length field, and an optional checksum). In contrast, a TCP header is significantly larger, containing at least 20 bytes (source and destination port, sequence numbers, acknowledgment numbers, flags, and a mandatory checksum) and up to 60 bytes when optional fields are included.
This directly impacts the physical cost of using TCP, as more features translate to more bytes spent on bookkeeping with every packet sent.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.