Opscraft
How DNS works behind the scenes · Lesson 2 of 9 · 4 min read

A lookup from start to finish

Step by step: resolving api.example.com cold

Assume every cache is empty.

  1. Application → stub resolver: the app calls getaddrinfo("api.example.com").
  2. Local checks: per /etc/nsswitch.conf (hosts: files dns), the OS checks /etc/hosts first, then any local cache (systemd-resolved, nscd). The browser checked its own cache even before asking the OS.
  3. Stub → recursive resolver: a recursive query (RD flag set) goes over UDP 53 to the resolver in /etc/resolv.conf, e.g. the VPC resolver 10.0.0.2.
  4. 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 for com.). The root replies with a referral: the NS records for com. plus their addresses (glue).
  5. Recursive → TLD: it asks a .com server (e.g. a.gtld-servers.net), which replies with a referral: the NS records for example.com (e.g. four awsdns servers), plus glue if needed.
  6. 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.
  7. Caching: the resolver caches the A record (300 s), the example.com NS records, the .com NS records (2 days) — each per its TTL.
  8. 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").

Enlarge diagramApp + stubRecursiveresolverRoot.com TLDRoute 53authoritativeapi.example.com?api.example.com?ask the .com serversapi.example.com?ask ns-1234.awsdns…api.example.com?203.0.113.10, TTL 300caches every answer203.0.113.10
Figure 1. A cold lookup of api.example.com. The client asks once and waits (recursive); the resolver asks each level in turn and follows the referrals (iterative).
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramcoldroot.comauthoritative~100 ms or morewarmfrom cache: ~1 msA lookup of www.example.com can skip root and .com too: those NS records are already cached.
Figure 2. Caching is what makes DNS fast. After the first lookup, the resolver answers from memory until the TTL runs out.
Swipe sideways to see all of it, or tap to open it full screen.

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.