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 DNS | With DNS |
|---|---|
| Hard-code IPs in every app | Change servers without rewriting every bookmark |
| One IP forever | Move api.myapp.com to a new LB IP overnight |
| No branding | Shareable, memorable URLs |
Phone book analogy
| Phone book | DNS |
|---|---|
| Personβs name | Domain name (shop.example.com) |
| Phone number | IP address |
| Local address book on your phone | Browser / OS DNS cache |
| Calling 192 directory assistance | Recursive resolver (often your ISP or 1.1.1.1 / 8.8.8.8) |
| Official city phone registry | Authoritative 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
- Browser cache β Did we resolve this recently? If yes, done.
- OS / stub resolver β Check local cache /
hostsfile. - Recursive resolver (ISP, Cloudflare
1.1.1.1, Google8.8.8.8) β Does it have a cached answer? - If not, resolver asks the root β TLD β authoritative servers (as above).
- Resolver returns the IP to your device and caches it for TTL seconds.
- 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)
| Style | Who does the work? |
|---|---|
| Recursive query | Client asks resolver: βGive me the final answer.β Resolver does the full chase. |
| Iterative query | Resolver 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
| Type | Meaning | Example use |
|---|---|---|
| A | Name β IPv4 address | api.myapp.com β 203.0.113.10 |
| AAAA | Name β IPv6 address | Same host on IPv6 |
| CNAME | Name β another name | www β myapp.com or cdn β xyz.cloudfront.net |
| NS | Who is authoritative for this zone | example.com NS ns1.registrar.com |
| MX | Mail servers | Email delivery (Email at Scale) |
| TXT | Arbitrary text | SPF, domain verification, ACME challenges |
| SRV | Service location | Some 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 choice | Pros | Cons |
|---|---|---|
| Long (1 hourβ1 day) | Less DNS traffic, faster repeat lookups | Failover / IP change takes longer to spread |
| Short (30β60 s) | Faster failover and blue/green cuts | More 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
| Target | Typical record |
|---|---|
| Cloud load balancer | A / ALIAS to LB hostname |
| CDN | CNAME to d123.cloudfront.net |
| Multi-region | Geo 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)
| Technique | Idea |
|---|---|
| Multiple A records | Several IPs; clients try another if one fails (limited control) |
| Health-checked DNS failover | Route53 / similar removes unhealthy endpoints |
| Geo / latency routing | Users in IN get Mumbai LB; US get Virginia LB |
| Anycast | Same 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
| Kind | Who uses it | Example |
|---|---|---|
| Public | Internet users | www.myapp.com |
| Private (VPC) | Internal services | orders.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)
| Topic | One-liner |
|---|---|
| DNS spoofing / cache poisoning | Attacker feeds a resolver a fake IP |
| DNSSEC | Cryptographic signatures on DNS data (adoption uneven) |
| DoH / DoT | DNS over HTTPS/TLS β encrypts lookups to the resolver |
| Domain hijacking | Attacker 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
| Symptom | Likely layer |
|---|---|
NXDOMAIN / βserver not foundβ | Typo, missing record, registrar issue |
| Some users OK, some not | TTL / cache propagation |
| Wrong site / cert mismatch | CNAME to wrong host; leftover old A record |
Works after flushdns | Local OS/browser cache |
| App up but name fails | DNS 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
- Why donβt we hard-code IP addresses in mobile apps for production APIs?
- Order the lookup: authoritative NS, browser cache, root, recursive resolver, TLD.
- What does TTL control? Why lower it before a cutover?
- A vs CNAME β when use each?
- Why is DNS failover slower than load-balancer failover?
- Where does DNS sit relative to the load balancer in an HLD?
Answers
- IPs change (new LB, migration, failover). Names stay stable; DNS updates the mapping.
- Browser cache β recursive resolver (its cache) β root β TLD β authoritative β answer back.
- How long resolvers may reuse an answer. Lower TTL so clients pick up the new IP faster after you change it.
- A maps a name to an IP. CNAME aliases one name to another name (often CDN/LB hostname).
- Clients and resolvers cache DNS; LB health checks remove bad backends in seconds without waiting for TTL.
- Before the LB: Client β DNS β LB IP β apps.
Common beginner mistakes
| Mistake | Better |
|---|---|
| Forgetting DNS on the HLD | Always show Client β DNS β LB |
| Pointing apex at a single VM IP | Point at LB/CDN |
| TTL = 1 day, then emergency failover | Plan short TTL before risky changes |
| βDNS is always instantβ | Explain cache + TTL |
| Confusing DNS with load balancing | DNS 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.