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.
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
| Level | Run by | Knows |
|---|---|---|
| Root servers | 13 named root servers (a–m), hundreds of anycast instances | Where each TLD's servers are |
| TLD servers | Registries (Verisign for .com) | Which name servers are authoritative for each domain |
| Authoritative servers | The domain owner or their DNS provider (Route 53, Cloudflare) | The actual records for the domain |
How a lookup works
- 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
| Type | Maps | Example |
|---|---|---|
| A | Name → IPv4 | example.com. A 93.184.216.34 |
| AAAA | Name → IPv6 | example.com. AAAA 2606:2800:220:1::1946 |
| CNAME | Name → another name (alias) | www CNAME example.com. |
| MX | Mail servers for the domain (with priorities) | MX 10 mail.example.com. |
| NS | Authoritative name servers for a zone | NS ns1.provider.net. |
| TXT | Arbitrary text: SPF, DKIM, domain verification | "v=spf1 include:_spf.google.com ~all" |
| SOA | Zone metadata (primary server, serial, timers) | — |
| SRV | Service location (host + port) | _sip._tcp SRV 10 5 5060 sip.example.com. |
| CAA | Which CAs may issue certificates | CAA 0 issue "letsencrypt.org" |
| PTR | Reverse lookup: IP → name | 34.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.localresolves 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
| Threat | Description | Defence |
|---|---|---|
| Cache poisoning | Injecting forged answers into a resolver's cache | Random source ports and query IDs; DNSSEC signs records |
| Eavesdropping | DNS is plain-text UDP by default; observers see every site you visit | DoH (DNS over HTTPS), DoT (over TLS), DoQ |
| Hijacking | Compromised registrar account or changed NS records | Registrar locks, 2FA |
| DDoS | Overwhelming authoritative servers (Dyn, 2016) | Anycast, multiple providers |
| Amplification | Small spoofed queries produce large replies aimed at a victim | Response rate limiting, closing open resolvers |
Debugging DNS
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 WindowsCommon 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.
- 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.
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.