UDP · User Datagram Protocol

Fast, simple, no promises.

04 · Transport · IP protocol 17

UDP

User Datagram Protocol

Fast, simple, no promises.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a UDP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why does DNS use UDP?
  2. What does UDP add to IP?

The answers are in part 4.

Part 3 of 5

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.

Client
Server
Datagram :52614 → :53
Datagram :53 → :52614
Step 1 of 2

The client sends a single self-contained message. Nothing was set up beforehand.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

UDP datagram

The whole UDP header is 8 bytes. Here it carries the DNS query from the previous lesson.

0000cd8600350025c4f31a2b010000010000···5·%···+······
001000000000076578616d706c6503636f6d·····example·com
00200000010001·····
FieldSource port
Length2 bytes
Decimal52614

Ephemeral port chosen by the OS. Replies come back to it.

FieldOffsetBytesHex
Source port02cd 86
Destination port2200 35
Length4200 25
Checksum62c4 f3
Payload8291a 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.

udp.tsTypeScriptCopy
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}
20 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why does DNS use UDP?
One small request and one small reply are cheaper without a handshake. It falls back to TCP for large answers.
What does UDP add to IP?
Ports and a checksum.
Part 5 of 5

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.
← ICMPMark as understoodDNS →