05. How DNS Resolution Works

Big picture: Before your browser can talk to api.myapp.com, it must learn which IP address owns that name. DNS (Domain Name System) is the Internet’s phone book β€” a distributed lookup that turns human names into machine addresses.

You use DNS on every request, but it often sits as a tiny box labeled β€œDNS” on HLD diagrams. This lesson explains what actually happens, so you can debug outages, design multi-region failover, and talk about TTL / caching in interviews.

Prerequisites: 04. Clients, Servers & APIs
Read next: 06. HTTP & the Network Path β€” TCP, TLS, and HTTP after DNS returns an IP.


Learning goals

By the end of this lesson you will be able to:

  • Explain why the Internet needs DNS (names vs IP addresses)
  • Trace a full recursive DNS lookup step by step
  • Name the main record types (A, AAAA, CNAME, NS, MX, TXT)
  • Explain TTL and why DNS feels β€œslow to update”
  • Place DNS correctly in a request path: Client β†’ DNS β†’ LB β†’ App
  • Describe DNS caching (browser, OS, resolver, authoritative)
  • Use DNS for failover / geo routing at a high level
  • Avoid common DNS interview mistakes

Why DNS exists

Computers route packets with IP addresses (203.0.113.42). Humans remember names (www.zomato.com).

Without DNSWith DNS
Hard-code IPs in every appChange servers without rewriting every bookmark
One IP foreverMove api.myapp.com to a new LB IP overnight
No brandingShareable, memorable URLs

Phone book analogy

Phone bookDNS
Person’s nameDomain name (shop.example.com)
Phone numberIP address
Local address book on your phoneBrowser / OS DNS cache
Calling 192 directory assistanceRecursive resolver (often your ISP or 1.1.1.1 / 8.8.8.8)
Official city phone registryAuthoritative name servers for the domain

You don’t call every registry yourself every time β€” you ask a helper who looks it up and remembers the answer for a while (TTL).


Where DNS sits in a request

Interview phrase: β€œDNS resolves the hostname to an IP; then TCP/TLS and HTTP happen against that IP (usually a load balancer).”

If DNS fails, nothing else matters β€” users see β€œserver not found,” not a 500 from your API.


Anatomy of a domain name

https://api.shop.example.com/v1/orders
         β”‚   β”‚    β”‚       β”‚
         β”‚   β”‚    β”‚       └─ top-level domain (TLD): .com
         β”‚   β”‚    └───────── registered domain: example
         β”‚   └────────────── subdomain: shop
         └────────────────── host / further label: api

DNS is a tree:

.  (root)
β”œβ”€β”€ com.
β”‚   └── example.com.          ← you buy this
β”‚       β”œβ”€β”€ www.example.com.
β”‚       └── api.example.com.
└── org.

Each zone has authoritative name servers that answer for that slice of the tree.


The resolution path (beginner walkthrough)

When you first visit www.example.com with an empty cache:

Step-by-step in plain English

  1. Browser cache β€” Did we resolve this recently? If yes, done.
  2. OS / stub resolver β€” Check local cache / hosts file.
  3. Recursive resolver (ISP, Cloudflare 1.1.1.1, Google 8.8.8.8) β€” Does it have a cached answer?
  4. If not, resolver asks the root β†’ TLD β†’ authoritative servers (as above).
  5. Resolver returns the IP to your device and caches it for TTL seconds.
  6. Browser opens TCP (and TLS) to that IP.

You almost never talk to root servers yourself β€” the recursive resolver does the walking.

Iterative vs recursive (vocab)

StyleWho does the work?
Recursive queryClient asks resolver: β€œGive me the final answer.” Resolver does the full chase.
Iterative queryResolver asks each NS: β€œWho next?” NS returns a referral, not always the final A record.

Browsers use recursive resolvers. Authoritative servers usually answer iteratively (referrals or final data for their zone).


Common DNS record types

TypeMeaningExample use
AName β†’ IPv4 addressapi.myapp.com β†’ 203.0.113.10
AAAAName β†’ IPv6 addressSame host on IPv6
CNAMEName β†’ another namewww β†’ myapp.com or cdn β†’ xyz.cloudfront.net
NSWho is authoritative for this zoneexample.com NS ns1.registrar.com
MXMail serversEmail delivery (Email at Scale)
TXTArbitrary textSPF, domain verification, ACME challenges
SRVService locationSome discovery protocols

CNAME gotcha (interview favorite)

A CNAME means β€œthis name is an alias of that name.” Typical pattern:

www.example.com  CNAME  example.com
example.com      A      203.0.113.10

You generally should not put a CNAME on the apex (example.com) in classic DNS (conflicts with NS/MX). Many CDNs offer β€œANAME/ALIAS” flattening as a workaround β€” awareness level is enough.


TTL β€” why DNS changes feel slow

TTL (Time To Live) = how many seconds a resolver may cache the answer before asking again.

api.myapp.com.  60  IN  A  203.0.113.10
                ↑
              TTL = 60 seconds
TTL choiceProsCons
Long (1 hour–1 day)Less DNS traffic, faster repeat lookupsFailover / IP change takes longer to spread
Short (30–60 s)Faster failover and blue/green cutsMore queries to authoritative / resolver

