DNS RESOLUTION

The distributed phone book of the internet

L215 MINNETWORKING
PREDICT

api.example.com has a TTL (how long resolvers may cache it) of 86400 seconds. At noon you change its A record and switch the old server off. A resolver cached the old record at 11:00. When does it start returning the new address?

A client asks a recursive resolver. On a miss, the resolver asks a root server, which refers it to the .com servers, which refer it to the domain’s authoritative server, which answers. The resolver caches each answer for the TTL the authority set and answers later clients from cache.

The cache is the point and the risk. A resolver keeps an old record for its full TTL, whatever you change upstream. It also accepts the first response that matches its outstanding query by transaction ID, port and name. If those are guessable, an off-path attacker can answer first, and the forgery is cached for everyone.

▶ SIMULATION
CLIENTS
last answer —
answers 0 · stale past 300 s 0 · forged 0
RECURSIVE RESOLVER (random port + ID)
api.example.com: empty
.com NS: empty
outstanding: none · upstream queries 0
ns1.example.com
api A 198.51.100.10 TTL 3600
no change yet
clockt = 0 s
last lookup—
lastresolver cache empty
✓ no client is sent to the old address more than 300 s after a change✓ no client is handed a forged answer
SCHEDULE
01[RESOLVER]resolver: random source port + random ID; record TTL 3600 s
02[CLIENT]@0 s: client resolves api.example.com
03[AUTH]ns1.example.com: the real answer arrives
04[CLIENT]@600 s: client resolves api.example.com
05[CLIENT]@4000 s: client resolves api.example.com
06[AUTH]ns1.example.com: the real answer arrives
07[CLIENT]report
Start of cold → warm cache scenario. 7 steps.
// resolver
const e = cache.get(name)
if (e) return e.addr                        // ignores expiry
send(query, { srcPort: 53, id: nextId++ })   // guessable
const r = await firstResponse()             // accepts anyone's
cache.set(name, { addr: r.addr, expires: now + r.ttl })

Watch the RESOLVER box and the invariant strip. COLD → WARM CACHE makes 3 upstream queries, then 0, then 1 after the TTL expires, because the .com delegation is still cached. MIGRATION, TTL 1 DAY sends a client to the switched-off address 2600 s after the change. LOWER TTL FIRST makes the same change with no stale answer. POISONING lands a forgery on the first guess against port 53 and sequential IDs. RANDOM PORT + ID survives 100 forgeries that now have to guess both the port and the ID.

BREAK IT NOT YET

The record has a 3600 s TTL. Inject one fault so that a client is sent to an address that has been switched off for more than 5 minutes, or to a forged one.

GOAL: a client gets a dead or forged address

FIX IT NOT YET

Make resolve honour the TTL (an entry is valid while now < expires) and accept only a response that matches the outstanding query: same ID, same port, same name. If nothing matches, return null and cache nothing.

Mental model:

a TTL is a promise you make before the change: every resolver may keep what you told it for that long, and you cannot take it back. A resolver trusts the first answer that matches its question. Make that match hard to guess (random port and ID) or impossible to forge (DNSSEC), and plan TTLs before a migration, not during it.

IN PRODUCTION

Cutovers and failovers that “take a while to propagate”, dig +trace and the TTL column in dig output, CDN and load balancer records with 60 s TTLs, JVMs and other clients that cache DNS longer than the TTL, and DNSSEC validation in resolvers like Unbound and 1.1.1.1.

IN INTERVIEWS

“What happens when you type a URL?” (the DNS part) and “we changed DNS and some users still hit the old server, why?” Walk root → TLD → authoritative, explain caching per TTL, give the lower-TTL-first migration plan, and describe cache poisoning: what has to be guessed, why randomization helps, and what DNSSEC adds.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: NAT TRAVERSAL →