When to use UDP instead of TCP? Principles and monitoring
A guide to choosing UDP or TCP based on latency, packet loss, and how to monitor them in homelab and Kubernetes.
You’re on a Zoom call. Your colleague’s voice cuts out, the video freezes for a few seconds and then jumps. You try opening a website at the same time: it still loads, just a bit slower. Why is the call worse? Because the call runs on UDP, while the website runs on TCP. TCP keeps every byte in order; UDP does not. When the network has problems, TCP’s price is latency: it waits for lost packets to be resent. For calls, waiting means lost audio. For websites, waiting means slower but still correct. This post covers when to choose UDP over TCP, and once you’ve chosen UDP, how monitoring it differs from TCP.
Two protocols, two kinds of contract
TCP is a contract: sender and receiver establish a connection, number each packet, acknowledge receipt, resend if lost, and reorder if out of order. In return, every byte arrives in order. UDP has no contract at all: the sender just throws packets, with source and destination ports. No handshake, no acknowledgment, no resend. If a packet is lost, neither side knows unless the application detects it.
This difference decides everything. TCP fits when losing a byte corrupts the data: file downloads, email, web browsing. UDP fits when a late resend is worse than loss: audio, video, game positions. The rule is short: if late data is useless, choose UDP; if missing data is wrong, choose TCP.
A real example: DNS and port 53
Every time you open a website, your machine asks the DNS server: “what IP address is this name?”. The question and answer usually fit in one packet. With TCP, each query needs a three-way handshake before asking, plus a connection close. With UDP, just one packet out and one back. That’s why DNS uses UDP port 53 for most queries. When the answer is too large (e.g., DNSSEC records), DNS switches to TCP to ensure full delivery. Same service, two protocols, chosen by size and requirement.
You can check it yourself with dig, available on most Linux systems:
$ dig +short thomas-labs.com
203.0.113.10
$ dig +tcp +short thomas-labs.com
203.0.113.10
The first uses UDP (default), the second adds +tcp to force TCP. Both return the same result, but the first needs no handshake. On a network with 20 ms latency, a TCP handshake adds at least one round trip, about 20 ms more. The 20 ms is an example, not a measurement from your system.
Cases where UDP wins
Beyond DNS, UDP wins where latency matters more than completeness:
- VoIP and video calls: losing a 20 ms audio packet is barely noticeable. Resending it 200 ms later is too late and only adds congestion.
- Multiplayer games: player positions are sent continuously. Old positions are worthless; dropping lost packets and waiting for new ones is better.
- Video streaming: each frame has a deadline. Resending a late frame only wastes bandwidth.
- Broadcast and multicast: TCP only does one-to-one. UDP supports sending one packet to many machines, needed for DHCP, ARP (actually a lower-layer protocol), and many service discovery protocols.
- QUIC and HTTP/3: an interesting exception. QUIC runs on UDP but implements reliability at the application layer to avoid one lost packet blocking the whole connection as TCP does. If you see UDP port 443 traffic, it’s usually HTTP/3.
Conversely, don’t choose UDP for databases, file transfers, or APIs that need guarantees. You’d have to rewrite resend, ordering, and congestion control yourself, i.e., redo TCP but worse.
How monitoring TCP and UDP differ
This is the easiest part to get wrong in operations. With TCP, you can look at connection state. ss -tan shows open connections, bytes sent, retransmissions. With UDP, there’s no connection to look at. ss -uan only lists open ports, no state, no reliable metrics. UDP packets arrive and vanish, no one acknowledges.
So UDP monitoring must rely on lower-layer or application metrics. At the network layer, look at packet statistics from the network card and OS:
$ nstat -az | grep -E 'UdpInDatagrams|UdpNoPorts|UdpInErrors'
UdpInDatagrams 12345 0.0
UdpNoPorts 12 0.0
UdpInErrors 0 0.0
These three numbers tell a story. UdpInDatagrams increasing means packets are arriving. UdpNoPorts increasing means packets arrive at a port no one listens on, a sign of misconfiguration or port scanning. UdpInErrors increasing means packets arrive but error out, often due to full buffer. If the buffer is full, packets are dropped without notice. That’s why with UDP you must increase buffer size and monitor it.
The chart below illustrates UDP drop rate at different loads in a hypothetical test, just to show the trend, not real measurements.
xychart-beta
title "UDP drop rate by load (hypothetical)"
x-axis ["100", "500", "1000", "2000", "5000"]
y-axis "Drops per second" 0 --> 100
bar [0, 2, 10, 45, 90]
In practice, measure with your own application. For DNS, track unanswered query rate. For VoIP, track jitter and packet loss from application logs. For games, track lost packets from the client side.
Best practices for running UDP services in a homelab
A UDP service in a homelab is usually DNS, VPN (WireGuard uses UDP), or a game server. A few habits save you from guessing:
- Increase buffers: Linux UDP defaults are often small. Check with
sysctl net.core.rmem_maxandnet.core.wmem_max. Increase if the application reports drops. - Log at the application: since the kernel doesn’t report drops, the app must count itself. For example, CoreDNS has
coredns_dns_responses_totalby response code; ifSERVFAILrises, it may be due to drops. - Use firewalls carefully: UDP has no state, so firewall rules must be more permissive. Don’t accidentally block UDP port 53.
- Check MTU: UDP packets larger than MTU are fragmented or dropped. For VPN, set MTU a bit lower to avoid fragmentation.
Testing and measuring on Kubernetes
In Kubernetes, a UDP service needs a ClusterIP or NodePort Service with protocol: UDP. If you forget, UDP traffic is blocked. Below is the packet flow when a pod queries DNS via a UDP Service.
sequenceDiagram
participant P as Pod
participant S as DNS Service (UDP)
participant C as CoreDNS
P->>S: Query A? (UDP)
S->>C: Forward packet
C-->>S: Reply (UDP)
S-->>P: Reply
To test, run a temporary pod and use dig:
$ kubectl run -it --rm debug --image=alpine -- sh
/ # apk add bind-tools
/ # dig +short +time=2 kubernetes.default.svc.cluster.local
10.96.0.1
If the command hangs or doesn’t respond, check that the Service has protocol: UDP and that CoreDNS is running. To measure drops, you can use mtr or count failed dig attempts over a period. For example, run 100 queries and count failures:
$ for i in $(seq 100); do dig +short +time=1 +tries=1 example.com >/dev/null || echo fail; done | wc -l
0
A result of 0 means no packets lost in 100 tries. If greater than 0, you have a network or configuration issue.
Summary
Choose UDP when late data is useless and loss doesn’t break the application; choose TCP when you need every byte guaranteed. Once you’ve chosen UDP, don’t look for connection state: measure at the application and in OS packet statistics. A healthy UDP service is one where you know exactly how many packets it’s losing, not one that looks quiet.