Course contents

02. Networking and Internet Lesson 3 of 10

DNS Resolution

Follow a DNS lookup from the stub resolver through root, TLD and authoritative servers, and see how caching, TTLs and negative caching shape failover.

12 minBeginner8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Your device asks one recursive resolver; the resolver walks root → TLD → authoritative for anything it has not cached.
  • A cold lookup costs several round trips; cached answers cost a few milliseconds or nothing.
  • Each record carries a TTL that bounds how long caches may reuse it, which bounds staleness but does not force freshness.
  • NXDOMAIN answers are cached too, for the negative TTL from the SOA record.
  • DNS changes wait for caches to expire; for fast failover keep a stable address and switch behind it.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

The Domain Name System maps names such as www.example.com to the data clients need to connect: IP addresses (A and AAAA records), mail servers (MX), aliases (CNAME) and the servers responsible for a zone (NS). It is a distributed, hierarchical, heavily cached database. No single server knows every name; each zone's authoritative servers know their own names, and resolvers find and cache the answers.

Why it exists

Packets are routed by IP address, but people and configuration files use names. Names also add a level of indirection: a service can move to new addresses without every client being reconfigured. DNS provides that mapping at internet scale by delegating each part of the namespace to its owner and letting everyone cache what they learn.

How it works

The players.

  • The stub resolver in your operating system (and a cache in the browser) checks local caches and the hosts file, then sends the question to one configured recursive resolver.
  • The recursive resolver (your ISP's, your company's, or a public one such as 1.1.1.1 or 8.8.8.8) does the work. It answers from cache when it can and otherwise queries the hierarchy itself.
  • Root servers know which servers handle each top-level domain. There are 13 root server names, each served by hundreds of anycast instances.
  • TLD servers (for .com, .org, .de, …) know which name servers handle each domain below them.
  • The authoritative servers for example.com hold the actual records.

A cold lookup. The resolver asks a root server for www.example.com. The root answers with a referral: the NS records (and addresses) of the .com servers. The resolver asks a .com server, which refers it to the example.com name servers. The resolver asks one of those and gets the A record, say 203.0.113.10 with a TTL of 300 seconds. It returns that to the stub and caches every record it learned. Towards your device the resolver is recursive (you get a final answer); towards the hierarchy it is iterative (it follows referrals itself). Modern resolvers also use QNAME minimisation, sending each server only the part of the name it needs.

Caching and TTLs. Every record has a TTL in seconds, set by the zone owner. A cache may reuse the record until the TTL runs out, then must ask again. TTLs differ per record: NS records for TLDs typically live for a day or two, while A records for services are often 60–300 seconds. That is why a real resolver almost never asks a root server: the delegation for .com is nearly always cached, and only the short-lived leaf record is re-fetched.

Negative caching. "This name does not exist" (NXDOMAIN) and "this name has no record of that type" (NODATA) are cached as well, for the negative TTL: the smaller of the TTL of the zone's SOA record and the SOA's minimum field (RFC 2308).

Transport. Queries normally use UDP on port 53, falling back to TCP for large answers; DNS over TLS and DNS over HTTPS encrypt the hop between the stub and the resolver.

When to use it

DNS is how clients find almost every public service, and it is the right place for things that change slowly: which addresses or which CDN serve a name, mail routing, ownership proofs (TXT records), and coarse traffic steering such as latency- or geography-based answers. Inside a platform, DNS-based service discovery works well when instances change at most every few minutes.

When not to

  • As a fast failover switch: caches hold old answers for at least the TTL.
  • For precise load balancing: resolvers cache one answer for many users, so traffic follows resolvers, not users, and shifts in coarse steps.
  • For rapidly changing membership (autoscaling instances every few seconds): use a load balancer or a service registry that clients query directly.

Trade-offs

Choice You gain You pay
Long TTL (hours) Fewer queries, faster lookups, resilience if your DNS provider has an outage Changes and failovers take hours to propagate
Short TTL (seconds to a minute) Changes take effect quickly More queries and cost, more lookups that pay a round trip; some resolvers raise very low TTLs
DNS-based steering (geo, latency, health-checked answers) Global traffic control without extra hops Coarse and delayed by caching; clients behind a distant resolver get its location
Public resolver (1.1.1.1, 8.8.8.8) Large shared cache, privacy features Geo-steering may see the resolver's location rather than the user's (EDNS Client Subnet mitigates)

