What UDP is
UDP (User Datagram Protocol) is the "just send it" transport. No connection setup, no tracking, no acknowledgements. You hand it a message and it fires it off. Its unit of data is called a datagram.
You have already used UDP many times in this course without seeing it:
| Where | Port | What was sent over UDP |
|---|---|---|
| DNS (chapter 1) | 53 | "What is the IP of api.kade.lk?" and the answer |
| DHCP (chapter 3) | 67, 68 | The four DORA messages, including the broadcast Discover |
| traceroute (chapter 13) | 33434+ | The probes on macOS and Linux |
| STUN (chapter 10) | 3478 | "What is my public ip:port?" for hole punching |
Connectionless
UDP is connectionless: no handshake happens before data flows, and neither side keeps any state about "a conversation". Each datagram stands alone. If one is lost, UDP does not notice and does not resend it. That is the application's problem to handle, or to ignore.
Wide by design - scroll it sideways.
What UDP promises (almost nothing)
Datagrams may be lost, duplicated or arrive out of order, and UDP fixes none of it:
| Feature | UDP | Meaning |
|---|---|---|
| Guaranteed delivery | No | A lost datagram is simply gone |
| Ordering | No | They arrive in whatever order the network delivers them |
| Retransmission | No | Nobody resends anything |
| Duplicate removal | No | If the network delivers one twice, the app gets it twice |
| Flow and congestion control | No | UDP sends as fast as the app asks, even if the receiver or the network cannot keep up (section 6) |
| Error detection | Yes | A checksum lets the receiver throw away damaged datagrams (chapter 15) |
| Message boundaries | Yes | One send = one datagram = one receive (section 5) |
| Speed and low overhead | Yes | The whole point |
Try it. The simulator sends 12 numbered datagrams across a network that loses some, delays some and occasionally duplicates one. Watch what arrives:
Press Send.
Why it is fast
A UDP header is just 8 bytes. TCP's is at least 20, and TCP also needs a handshake before the first byte of data, plus acknowledgements and state on both sides. Less bookkeeping means less waiting:
| UDP | TCP | |
|---|---|---|
| Header size | 8 bytes | 20-60 bytes |
| Before the first data can be sent | nothing | 1 full round trip for the handshake (~41 ms from home to kade-api) |
| When one piece is lost | the rest keep flowing | later data waits until the lost piece is resent |
| State kept per conversation | none | sequence numbers, buffers, timers on both sides |
Messages keep their shape
A detail that changes how you write code: UDP keeps message boundaries. If the app sends a 100-byte message and then a 50-byte one, the receiver gets exactly two reads: 100 bytes, then 50. TCP is a byte stream: it may deliver the same 150 bytes as one read of 150, or 3 reads of 50. With TCP, the app must mark where each message ends (for example with a length prefix or a newline). With UDP, each datagram is one message.
UDP: datagrams
send(100) send(50)
→ recv 100 · recv 50
TCP: byte stream
send(100) send(50)
→ recv 150, or 60 + 90, or …
When to use UDP
UDP is the right choice when speed matters more than perfection, or when the application handles reliability itself. In the Kadé world:
| # | Use | Why UDP fits | Kadé example |
|---|---|---|---|
| 1 | Voice and video calls | A dropped frame is better than a laggy one | A support call between a shopper and Kadé's help desk |
| 2 | Live position updates | Only the latest position matters; an old lost one is useless | The delivery rider's app sending GPS every 2 seconds |
| 3 | Online games | Same: the current state beats a late old one | - |
| 4 | DNS | One small question, one small answer; just ask again if lost | Every lookup of api.kade.lk |
| 5 | Live streaming | Keep playing; do not stall to resend | A live product launch video |
| 6 | Broadcast and multicast | Only UDP can send to many at once; TCP is always one-to-one | DHCP Discover (chapter 3) |
| 7 | Small, frequent reports | Losing one metric point does not matter | kade-api sending metrics (StatsD), syslog, NTP time sync |
Building reliability on top of UDP
"Unreliable" does not mean "useless". Many protocols add just the reliability they need on top of UDP, and nothing more:
| DNS | No answer within a timeout (often 1-5 s)? Ask again, or ask another server. That is all it needs. |
| RTP (voice/video) | Adds a sequence number and a timestamp to each packet. The receiver's jitter buffer holds packets for a few tens of milliseconds to put them back in order and smooth out uneven arrival, then plays them. Late or missing packets are skipped. |
| Games | Send the full current state often, so a lost update is replaced by the next one. |
| QUIC (HTTP/3) | A complete reliable, encrypted transport built on UDP. It fixes TCP's head-of-line blocking by keeping separate streams independent. Modern browsers already use it with Google and Cloudflare, often on UDP port 443. |
Size limits and fragmentation
The UDP length field (chapter 15) allows datagrams up to 65,535 bytes, so the most data one can carry over IPv4 is 65,507 bytes (65,535 minus 8 bytes UDP header, minus 20 bytes IP header). But anything bigger than the path MTU (chapter 13) gets split by IP into fragments, and if one fragment is lost, the whole datagram is lost. So real UDP apps keep datagrams small:
| Largest that fits a 1,500 MTU | 1,500 − 20 (IP) − 8 (UDP) = 1,472 bytes of data |
| Inside GCP (MTU 1,460) | 1,432 bytes of data |
| Safe DNS answer size today | about 1,232 bytes; bigger answers are marked "truncated" and the client asks again over TCP |
UDP, NAT, firewalls and attacks
| # | Topic | What happens with UDP |
|---|---|---|
| 1 | NAT timeouts | There is no "connection closed" signal, so NAT rows for UDP just time out, often after 30 seconds (chapter 10). Long-lived UDP apps send a small keepalive every 15-25 seconds, or the reply path disappears. |
| 2 | Firewalls | A stateful firewall treats "a datagram out, then one back on the same 5 values, soon after" as a reply. Anything else inbound needs a rule. In GCP, rules name the protocol: --allow=udp:53. |
| 3 | Spoofed sources | With no handshake, nothing proves the source IP is real. An attacker can send a datagram with a fake source. |
| 4 | Amplification attacks | Send a small request to an open DNS or NTP server with the victim's IP as the fake source. The server sends a much bigger answer to the victim. Thousands of servers doing this at once is a common DDoS. Never run open resolvers. |
UDP or TCP?
| Question | Leans to |
|---|---|
| Must every byte arrive, in order? (payments, orders, files, web pages) | TCP |
| Is late data worthless? (live audio, video, positions) | UDP |
| One tiny question and answer? (DNS, NTP) | UDP |
| Sending to many devices at once? | UDP only option |
| Need reliability without head-of-line blocking? | QUIC on UDP |
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | UDP | Transport that just sends; unit is a datagram | UDP, datagram |
| 2 | Connectionless | No handshake, no state, no resend | connectionless |
| 3 | Promises | No delivery, order, dedupe or flow control; only a checksum | |
| 4 | Speed | 8-byte header, no setup, never waits for lost data | head-of-line blocking |
| 5 | Boundaries | One send = one datagram = one receive | message boundaries, byte stream |
| 6 | Uses | Calls, positions, games, DNS, streaming, broadcast, metrics | multicast |
| 7 | Reliability on top | Apps add only what they need: retries, sequence numbers, QUIC | RTP, jitter buffer, QUIC |
| 8 | Size | Stay under the MTU: ~1,472 bytes of data | fragmentation |
| 9 | Risks | NAT timeouts, spoofing, amplification | amplification attack |
Try it yourself
A DNS query over UDP
- What to look for: The footer line
SERVER: …#53(…) (UDP). - From section: 1. What UDP is
dig example.comThe same query over TCP
- What to look for: Same answer,
(TCP)in the footer, a little slower. - From section: 8. Size limits and fragmentation
dig +tcp example.comYour own UDP sender and receiver
- Steps:
nc -u -l 9999in one terminal,nc -u 127.0.0.1 9999in another - What to look for: Type lines: each one arrives as a datagram. Stop the listener and keep typing: no error, the data just vanishes.
- Runs on: some Linux versions: nc -u -l -p 9999
- From section: 2. Connectionless
nc -u -l 9999nc -u 127.0.0.1 9999Programs listening on UDP
- What to look for: Ports like 53, 68, 123, 5353.
- Runs on: Linux · macOS: lsof -iUDP -n -P
- From section: 6. When to use UDP
ss -ulnpUDP counters
- What to look for: "no socket" (sent to a port nobody listens on), "bad checksum", "buffer full" drops.
- Runs on: macOS · Linux: netstat -su
- From section: 3. What UDP promises (almost nothing)
netstat -s -p udp