Notes
Side by side
Side by side. TCP sets up a connection with a handshake, guarantees delivery and order, and treats data as one continuous stream. UDP has no connection and no handshake, does not guarantee delivery or order, but keeps message boundaries: one send is one datagram on the other side. Its header is just eight bytes: two ports, a length and a checksum. And it has no congestion control, which becomes the application's job.
- TCP UDP
- connection yes no handshake 1 RTT none
- delivery yes no order yes no boundaries stream message
- header 20+ B 8 B
- congestion built in your job
No handshake
No handshake means the very first packet carries data. On our machine a UDP datagram to a local receiver arrived in forty thousandths of a millisecond. For comparison, our TCP client's connect step alone took between two point eight and four point six milliseconds on the same machine, including Python's overhead. That difference is why DNS uses UDP: a lookup is one small question and one small answer, and a handshake would double the time.
s = socket(AF_INET,
SOCK_DGRAM)
s.sendto(b"ping", (host,
9001))
datagram arrived in
0.040 ms
TCP connect alone took
2.8 to 4.6 ms in our tests
(incl. Python overhead) No guarantees
No guarantees means silence. Sending a UDP datagram to a port where nothing listened returned no error at all, it simply reported four bytes sent. TCP to a closed port fails loudly with connection refused. With UDP the remote kernel may send back an ICMP port unreachable message, but the send call never waits for it. This is why a misconfigured UDP log shipper or metrics client can lose data for weeks without a single error.
TCP to a closed port: ConnectionRefused UDP sendto() a closed port: no error, returns 4 (bytes "sent") the kernel may get an ICMP "port unreachable" later, but sendto() never waits
Where UDP fits
UDP fits three cases. Small request and answer exchanges, like DNS on port fifty three and NTP time sync. Data where losing a little is acceptable, like syslog lines or StatsD metrics. And real-time traffic, voice, video and games, where a late packet is useless anyway, so retransmitting it would only add delay. The fourth, newest case: QUIC, the transport under HTTP three, builds its own reliability and encryption on UDP to avoid TCP's limits.
- DNS one question, one answer (port 53)
- NTP time sync (123)
- syslog, StatsD metrics: losing one is fine
- VoIP, video, games: late data is useless
- QUIC / HTTP/3: reliability built on top of UDP
If you use UDP
If you build on UDP, you inherit TCP's jobs. Add timeouts and retries; DNS resolvers retry and fall back to TCP for large answers. Keep datagrams below the path MTU, since fragments are often dropped. Rate limit yourself, because there is no congestion control. Handle duplicates and ordering if they matter. And remember UDP source addresses are easy to fake, which is how DNS and NTP amplification attacks work.
- timeouts and retries (DNS: retry, then TCP)
- keep datagrams small: under the path MTU
- rate limiting: no congestion control
- dedupe and ordering if needed
- spoofing: amplification
Testing UDP
Testing UDP is tricky because there is no handshake to prove anything. netcat with minus u will happily report success to a port where nothing listens. So test with the real protocol: dig for DNS, a real NTP query, or send a test log line and check it arrives. On the server, ss minus u l n p shows which UDP ports are listening, and tcpdump shows whether datagrams arrive at all. The same applies to UDP load balancer health checks.
$ dig @10.0.0.2 example.com speaks the real protocol $ nc -u -v host 514 "succeeded" means little: no handshake to confirm $ ss -ulnp who listens $ tcpdump -n udp port 53 test with the protocol itself, not a port scan
Common mistakes
Mistakes. Treating a successful send as delivery. Trusting netcat's UDP port check. Sending large datagrams that fragment and disappear. No timeouts or retries in the client. And firewall or security group rules that open TCP fifty three but not UDP fifty three, or the reverse; DNS needs both.
- assuming sendto() success means delivered
- "port open" from nc -u
- large datagrams that fragment and vanish
- no retry or timeout
- firewall allows TCP 53 but not UDP 53 (or vice versa)
Say it like this
In the interview: TCP when every byte must arrive in order, like APIs, databases and file transfer. UDP when messages are small or real-time and late data is worthless, like DNS, metrics, voice and games. With UDP I own retries, timeouts, sizing and rate limiting, or I use a protocol like QUIC, which does that work for HTTP three on top of UDP.
- "TCP when every byte must arrive
- in order: APIs, databases, files. UDP when messages are small or real-time and late data
- is useless: DNS, metrics, voice, games. With UDP I own retries, timeouts and rate limits; QUIC
- does that for HTTP/3."