DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · TRANSPORT · 33 OF 187

UDP vs TCP: Differences and When to Use Each

UDP compared with TCP, measured: no handshake (a localhost datagram arrived in 0.040 ms), no delivery guarantee (sending to a closed UDP port raised no error at all), message boundaries, where UDP is used (DNS, NTP, syslog, metrics, video, QUIC and HTTP/3), why health checks for UDP services are tricky, and how to test a UDP port with nc and dig.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 10

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • No handshake
  • no error to a closed port
  • 8-byte header
  • DNS, NTP, metrics, video, QUIC
  • test with the real protocol

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 vs UDP
  • 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.

Our run, localhost
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.

Closed UDP port (ours)
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.

Common users
  • 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.

Your responsibilities
  • 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.

Tools
$ 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What are the main differences between TCP and UDP?

TCP is connection-oriented, reliable, ordered and stream-based with congestion control; UDP is connectionless, best-effort, unordered and message-based with an 8-byte header.

Q2Why does DNS use UDP?

Queries and answers are small; UDP avoids a handshake round trip. Resolvers retry on loss and fall back to TCP for large responses or zone transfers.

Q3Why is HTTP/3 built on UDP?

QUIC runs over UDP to avoid TCP head-of-line blocking, combine transport and TLS handshakes and evolve in user space; it adds its own reliability.

Q4How do you check if a UDP service works?

Use the real protocol (dig for DNS, an NTP query, a test log message), ss -ulnp on the server and tcpdump; nc -u cannot confirm a listener.

Q5What happens when you send UDP to a closed port?

The send succeeds locally; the remote host may return ICMP port unreachable, but the sender is not told unless it uses a connected socket and reads later.

Q6What is a UDP amplification attack?

An attacker spoofs the victim's address in small UDP queries to open DNS or NTP servers, which send much larger replies to the victim.