A lookup from start to finish
Step by step: resolving api.example.com cold
Assume every cache is empty.
- Application → stub resolver: the app calls
getaddrinfo("api.example.com"). - Local checks: per
/etc/nsswitch.conf(hosts: files dns), the OS checks/etc/hostsfirst, then any local cache (systemd-resolved, nscd). The browser checked its own cache even before asking the OS. - Stub → recursive resolver: a recursive query (RD flag set) goes over UDP 53 to the resolver in
/etc/resolv.conf, e.g. the VPC resolver10.0.0.2. - Recursive → root: the resolver starts from its root hints (built-in list of root server addresses) and asks a root server about
api.example.com(modern resolvers with QNAME minimisation ask only forcom.). The root replies with a referral: the NS records forcom.plus their addresses (glue). - Recursive → TLD: it asks a
.comserver (e.g.a.gtld-servers.net), which replies with a referral: the NS records forexample.com(e.g. fourawsdnsservers), plus glue if needed. - Recursive → authoritative: it asks one of
example.com's name servers, which returns the authoritative answer (AA flag):api.example.com. 300 IN A 203.0.113.10. If the answer is a CNAME, the resolver restarts the process for the target name. - Caching: the resolver caches the A record (300 s), the
example.comNS records, the.comNS records (2 days) — each per its TTL. - Answer to the stub, which hands the IP to the app. The app opens a TCP connection.
This is the distinction interviewers want: the client's query is recursive ("give me the final answer"); the resolver's queries are iterative ("tell me what you know, or who to ask next").
Phone-book analogy: you ask a librarian (recursive resolver) for a number. The librarian checks the master index (root) to find which directory covers .com, opens that directory (TLD) to find which book covers example.com, then reads the number from that book (authoritative) — and remembers it for next time.
The warm path
The second lookup of api.example.com from any client of the same resolver returns from cache in about a millisecond. A lookup of www.example.com skips root and TLD (NS for example.com are cached) and goes straight to the authoritative server. Popular names are almost always cached in big resolvers — which is why DNS scales to the whole internet.
See it yourself
$ dig +trace api.example.com
. 518400 IN NS a.root-servers.net. ← from root hints
...
com. 172800 IN NS a.gtld-servers.net. ← referral from a root server
...
example.com. 172800 IN NS ns-1234.awsdns-12.org. ← referral from a .com server
example.com. 172800 IN NS ns-567.awsdns-07.net.
...
api.example.com. 300 IN A 203.0.113.10 ← answer from Route 53 (authoritative)
;; Received 59 bytes from 205.251.196.210#53(ns-1234.awsdns-12.org) in 12 ms
dig +trace performs the iterative walk itself (not using your resolver's cache) — perfect for spotting a broken delegation at a specific level.