Sale / launch tip: Lower TTL before a planned cutover, wait for old TTLs to expire, then change the record. Raising TTL again afterward reduces load.

DNS is not instant global push β€” it is cache expiry. That is why β€œI updated DNS but some users still hit the old IP” is normal for a while.


Caching layers (all of them matter)

Browser cache
  β†’ OS cache
    β†’ Recursive resolver cache (ISP / 1.1.1.1)
      β†’ (on miss) Authoritative name servers

A change is β€œlive” for a user only after every cache in their path expires or is flushed.

Negative caching: Resolvers also cache β€œthis name does not exist” (NXDOMAIN) for a while β€” mistyped domains can β€œstick” briefly.


DNS in system design diagrams

Minimum viable HLD snippet

What the DNS box usually points at

TargetTypical record
Cloud load balancerA / ALIAS to LB hostname
CDNCNAME to d123.cloudfront.net
Multi-regionGeo DNS or latency-based routing to regional LBs

DNS usually does not point at a single EC2 IP in production β€” it points at something that can move: LB or CDN.


DNS for high availability (awareness)

TechniqueIdea
Multiple A recordsSeveral IPs; clients try another if one fails (limited control)
Health-checked DNS failoverRoute53 / similar removes unhealthy endpoints
Geo / latency routingUsers in IN get Mumbai LB; US get Virginia LB
AnycastSame IP announced from many PoPs (CDN/DNS providers)

Related deeper reading: Load Balancing β€” global LB, DDoS mitigation.

Caveat: DNS failover is slow compared with LB health checks (TTL + client caching). Prefer LB-level failover for instance death; use DNS for region or provider level moves.


Public vs private DNS

KindWho uses itExample
PublicInternet userswww.myapp.com
Private (VPC)Internal servicesorders.prod.internal β†’ private IPs

Microservices often resolve internal names via cloud private DNS or service discovery (Consul, Kubernetes DNS) β€” same idea (name β†’ address), different trust boundary.


Security awareness (short)

TopicOne-liner
DNS spoofing / cache poisoningAttacker feeds a resolver a fake IP
DNSSECCryptographic signatures on DNS data (adoption uneven)
DoH / DoTDNS over HTTPS/TLS β€” encrypts lookups to the resolver
Domain hijackingAttacker controls registrar β†’ points your name elsewhere

For interviews: know that compromising DNS is catastrophic (users send traffic to the attacker’s IP while the URL still looks correct).


Debugging cheatsheet

SymptomLikely layer
NXDOMAIN / β€œserver not found”Typo, missing record, registrar issue
Some users OK, some notTTL / cache propagation
Wrong site / cert mismatchCNAME to wrong host; leftover old A record
Works after flushdnsLocal OS/browser cache
App up but name failsDNS outage β‰  app outage

Handy tools (awareness): dig, nslookup, host.

dig api.myapp.com A +short
dig api.myapp.com NS

Worked mini-example: URL shortener HLD

Users type: https://short.io/abc123

1. DNS: short.io β†’ 203.0.113.8 (LB)
2. TLS to LB
3. LB β†’ app
4. App: cache/DB lookup abc123 β†’ 302 to long URL

If you skip DNS in the verbal walkthrough, interviewers may prompt: β€œHow does the client find your servers?” Have this answer ready in 15 seconds.


Check your understanding

  1. Why don’t we hard-code IP addresses in mobile apps for production APIs?
  2. Order the lookup: authoritative NS, browser cache, root, recursive resolver, TLD.
  3. What does TTL control? Why lower it before a cutover?
  4. A vs CNAME β€” when use each?
  5. Why is DNS failover slower than load-balancer failover?
  6. Where does DNS sit relative to the load balancer in an HLD?
Answers
  1. IPs change (new LB, migration, failover). Names stay stable; DNS updates the mapping.
  2. Browser cache β†’ recursive resolver (its cache) β†’ root β†’ TLD β†’ authoritative β†’ answer back.
  3. How long resolvers may reuse an answer. Lower TTL so clients pick up the new IP faster after you change it.
  4. A maps a name to an IP. CNAME aliases one name to another name (often CDN/LB hostname).
  5. Clients and resolvers cache DNS; LB health checks remove bad backends in seconds without waiting for TTL.
  6. Before the LB: Client β†’ DNS β†’ LB IP β†’ apps.

Common beginner mistakes

MistakeBetter
Forgetting DNS on the HLDAlways show Client β†’ DNS β†’ LB
Pointing apex at a single VM IPPoint at LB/CDN
TTL = 1 day, then emergency failoverPlan short TTL before risky changes
β€œDNS is always instant”Explain cache + TTL
Confusing DNS with load balancingDNS finds an IP; LB spreads across many servers

Recap

  • DNS translates names β†’ IPs using a hierarchical, cached lookup.
  • Path: browser/OS β†’ recursive resolver β†’ root β†’ TLD β†’ authoritative.
  • TTL controls cache duration β€” the main lever for cutover speed.
  • Know A, AAAA, CNAME, NS, MX, TXT.
  • In HLD: DNS usually targets a load balancer or CDN, not one app box.
  • DNS failover helps at region scale; instance health is the LB’s job.

Next: HTTP & the Network Path β€” TCP, TLS, HTTP versions, and latency budgets after DNS.

Then: Load Balancing β€” what happens when many connections hit one IP.