DNS

How the Domain Name System resolves names — the hierarchy, recursive and iterative resolution, record types, caching and TTLs, and DNS for load balancing, security and debugging.

Intermediate 4 min readLesson 9 of 15 Interview Q&A

DNS is the Internet's phone book: it translates names like www.example.com into IP addresses. It's a globally distributed, hierarchical, heavily cached database, and nearly every network connection starts with a DNS lookup. When DNS breaks, "the Internet is down".

The hierarchy

wwwapiexamplegooglecomwikipediaorgcoin. (root)
Names are read right to left: www.example.com. → com → example → www
LevelRun byKnows
Root servers13 named root servers (a–m), hundreds of anycast instancesWhere each TLD's servers are
TLD serversRegistries (Verisign for .com)Which name servers are authoritative for each domain
Authoritative serversThe domain owner or their DNS provider (Route 53, Cloudflare)The actual records for the domain

How a lookup works

Browser / OSRecursive resolverRootTLD (.com)Authoritativewww.example.com?www.example.com?ask .com serverswww.example.com?ask ns1.example-dns.netwww.example.com?A 93.184.216.34, TTL 30093.184.216.34
  • The client asks a recursive resolver (your ISP's, or 1.1.1.1 / 8.8.8.8) to do all the work: a recursive query.
  • The resolver asks each level in turn, following referrals: iterative queries.
  • Every answer is cached for its TTL at every layer — browser, OS, resolver — so most lookups are answered instantly from a cache and the root servers see only a tiny fraction of the traffic.

Record types

TypeMapsExample
AName → IPv4example.com. A 93.184.216.34
AAAAName → IPv6example.com. AAAA 2606:2800:220:1::1946
CNAMEName → another name (alias)www CNAME example.com.
MXMail servers for the domain (with priorities)MX 10 mail.example.com.
NSAuthoritative name servers for a zoneNS ns1.provider.net.
TXTArbitrary text: SPF, DKIM, domain verification"v=spf1 include:_spf.google.com ~all"
SOAZone metadata (primary server, serial, timers)—
SRVService location (host + port)_sip._tcp SRV 10 5 5060 sip.example.com.
CAAWhich CAs may issue certificatesCAA 0 issue "letsencrypt.org"
PTRReverse lookup: IP → name34.216.184.93.in-addr.arpa PTR …

TTLs and caching

The TTL (time to live, in seconds) says how long resolvers may cache a record.

  • Long TTLs (hours) reduce lookups and latency, but changes take that long to propagate everywhere.
  • Short TTLs (30–60 s) let you fail over quickly, at the cost of more queries.
  • Before a migration, lower the TTL a day ahead, switch, then raise it again.

"DNS propagation" isn't really propagation: it's old cached answers expiring.

DNS as infrastructure

  • Load balancing — return several A records (round-robin DNS) or different answers per client.
  • GeoDNS / latency-based routing — answer with the server or CDN edge nearest the user.
  • Failover — health-checked records stop returning unhealthy endpoints.
  • Service discovery — inside Kubernetes, my-svc.my-namespace.svc.cluster.local resolves to a service IP via CoreDNS.

Clients and resolvers cache, so DNS-based failover is never instant: it's bounded below by the TTL, and some clients ignore TTLs entirely.

Security

ThreatDescriptionDefence
Cache poisoningInjecting forged answers into a resolver's cacheRandom source ports and query IDs; DNSSEC signs records
EavesdroppingDNS is plain-text UDP by default; observers see every site you visitDoH (DNS over HTTPS), DoT (over TLS), DoQ
HijackingCompromised registrar account or changed NS recordsRegistrar locks, 2FA
DDoSOverwhelming authoritative servers (Dyn, 2016)Anycast, multiple providers
AmplificationSmall spoofed queries produce large replies aimed at a victimResponse rate limiting, closing open resolvers

Debugging DNS

Shell
dig example.com                 # A record, with TTL and the server that answered
dig +short example.com AAAA
dig example.com MX
dig +trace www.example.com      # follow the delegation from the root down
dig @1.1.1.1 example.com        # ask a specific resolver
nslookup example.com            # available everywhere, including Windows

Common culprits: stale caches (wait out the TTL or flush with resolvectl flush-caches / ipconfig /flushdns), a wrong resolver in /etc/resolv.conf, a split-horizon setup returning internal addresses, or a missing trailing dot in a zone file.

Key takeaways
  • DNS is a hierarchical, distributed, cached database: root → TLD → authoritative.
  • Clients ask a recursive resolver, which iterates through the hierarchy and caches the answers.
  • Know the records: A, AAAA, CNAME, MX, NS, TXT, SRV, CAA.
  • TTLs trade propagation speed against query load; lower them before migrations.
  • DNS powers load balancing, geo-routing and service discovery; DNSSEC and DoH address security and privacy.
Interview questions
QWhat happens if you change an A record — why do some users still hit the old server?easy

Resolvers, operating systems and browsers cached the old answer and keep using it until its TTL expires; some clients cache longer than the TTL. Lower the TTL well before the change and keep the old server running until traffic drains.