UDP
User Datagram Protocol
Fast, simple, no promises.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
UDP adds just two things to IP: port numbers, so a datagram reaches the right program, and an optional checksum. No connection, no retransmission, no ordering, no congestion control.
That makes it ideal when speed matters more than perfection, or when the application wants to do its own recovery: DNS, DHCP, video calls, games, and QUIC, the transport under HTTP/3.
1.2Key ideas
The handful of ideas everything else follows from.
Message boundaries
UDP keeps each datagram intact. One send is one receive, unlike TCP’s byte stream.
The application owns reliability
DNS simply retries. Games send the latest state again. QUIC builds its own loss recovery on top.
Pseudo-header checksum
The checksum includes IP addresses, so a datagram delivered to the wrong host is detected.
Amplification
Attackers can forge the source, so a small request that triggers a large reply enables DDoS. Servers limit response size.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why does DNS use UDP?
- What does UDP add to IP?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
The client sends a single self-contained message. Nothing was set up beforehand.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
The whole UDP header is 8 bytes. Here it carries the DNS query from the previous lesson.
Ephemeral port chosen by the OS. Replies come back to it.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Source port | 0 | 2 | cd 86 | |
| Destination port | 2 | 2 | 00 35 | |
| Length | 4 | 2 | 00 25 | |
| Checksum | 6 | 2 | c4 f3 | |
| Payload | 8 | 29 | 1a 2b 01 00 00 01 00 00 … |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Decoding a UDP datagram
The whole header is 8 bytes, and it includes no delivery guarantee. Reliability is the application's job.
1export function parseUdp(b: Uint8Array) {2 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);3 const length = view.getUint16(4); // header plus payload4 if (length < 8 || length > b.length) throw new Error("bad UDP length");5 return {6 sourcePort: view.getUint16(0),7 destinationPort: view.getUint16(2),8 length,9 payload: b.subarray(8, length),10 };11}12 13/** UDP gives no retries, so the application adds its own. Resend until answered, then give up. */14export async function queryWithRetry<T>(send: (attempt: number) => Promise<T | null>, attempts = 3): Promise<T | null> {15 for (let attempt = 0; attempt < attempts; attempt++) {16 const reply = await send(attempt);17 if (reply !== null) return reply;18 }19 return null;20}length < 8- Never trust a length field from the network.
queryWithRetry- What a DNS client does: resend until answered, then give up.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why does DNS use UDP?
What does UDP add to IP?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- Datagrams larger than the path MTU get fragmented, and networks often drop them.
- NAT and firewalls expire UDP “connections” after seconds, so long-lived flows need keepalives.
- Never assume ordering or delivery.