DNS Caching & TTL

TTL mechanics, negative caching, NXDOMAIN propagation, stale-while-revalidate, cache poisoning attacks, and resolver hierarchies.

Overview

Overview DNS caching is fundamental to the scalability of the internet. Without caching, every DNS query would traverse from the client to the root nameservers, through TLD nameservers, to the authoritative nameservers for every single request. Caching allows resolvers to store and reuse responses for the duration specified by the Time-To-Live (TTL) value in each DNS record, dramatically reducing query volume to authoritative servers and decreasing resolution latency for clients.

The TTL field in a DNS resource record specifies how long, in seconds, the record may be cached by resolvers. When an authoritative nameserver returns a record, it includes the TTL; each resolver that caches the response decrements the TTL as time passes and re-queries the authoritative server when it reaches zero. A record with TTL 300 cached by a resolver five minutes ago has TTL 0 — the resolver must re-query before serving the record to the next client.

The resolver hierarchy has several layers. Clients first check their local DNS cache (maintained by the OS resolver), then query a stub resolver (typically the router or DHCP-provided nameserver), which queries a recursive resolver (often the ISP's resolver or a public resolver like 8.8.8.8). The recursive resolver caches responses and serves them to multiple clients, making its cache the most impactful layer for TTL decisions.

Negative caching handles NXDOMAIN (non-existent domain) responses. RFC 2308 standardizes that NXDOMAIN responses should be cached for the duration specified in the SOA record's minimum field or the SOA TTL, whichever is smaller. This prevents resolvers from hammering authoritative servers with repeated queries for domains that do not exist.

Cache poisoning is the most serious security threat to DNS caching. An attacker who can inject a fraudulent DNS response into a resolver's cache can redirect traffic for any domain the resolver serves. The Kaminsky attack in 2008 showed how an attacker could poison the cache of any resolver by racing to inject a forged response before the legitimate one arrived. DNSSEC provides cryptographic protection against cache poisoning; source port randomization and 0x20 encoding are mitigations for non-DNSSEC resolvers.

Key concepts

  • TTL (Time-To-Live)
  • Negative Caching (NXDOMAIN)
  • Resolver Hierarchy
  • Cache Poisoning
  • Stale-While-Revalidate
  • Minimum TTL Floor

Interview questions

  • You update a DNS A record on Route 53 but some users still see the old IP an hour later. Walk me through all the caching layers that could explain this.
  • Explain the Kaminsky DNS cache poisoning attack and what mitigations are in place today.
  • How does negative caching work, and how does it affect new service deployments?

Sign up free to read the full topic

More Networking topics

Start free on Cariara · Coding problems · System design studio