TCP vs UDP
What TCP does to turn lossy packets into an ordered stream, what that costs in latency, what UDP leaves out on purpose, and how to choose between them.
The problem
The internet’s base layer, IP, makes one modest promise: it will try to deliver each packet. Packets can be lost, duplicated, delayed or delivered out of order, and each one holds about 1,500 bytes at most. A 1 MB response is therefore about 700 packets. On a path that drops 1% of them, about seven will vanish, and the rest may not arrive in the order they were sent.
Every application that wanted a reliable connection would have to number its packets, acknowledge them, resend the missing ones and put them back in order. TCP, specified in 1981, does that once, in the operating system, and hands applications a simple ordered stream of bytes.
It was not enough. In October 1986 the link between Lawrence Berkeley Laboratory and UC Berkeley, two sites a few hundred metres apart, dropped from 32 kbit/s to 40 bit/s. Every sender was reacting to loss by resending, which caused more loss. Van Jacobson’s 1988 fix taught TCP to treat loss as a sign of congestion and slow down. That is the congestion control every TCP connection still runs.
Then a different kind of application arrived. In a voice call, a packet that turns up 300 ms late is useless: the conversation has moved on. TCP would hold up newer audio to wait for it. For that application TCP’s guarantees are the problem, and the answer is the other transport protocol, UDP, which promises almost nothing.
What TCP does
Step 1 of 9
Before any data moves, both ends must agree on starting sequence numbers. The side opening the connection sends a SYN carrying its own, and the other side answers with a SYN-ACK that acknowledges it and adds its own.
Read the steps as text
- Before any data moves, both ends must agree on starting sequence numbers. The side opening the connection sends a SYN carrying its own, and the other side answers with a SYN-ACK that acknowledges it and adds its own.
- The opener acknowledges that number and the connection is open. That is three packets and one full round trip spent before the first byte of data.
- The app writes a stream of bytes. TCP cuts it into numbered segments. Segment one arrives, the receiver acknowledges it and passes its bytes up to the application.
- Segment two is dropped by a congested router somewhere on the path. Nobody is told. The packet is simply gone.
- Segment three arrives intact, but TCP promised the application an ordered stream. It parks segment three in the kernel buffer and delivers nothing. This is head-of-line blocking.
- The receiver keeps acknowledging the last in-order byte. Those duplicate acknowledgements, or a timer, tell the sender that segment two is missing. The sender also reads the loss as congestion and slows down.
- The sender retransmits segment two. Now the receiver can release two and three together. The application stalled for at least a round trip, even though segment three was sitting there the whole time.
- Send the same three messages over UDP and there is no handshake, no stall and no slowdown. One and three reach the application at once. Two is gone for good, unless the application notices and asks again.
- TCP buys a perfect ordered stream and pays for it in latency when packets are lost. UDP hands you raw datagrams and leaves the policy to you. Choose by asking whether late data is still worth having.
The handshake
A TCP connection starts with three packets. The client sends SYN with its starting sequence number, the server replies SYN-ACK with its own, and the client sends ACK. Both sides now hold state for the connection and know the other is reachable.
The cost is one round trip before the first byte of data. At 100 ms between client and server, that is 100 ms gone, and TLS adds at least one more round trip on top. This is why connection pools and keep-alive exist: the cheapest handshake is the one you do not repeat.
Reliability and ordering
Every byte has a sequence number. The receiver acknowledges the highest in-order byte it has, and the sender retransmits anything not acknowledged, either when a timer expires or when repeated acknowledgements for the same byte show that something is missing. The receiver buffers data that arrives early and releases it to the application strictly in order.
Note what the application gets: a stream of bytes, not messages. Two writes of 100 bytes may arrive as one read of 200, or as reads of 150 and 50. Protocols on TCP add their own framing, such as a length prefix.
Flow control and congestion control
Two separate limits apply to a sender, and it obeys the smaller.
- Flow control protects the receiver. Each acknowledgement advertises how much buffer space is free. A slow reader shrinks that window and the sender stops. This is backpressure built into the transport.
- Congestion control protects the network. The sender keeps its own estimate of what the path can carry, the congestion window. It starts small, at about ten segments (roughly 14 KB) on modern systems, doubles every round trip during slow start, then grows gently, and cuts back when it detects loss. Linux uses the CUBIC algorithm by default. Google’s BBR estimates bandwidth and round-trip time, and does not wait for loss.
Slow start has a design consequence. On a brand-new connection, with no loss, sending 1 MB takes about seven round trips, because the window has to double its way up from 14 KB. A warm connection sends the same data in one or two.
Head-of-line blocking
Ordered delivery has a price. If one segment is lost, every later segment waits in the kernel until the retransmission arrives, even though those bytes are already on the machine. For a single file download that hardly matters. It matters a great deal when several independent things share one connection, which is the story of HTTP/2 and HTTP/3.
What UDP leaves out
UDP adds an 8-byte header to IP: source port, destination port, length and checksum. There is no handshake, no acknowledgement, no retransmission, no ordering, no flow control and no congestion control. Each datagram is independent and arrives whole or not at all, so message boundaries are preserved. Senders usually keep datagrams to around 1,200 bytes so that they fit in one IP packet on almost any path.
That emptiness is useful in three situations.
- One small question, one small answer. A DNS lookup is two packets over UDP. Over TCP the handshake alone would more than double that. If the reply is lost, the client just asks again.
- Late data is worthless. Voice and video calls send media over UDP (WebRTC uses RTP on UDP). A lost packet becomes a brief glitch that the codec hides. Fast-paced games send positions many times a second, and a newer update replaces a lost one.
- You want a different transport. QUIC wanted streams that do not block each other, a faster handshake and connections that survive a change of network. A new protocol beside TCP and UDP would be dropped by firewalls and NAT devices across the internet, and changing TCP means waiting years for operating systems and network equipment. So QUIC runs over UDP and implements reliability, congestion control and encryption itself, in the application.
What you take on
If you choose UDP, the missing features become your job when you need them. Three are easy to forget:
- Congestion control. A sender that ignores loss harms everyone on the path, including itself.
- Spoofed senders. With no handshake, the source address is unverified. Attackers send small requests with a victim’s address and let servers flood the victim with large replies. A protocol on UDP must not send a big response to an address it has not checked. QUIC limits a server to three times the bytes it has received until the client’s address is validated.
- Hostile networks. NAT devices forget idle UDP flows quickly, often within a minute or two, so you need keep-alives. Some corporate networks block UDP outright, so you need a fallback over TCP.
When to choose each
| Workload | Choose | Reason |
|---|---|---|
| APIs, web pages, database connections, file transfer | TCP | Every byte must arrive, in order |
| Tiny self-contained request and reply (DNS, NTP) | UDP | A handshake would cost more than the exchange |
| Voice and video calls | UDP | Late packets are useless; hide the loss |
| Fast-paced game state | UDP | New state replaces old; add reliability only for the events that need it |
| Many streams on lossy mobile networks | QUIC | Reliable, but a loss delays only one stream |
The test is one question: is late data still worth having? If yes, use TCP, or QUIC. If you choose UDP and then find yourself building acknowledgements, ordering and congestion control, you are rebuilding TCP, and should use TCP or QUIC unless you have a measured reason not to.
In the interview
You rarely design a transport protocol in an interview, but the choice shows up in chat, video, gaming and anything on mobile networks, and “why TCP here?” is a common probe.
A strong answer names the property the workload needs. For example: “Messages must all arrive and in order, so TCP, through WebSockets. The media stream for calls is different: a late frame is worthless, so that goes over UDP with WebRTC.” Then mention the cost you are accepting: a handshake round trip and slow start on new connections for TCP, so you reuse connections; or, for UDP, that loss handling, congestion control and a TCP fallback are now your job.
Key takeaways
- IP delivers packets on a best-effort basis. TCP adds a handshake, ordering, retransmission, flow control and congestion control on top.
- A new TCP connection costs one round trip before data and starts slowly, which is why connection reuse matters so much.
- TCP is one ordered byte stream, so a single lost packet holds up everything behind it. That is head-of-line blocking.
- UDP adds only ports and a checksum. It suits small request-response exchanges and data that is worthless when late.
- Building on UDP means you own reliability, congestion control and protection against spoofed senders. QUIC is that work done once.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
What are the three packets of the TCP handshake, and what does it cost?
SYN, SYN-ACK, ACK. Each side sends its starting sequence number and acknowledges the other's. It costs one round trip before any data can be sent.
Flow control and congestion control both limit a TCP sender. What is the difference?
Flow control protects the receiver, which advertises how much buffer it has free. Congestion control protects the network, using the sender's own estimate of what the path can carry.
What is head-of-line blocking in TCP?
Because TCP delivers bytes strictly in order, one lost segment holds back every later segment, even ones that have already arrived, until the lost one is retransmitted.
What does UDP add on top of IP?
Source and destination ports, a length and a checksum, in an 8-byte header. No handshake, acknowledgements, ordering, flow control or congestion control.
Name three good uses of UDP and the reason they share.
DNS lookups, real-time voice and video, and fast-paced game state. In each, either the exchange is one small request and reply, or data that arrives late is not worth having.
Why is connection reuse so valuable with TCP?
A reused connection skips the handshake round trip (and the TLS handshake on top) and keeps its grown congestion window, so it is not stuck in slow start.