TCP vs UDP, and Why It Matters
One protocol makes sure every byte arrives, the other gets data there fast, and choosing between them affects everything from games to QUIC.
IP (Internet Protocol) moves packets towards their destination, and promises nothing else. A packet can be lost, arrive twice, or arrive after packets that were sent later.
The transport layer sits on top of IP and decides what to do about that. It offers two choices. TCP (Transmission Control Protocol) makes delivery reliable and in order, and that costs some time. UDP (User Datagram Protocol) adds almost nothing, and stays fast.
TCP is often treated as the normal choice and UDP as the unusual one. I think choosing between them is a real engineering decision, and the newest protocols are built on UDP.
The core trade-off
| TCP | UDP | |
|---|---|---|
| Connection | Yes, with a handshake | None |
| Delivery | Guaranteed and retransmitted | Best effort, may drop |
| Ordering | In order | Whatever arrives |
| Flow and congestion control | Yes | No, you build it |
| Header overhead | 20 bytes or more | 8 bytes |
| Feel | A phone call | Postcards |
TCP is like a phone call. You connect first, and then everything you say arrives in order. It gives your program a stream of bytes and deals with lost or out-of-order packets for you. UDP is like sending postcards. Each one travels alone, and some may never arrive. UDP adds only a small header to each packet, with the sender's port, the receiver's port, the length and a checksum, a number used to detect damaged data. Everything else is up to you.
TCP's three way handshake
Before any data is sent, TCP sets up a connection, and both sides agree on their starting sequence numbers.
That costs one full round trip, the time for a message to go there and back, before any of your data moves. For a short exchange, that's pure delay. For a long one, it's the price of reliability.
The sequence numbers are what let TCP notice a lost packet and put packets back in order. They also let both sides agree on where the stream starts.
How TCP keeps it reliable
Every byte has a sequence number. The receiver sends back acknowledgments, which say what it has received. If the sender gets no acknowledgment within a certain time, or gets the same acknowledgment several times, it sends the data again. That's called retransmitting.
Two more mechanisms work on top of that.
Flow control protects a slow receiver. The receiver keeps telling the sender how much free space it has, which is called the window. The sender never sends more than that, so the receiver's buffer never overflows.
Congestion control protects the network. The sender raises its sending rate slowly to find out how much the network can carry, and lowers it when packets get lost. This stops many TCP connections from overloading a shared link. Algorithms such as CUBIC and BBR do this work.
All of this happens automatically. You write bytes, and they arrive in order.
Head-of-line blocking: TCP's hidden cost
Here is a less obvious problem, and the rest of this post is about it.
TCP delivers bytes strictly in order. Suppose packet 5 is lost, and packets 6, 7 and 8 have already arrived. TCP won't give any of them to your program until packet 5 has been sent again and received. Everything waits behind the missing packet.
This is called head-of-line blocking, after a queue where the person at the front holds up everyone behind them.
For downloading one file, it doesn't matter. But some programs send many separate messages and need each one quickly. For them, it's a serious problem, because one lost packet holds up everything behind it, including data that has nothing to do with it.
When UDP wins
Sometimes data is only useful for a short time, and losing a little of it is acceptable. Then waiting for a resend is worse than moving on, because by the time the resent packet arrives, it's already out of date.
Online games. A player's position from 50 milliseconds ago is useless. You want the next update, not the old one sent again.
Live voice and video. Skip the lost frame, hide the gap, and continue. A short glitch is better than a frozen picture.
DNS (Domain Name System). It's one tiny question and one tiny answer. A TCP handshake would double the wait for no benefit.
With UDP you can build exactly the reliability you need and skip the rest. Here is a minimal UDP server in Go. Notice that there is no connection to accept, only packets to read.
addr, _ := net.ResolveUDPAddr("udp", ":9000")
conn, _ := net.ListenUDP("udp", addr)
defer conn.Close()
buf := make([]byte, 1500) // about one Ethernet MTU
for {
n, from, err := conn.ReadFromUDP(buf)
if err != nil {
continue
}
// each read is one independent datagram: no stream, no ordering
handle(buf[:n], from)
}
Each read returns one complete datagram, which is what a UDP packet is called. There is no stream that could get stuck. That independence is the reason to use UDP.
QUIC: having it both ways
The downside of UDP used to be that you lost reliability completely. QUIC (Quick UDP Internet Connections) solves that. It's the transport underneath HTTP (Hypertext Transfer Protocol)/3.
QUIC runs on top of UDP, but rebuilds reliability, ordering and congestion control inside the program itself. That gives two big advantages.
No head-of-line blocking between streams. QUIC carries several separate streams over one connection. A lost packet only holds up its own stream, and the others carry on. This was the biggest weakness of HTTP/2, which put all its streams on one TCP connection, so one lost packet stopped all of them.
Faster setup. QUIC combines its own handshake with the TLS (Transport Layer Security) encryption handshake. It often has an encrypted connection ready after one round trip, or after none when reconnecting to a server it knows. TCP plus TLS needs two.
Because QUIC runs inside programs on top of UDP, it can be improved without waiting for operating systems and network equipment to be updated. TCP can't do that, because it's built into the operating system and inspected by equipment along the way.
The HTTP side of this, from six connections per site to streams over QUIC, has its own post, HTTP/2 and HTTP/3: Same Requests, Different Wire.
The pattern is to start with UDP, which promises nothing, and add back only the promises you need. That's why the newest protocols are built on UDP rather than on TCP.
How to choose
Do you need every byte, in order, and can you accept one round trip to set up? Use TCP. That covers web pages, APIs, file transfers, SSH (Secure Shell) and databases.
Do you need the lowest delay, can you accept some loss, or do you want to build your own reliability? Use UDP. That covers games, live audio and video, and DNS.
Do you want several reliable streams with low delay and no head-of-line blocking? Use QUIC, which is UDP underneath with reliability added on top.
Knowing the trade-off lets you choose the right protocol. You don't pay for promises you don't need, and you don't find out in production that you needed them.
Check yourself
3 questions · nothing is savedReferences & Primary Sources
Official specifications, standards, and documentation cited in this post.
- [1]
DARPA Internet Program Protocol Specification defining connection establishment, reliability, and flow control
RFCrfc-editor.org - [2]
The original minimal datagram protocol specification
RFCrfc-editor.org - [3]
Modern replacement for TCP implementing congestion control in userspace
RFCrfc-editor.org
- Written
- Nov 9, 2025
- Length
- 1,219 words · 6 min
- Built
- …
- Served by
- …
- Delivered
- …
- Over the wire
- …
- Go's share
- …
HTTP/2 and HTTP/3: Same Requests, Different Wire
A GET request still means the same thing. What changed twice is how it travels: HTTP/2 put every request on one TCP connection, and HTTP/3 moved everything onto QUIC to fix the one problem HTTP/2 couldn't.
