DNS
Domain Name System
The internet’s phone book.
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.
People remember names, networks route addresses. DNS translates example.com into 93.184.216.34. It is a distributed database: no single server knows everything, but each knows where to ask next.
Your device asks a recursive resolver. If the answer is not cached, the resolver walks the hierarchy: root servers, then the .com servers, then example.com’s own authoritative servers. Resolvers cache the answer for its TTL.
1.2Key ideas
The handful of ideas everything else follows from.
Records
A and AAAA give addresses, CNAME an alias, MX mail servers, TXT arbitrary text (SPF, DKIM), NS the delegated name servers.
TTL trades freshness for speed
Short TTLs allow fast changes and cost more queries. Long TTLs are cheap and slow to update.
Name compression
A two-byte pointer replaces repeated names, pointing back to the first occurrence.
Security
Plain DNS is unauthenticated. DNSSEC signs answers. DoH and DoT encrypt the transport.
EDNS0
Extends UDP size beyond 512 bytes and carries options such as client subnet.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- What is the difference between recursive and authoritative servers?
- Why does the QNAME have no dots on the wire?
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 browser or OS asks the configured resolver, usually learned from DHCP.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
A recursive question for the A record of example.com, sent as a UDP datagram to port 53.
Random 16-bit ID. The reply must echo it. Predictable IDs enable cache poisoning.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Transaction ID | 0 | 2 | 1a 2b | |
| Flags | 2 | 2 | 01 00 | |
| Questions | 4 | 2 | 00 01 | |
| Answer RRs | 6 | 2 | 00 00 | |
| Authority RRs | 8 | 2 | 00 00 | |
| Additional RRs | 10 | 2 | 00 00 | |
| QNAME | 12 | 13 | 07 65 78 61 6d 70 6c 65 … | |
| QTYPE | 25 | 2 | 00 01 | |
| QCLASS | 27 | 2 | 00 01 |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Encoding and decoding DNS
This parses the Query and Response on this page, including the compression pointer.
1/** A name goes on the wire as length-prefixed labels, ending in a zero byte. */2export function encodeName(name: string): number[] {3 const out: number[] = [];4 for (const label of name.split(".")) out.push(label.length, ...new TextEncoder().encode(label));5 out.push(0);6 return out;7}8 9/** Reads a name. Two high bits set means a pointer to an earlier name, which saves space. */10export function readName(b: Uint8Array, start: number): { name: string; next: number } {11 const labels: string[] = [];12 let i = start;13 let next = -1;14 for (let jumps = 0; jumps < 32; jumps++) {15 const len = b[i];16 if (len === 0) return { name: labels.join("."), next: next === -1 ? i + 1 : next };17 if ((len & 0xc0) === 0xc0) {18 if (next === -1) next = i + 2; // the record continues after the first pointer19 i = ((len & 0x3f) << 8) | b[i + 1];20 } else {21 labels.push(new TextDecoder().decode(b.subarray(i + 1, i + 1 + len)));22 i += 1 + len;23 }24 }25 throw new Error("pointer loop in DNS name"); // a hostile packet could otherwise hang the parser26}27 28export function parseDns(b: Uint8Array) {29 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);30 const flags = view.getUint16(2);31 let i = 12;32 const questions = [];33 for (let n = view.getUint16(4); n > 0; n--) {34 const { name, next } = readName(b, i);35 questions.push({ name, type: view.getUint16(next) });36 i = next + 4;37 }38 const answers = [];39 for (let n = view.getUint16(6); n > 0; n--) {40 const { name, next } = readName(b, i);41 const type = view.getUint16(next);42 const rdLength = view.getUint16(next + 8);43 const data = b.subarray(next + 10, next + 10 + rdLength);44 answers.push({ name, type, ttl: view.getUint32(next + 4), address: type === 1 ? [...data].join(".") : null });45 i = next + 10 + rdLength;46 }47 return { id: view.getUint16(0), isResponse: (flags & 0x8000) !== 0, wantsRecursion: (flags & 0x0100) !== 0, rcode: flags & 0x0f, questions, answers };48}out.push(label.length, ...)- Names go on the wire as length-prefixed labels, with no dots.
(len & 0xc0) === 0xc0- Two high bits mark a pointer to a name earlier in the packet.
next === -1- The record continues after the first pointer, not after the place it points to.
jumps < 32- A hostile packet can point at itself. Without this limit, the parser would hang.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
What is the difference between recursive and authoritative servers?
Why does the QNAME have no dots on the wire?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- “It’s always DNS” is a joke because a wrong record or a stale cache looks like an outage.
- Changing a record does not take effect everywhere until old TTLs expire.
- A CNAME cannot coexist with other records at the same name, which is why the zone apex needs ALIAS tricks.