Alternatives

  • Anycast: announce one IP from many locations; the network routes each user to the nearest. Failover happens in routing, without changing DNS.
  • A load balancer or global front door with a stable address: health checks move traffic behind the address within seconds.
  • Service registries (Consul, Kubernetes endpoints, cloud service discovery) for internal services whose instances change often.
  • Hard-coded IPs in tightly controlled environments: no lookup, no indirection, no flexibility.

Failure modes

  • Stale answers after a change: caches keep the old record for its TTL. Browsers, operating systems and runtimes (some application runtimes historically cached lookups for the process lifetime) add their own layers, and open connections never re-resolve.
  • Negative caching surprises: a name looked up before it was created stays "missing" for the negative TTL.
  • Authoritative outage: once cached records expire, resolution fails for everyone. Use more than one DNS provider for critical zones, and do not set TTLs so low that a short provider outage empties every cache.
  • Resolver outage or slowness: every new connection on that network stalls. Clients often have a secondary resolver configured, with a timeout before they try it.
  • Misconfiguration: a wrong NS delegation, a missing glue record or an expired domain takes everything down at once, and the fix itself is subject to TTLs.

In production

Lower TTLs a day or more before a planned migration (at least one old TTL ahead), make the change, then raise them again. Keep critical names on two independent DNS providers. Monitor resolution from several regions and resolvers, not only from inside your network. Create records before announcing them. For fast failover, keep DNS pointing at stable addresses (load balancers, anycast IPs) and fail over behind them; health-checked DNS failover still waits for caches.

Common misconceptions

  • "A TTL of 60 seconds means everyone switches within a minute." It bounds staleness for caches that honour it; some do not, and clients with open connections never ask again.
  • "Every lookup goes to the root servers." Almost none do: TLD delegations are cached for days.
  • "DNS propagation is the new record spreading out." Nothing is pushed. Caches expire and fetch the new answer on their own schedule.
  • "If the name does not resolve yet, I'll just create it." The failed lookup may now be cached for the negative TTL.

Interview angle

Explain the path (stub → recursive → root → TLD → authoritative) and point out that caching makes almost every lookup cheap. Then talk about the operational side: what TTL you would choose and why, how you would migrate a service to new addresses, and why you would not rely on DNS alone for failover. Mentioning negative caching, multiple DNS providers and anycast shows practical experience.

Model assumptions

This lesson is a scripted walk-through, not a simulation: every packet is placed by hand to show the order of queries and answers, and the caches are labels. Latencies are illustrative one-way values (1 ms inside the device, 8 ms to the recursive resolver, 12 ms to a root server, 15 ms to the TLD server, 30 ms to the authoritative server, about 1 ms processing per server). The cold lookup assumes an empty resolver cache, which is rare in practice because root and TLD referrals are cached for a day or two. Real resolvers also retry other servers on timeouts, use TCP or encrypted transports, and may prefetch popular records before they expire.

