TCP and UDP: choose by task, verify by measurement

Set up two containers, measure TCP and UDP with iperf3, add 2% packet loss with tc, then see which hop mtr blames. Plus how to choose and the port 53 trap.

The evening the dashboard spins

You open the Grafana dashboard you built on your homelab, and wait ages for the chart to appear. You click a panel, it spins for a few seconds. You think the machine is weak, but it’s only running a few containers, CPU below 20%.

The usual culprit is packet loss on the path. With TCP, each lost packet means the sender has to wait and resend, and it reads loss as a sign of congestion so it slows down further. On a path with loss, that waiting adds up over thousands of packets and the page gets slower.

TCP (Transmission Control Protocol) is connection-oriented: before sending data, both sides do a three-way handshake to confirm they can hear each other; while sending, it numbers every byte, acknowledges receipt and resends if lost. UDP (User Datagram Protocol) is just an envelope: send a packet, no warning, no acknowledgement, no resend. The application handles loss itself.

This post sets up two containers side by side, measures both with iperf3, adds packet loss with tc, then uses mtr to find the lossy hop. All numbers below are from my run on a lab machine, not a standard for every network.

What TCP does that UDP skips

The diagram below shows the order of things TCP does before and during data transfer, the part UDP skips entirely.

sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: SYN
    S->>C: SYN-ACK
    C->>S: ACK
    Note over C,S: TCP connected, now data can flow
    C->>S: data 1
    S->>C: ACK 1
    C->>S: data 2 (lost)
    Note over C,S: TCP waits for timeout then resends data 2

The price of TCP is latency: it keeps state per connection and waits when a packet is lost. The price of UDP is that the application must handle loss, or accept it.

Building a small lab

I set up two containers on the same machine, connected via a bridge network. One runs iperf3 -s as server, the other runs the client. iperf3 is a bandwidth measurement tool, with both TCP and UDP modes.

Measure TCP for 10 seconds:

iperf3 -c 172.18.0.2 -t 10

My result: about 2.4 Gbps, retransmit 0, because the path between two containers on the same machine is clean.

Measure UDP with a preset bandwidth:

iperf3 -c 172.18.0.2 -u -b 1G -t 10

Result: about 940 Mbps received, 0.02% packet loss. I set 1G to see if UDP keeps its rate with minor loss. It does, because it doesn’t wait to resend.

Now add simulated loss with tc, the tool that controls the kernel packet queue, on the container’s interface:

tc qdisc add dev eth0 root netem loss 2%

Measure TCP again with the same command: bandwidth drops to about 300 Mbps, retransmits climb into the thousands. TCP reads loss as congestion and reduces its send rate. On a path with 2% loss, a web page can be many times slower than on a clean path.

Measure UDP again: still about 940 Mbps, but losing about 2% of packets. UDP doesn’t slow down, and doesn’t know it’s losing. For video, 2% of frames lost is usually invisible. For files, 2% of bytes lost is a corrupted file.

The chart below is the measured result with 2% loss enabled, not an illustrative number.

xychart-beta
    title "Measured bandwidth with 2% packet loss"
    x-axis ["TCP", "UDP"]
    y-axis "Mbps" 0 --> 1000
    bar [300, 940]

Other networks will give different numbers, but the trend is the same: TCP drops when packets are lost, UDP doesn’t.

Finding where packets are lost with mtr

mtr (my traceroute) runs continuously and counts loss at each hop along the path:

mtr -rwzc 50 172.18.0.2

My result showed 2% loss at the last hop, matching the netem config. If the loss is at an intermediate hop, mtr points to that hop.

With UDP, mtr also works, but many routers block ICMP so results may miss hops. That’s one cost of UDP for monitoring: middleboxes often don’t reply.

How monitoring TCP differs from UDP

With TCP, you can see connection state. ss -tan prints connections and their states. On a slow web server, many lines in TIME-WAIT mean many short connections opened and closed repeatedly. Many lines in SYN-SENT mean the handshake didn’t complete, usually due to packet loss or a firewall blocking.

UDP has no connection state. ss -uan only lists open sockets, it doesn’t tell you whether packets arrive. To know, you must measure at the application layer or count packets at the interface. That’s why TCP dashboards are full of metrics while UDP is nearly empty: not because UDP is less important, but because it leaves no trace. For DNS, you track response rate and latency at the DNS server itself.

Common mistakes when choosing

DNS defaults to UDP on port 53. Responses larger than 512 bytes under the original protocol switch to TCP, or use EDNS0 to extend. So on the firewall you must open port 53 for both TCP and UDP. Opening only UDP is enough for most queries, until you hit a large query and a puzzling error.

DHCP also uses UDP: a machine new to the network has no IP address yet so it can’t open a TCP connection, it broadcasts a UDP packet and waits for the server to reply.

In Kubernetes, Services default to TCP. Running a game server or VoIP service in the cluster means declaring protocol: UDP in the Service, otherwise traffic never reaches the pod. This error gives no clear message, just a connection that doesn’t work.

A common misconception: UDP is always faster. In a clean lab, TCP hit 2.4 Gbps while UDP set to 1G only reached 940 Mbps. TCP isn’t slower on a good network, it’s only slower when packets are lost. Choosing UDP just for speed isn’t enough reason.

Conversely, choosing TCP for everything also has a cost. An online game using TCP will stutter when packets are lost, because TCP waits to resend instead of skipping, even with bandwidth to spare.

Choose by task

If losing one packet makes the data useless, use TCP: config files, HTTPS pages, SSH on port 22, email over SMTP. If losing one packet is still usable and latency matters more, use UDP: voice, games, live video.

The surest way is to measure before choosing. One iperf3 command and one mtr run tell you where your network stands, instead of guessing by feel.