4. DNS
4.3 What happens, step by step, when a name is looked up?
In your phone's contacts: You check your own contacts first. Not there? You ask a helpful friend. Your friend doesn't know the number either, but knows where to start: a master directory says "for that kind of number, ask this directory"; that directory says "for that person, call their office"; and the office gives the real number. Your friend tells you, and both of you save it.
In the system:
Figure: your laptop asks one question. The resolver does the walking and remembers the answer for next time.
- Local caches first. The browser checks its own cache, then the operating system's (which also checks the
hostsfile). A hit here means no network at all. - Ask the recursive resolver. Your device sends the question to a recursive resolver: usually your ISP's or your company's, or a public one like Google's
8.8.8.8or Cloudflare's1.1.1.1. It promises to come back with the final answer. - The resolver asks a root server. "Where's
shop.example.com?" The root replies with a referral: "I don't know, but here are the.comservers." - The resolver asks a
.comTLD server. Another referral: "here are the name servers forexample.com" (for examplens1.example.com). - The resolver asks the authoritative server.
ns1.example.comowns the answer: "shop.example.comis203.0.113.20, valid for 300 seconds." - The answer comes back and is cached by the resolver, the operating system and the browser.
Two words interviewers use:
- Recursive query: your device asks once and expects the full answer (steps 2 and 6).
- Iterative queries: the resolver follows the referrals itself, one server at a time (steps 3 to 5).
Why it's usually fast: the resolver serves thousands of users, so popular answers (and the root and .com referrals) are almost always in its cache already. A full walk from scratch is the rare case.
DNS questions usually travel over UDP on port 53, because one small question and answer don't need a connection (Chapter 3, 3.7). Large answers switch to TCP, and encrypted options exist too (DNS over HTTPS and DNS over TLS).
Memory code: "Nobody knows everything; everyone knows who to ask next." Cache, resolver, root, TLD, owner.