The lesson as textEvery step of the simulation, written out.
  1. 1. The browser needs an address

    You type www.example.com. The network routes packets by IP address, not by name, so before anything else the browser has to turn the name into an address.

    It checks its own cache first, then asks the operating system’s stub resolver. The stub does almost no work itself: it checks the OS cache and the hosts file, then forwards the question to a recursive resolver, usually your ISP’s or a public one such as 1.1.1.1 or 8.8.8.8, configured by DHCP.

    Scene: The browser asks the OS stub resolver for www.example.com. Its caches miss, so the stub forwards the question to the recursive resolver.

    Key idea: Your device only asks one question, to one recursive resolver. That resolver does the legwork.

  2. 2. Root, TLD, authoritative

    The resolver’s cache is empty, so it starts at the top. It asks a root server, which does not know the answer but replies with a *referral*: “ask the .com servers, here are their addresses”. The .com TLD server refers it on to the servers responsible for example.com. The authoritative server finally answers: an A record with the IP address and a TTL of 300 seconds.

    That is three round trips from the resolver plus one from your device: about 140 ms here. The resolver works *iteratively* (it asks each server itself); towards you it acts *recursively* (you get one final answer).

    Scene: The recursive resolver asks the root server, then the .com TLD server, then the example.com authoritative server, and passes the A record back to the browser. The whole lookup takes about 140 ms.

    Key idea: A cold lookup walks the hierarchy: root → TLD → authoritative, one round trip each.

  3. 3. The next user gets it from cache

    The resolver stores every answer for as long as its TTL allows. A minute later another device using the same resolver asks for www.example.com. The resolver answers from its cache straight away: about 17 ms, and not a single packet goes past it.

    A big resolver serves millions of users, so popular names are almost always cached. Your own browser and OS cache the answer too; for them a repeat lookup costs nothing at all.

    Scene: Another device asks the recursive resolver, which answers from its cache in about 17 ms. The root, TLD and authoritative servers are not contacted.

    Key idea: Caching at every level means most lookups never leave the recursive resolver.

  4. 4. Each record has its own TTL

    Five minutes later the A record’s TTL has run out, so the resolver drops it. But the records that say *which servers* handle .com and example.com (NS records) have much longer TTLs, typically one or two days. The resolver still has them, so it goes straight to the authoritative server: one round trip, about 80 ms.

    A TTL is an upper bound on how long a copy may be reused. It limits how stale an answer can get; it does not make anyone fetch a fresh copy earlier.

    Scene: The A record expired, so the resolver asks the authoritative server again, skipping the root and TLD servers because their referrals are still cached. The lookup takes about 80 ms.

    Key idea: TTLs bound staleness per record. Short-lived records are re-fetched; the long-lived delegation above them stays cached.

  5. 5. Caching that a name does not exist

    Someone looks up beta.example.com before you have created it. The authoritative server answers NXDOMAIN (no such name), and that answer is cached too: for the *negative TTL*, the smaller of the SOA record’s TTL and its minimum field, here one hour.

    A few seconds later you add the record. The next device to ask still gets NXDOMAIN, straight from the resolver’s cache, without the authoritative server ever hearing the question. Creating a name right after something looked it up can mean an hour of “it doesn’t exist”.

    Scene: The browser asks for beta.example.com; the authoritative server answers NXDOMAIN and the resolver caches that for an hour. After the record is created, another device asks and still gets NXDOMAIN from the resolver’s cache.

    Key idea: Missing answers are cached as well. Create records before anyone looks them up, and keep the SOA minimum modest.

  6. 6. Moving to a new IP takes a TTL

    The web server at 203.0.113.10 dies. You point the A record at a standby, 198.51.100.7, at the authoritative server. Done?

    Not for anyone whose resolver already cached the old address: it keeps handing out 203.0.113.10 until the 300-second TTL runs out, and the browser keeps connecting to a dead server. Some resolvers enforce a minimum TTL or serve stale answers on purpose; browsers, operating systems and long-running apps keep their own caches and open connections. A TTL is the minimum delay you should plan for, not the maximum.

    Scene: The old web server is offline and the authoritative server already has the new IP, but the resolver still returns the cached old address. The browser asks again, gets the old IP again and fails again.

    Key idea: DNS failover waits for every cache between you and the user. The TTL bounds the typical delay; misbehaving caches can extend it.

  7. 7. Plan the change: lower the TTL first

    For a planned move you lower the TTL before the change, say to 60 seconds a day ahead (at least one old TTL earlier), so every cache picks up the short TTL. After the switch, caches expire within about a minute; the resolver asks the authoritative server again and gets the new address.

    The cost: five times more queries to your authoritative servers and more lookups that pay the 80 ms round trip. For *unplanned* failover, teams put a stable address in front instead, a load balancer or an anycast IP, and fail over behind it, where no cache is involved.

    Scene: With a 60-second TTL the cached old address expires quickly. The resolver asks the authoritative server, gets 198.51.100.7, and the browser reaches the new web server.

    Key idea: Short TTLs make DNS changes faster at the price of more queries and slower lookups; for instant failover, keep the IP stable and switch behind it.

  8. 8. What to remember about DNS

    A lookup is one question to a recursive resolver, which walks root → TLD → authoritative only for what it has not cached. Caches at the browser, the OS and the resolver make most lookups cost a few milliseconds or nothing.

    The same caches make changes slow: positive answers live for their TTL, negative answers for the SOA minimum, and some caches hold on longer than they should. Treat DNS as a slowly-changing directory, not a fast switch.

    Scene: The full picture: browser, OS stub resolver, recursive resolver, root, TLD and authoritative servers, another device sharing the resolver, and the old (offline) and new web servers.

    Key idea: DNS is fast because it caches, and slow to change for exactly the same reason.