$ man 7 udp # RFC 768, 1980

UDP

UDP (User Datagram Protocol) sends independent messages, called datagrams, from one port to another with no connection, no acknowledgments, no retransmission and no ordering. Each datagram either arrives whole or not at all. That makes UDP fast and simple, and it leaves reliability to the application, if the application wants it.

TCP and UDP under the same packet loss

$ send 3 messages, 1 gets lost
TCP and UDP, same network, same loss
TCP CLIENT TCP SERVER UDP CLIENT UDP SERVER SYN SYN-ACK ACK datagram #1 ✕datagram #2 datagram #3 done, no waiting got #1, #3 seg 1 ✕seg 2 seg 3 ACK: still waiting for seg 2 retransmit seg 2 1, 2, 3 in order #2 is gone for good
  1. Left: TCP. Right: UDP. Each side sends three messages over the same lossy network.
  2. TCP spends a round trip on the handshake before any data. UDP just sends: three datagrams, and #2 is lost.
  3. UDP is already finished. The receiver got #1 and #3. Nothing tells either side that #2 is missing.
  4. TCP sends its three segments. Segment 2 is lost too.
  5. The receiver keeps ACKing only up to the gap (a duplicate ACK), so after a timeout or repeated duplicate ACKs the sender retransmits segment 2. Segment 3 waits in a buffer meanwhile.
  6. TCP delivers all three, in order, but later. UDP delivered two, immediately. Which is better depends on the app.

The whole header is 8 bytes

Field Size Meaning
Source port 16 bits Where replies go
Destination port 16 bits Which program receives it (53 = DNS)
Length 16 bits Header + data, in bytes
Checksum 16 bits Detects corruption (mandatory in IPv6)

Compare that with TCP’s 20 to 60 bytes of sequence numbers, ACKs, windows and flags. Because there is no connection, the server keeps no per-client state, which is why one DNS server can answer millions of clients.

TCP vs UDP

TCP UDP
Connection Handshake first None, send immediately
Delivery Guaranteed or the connection fails Best effort
Ordering In order Any order
Message boundaries No: a byte stream Yes: one send = one datagram
Head-of-line blocking Yes No
Typical use Databases, HTTP/1.1 and HTTP/2, Kafka, SSH DNS, video calls, games, metrics (StatsD), QUIC / HTTP/3

When UDP is the right choice

  • Small request, small reply. A DNS lookup fits in one datagram each way. Over TCP, the handshake alone would double the latency.
  • Fresh beats complete. In a voice call, a packet that arrives 300 ms late is useless. Skip it and play the next one.
  • You want to build your own reliability. QUIC, the transport under HTTP/3, runs over UDP and implements streams, retransmission and TLS 1.3 itself. Losing one stream’s packet no longer blocks the others, and it can be updated in user space instead of the OS kernel.
  • Fire-and-forget telemetry. StatsD and many syslog setups send metrics over UDP so a slow collector never slows the app.

Common mistakes

  • Assuming UDP delivery is “usually fine”. On a busy host, receive buffers overflow and datagrams are dropped silently. Watch netstat -su for receive errors.
  • Sending datagrams larger than the path MTU. Anything above roughly 1,200 to 1,400 bytes may be fragmented or dropped. DNS falls back to TCP for large answers for this reason.
  • Forgetting UDP is easy to spoof. No handshake means the source IP is not verified, which enables reflection and amplification DDoS attacks.

Learning Resources

See also: TCP/IP, DNS.