DNS Caching

DNS caching is the primary mechanism that makes the Domain Name System fast enough for real-world web browsing. Without caching, every page visit would require a full round-trip through the global DNS hierarchy, adding hundreds of milliseconds (or more) to every request.

The Problem Caching Solves

If DNS were not cached:

  • Every visit to google.com would trigger a full recursive query.
  • Billions of users × millions of domains = an impossibly large load on root and TLD servers.
  • Latency would make the web feel sluggish.

“Whenever there’s a slowness thing, you always think about caching. DNS is no different.” — Day 30 CKA Video

Multi-Level Cache Hierarchy

DNS is cached at multiple independent layers, each with its own TTL and eviction policy:

Browser Cache → OS Cache → Router Cache → ISP Cache → Recursive Resolver Cache

1. Browser Cache

  • Scope: Per-application (Chrome, Firefox, curl, etc.)
  • Behavior: First visit to a domain triggers a DNS lookup; subsequent visits use the cached IP.
  • Observation: In Chrome DevTools Network tab, the first request shows a DNS lookup timing row. On refresh, that row disappears because the browser already knows the IP.
  • TTL: Typically seconds to minutes; varies by browser.

2. Operating System Cache

  • Scope: System-wide (all applications)
  • Behavior: The OS maintains its own DNS cache so that multiple apps don’t duplicate queries.
  • Flush on Linux: sudo systemd-resolve --flush-caches (or sudo killall -HUP mDNSResponder on macOS)
  • TTL: Respects the TTL from the DNS response.

3. Router Cache

  • Scope: Local network (all devices behind the router)
  • Behavior: Home and office routers often run a small DNS forwarder (e.g., dnsmasq) that caches responses for all connected devices.
  • Benefit: Multiple family members visiting the same site only trigger one upstream query.
  • TTL: Configurable; often 5–30 minutes.

4. ISP Cache

  • Scope: All customers of the Internet Service Provider
  • Behavior: ISPs run large recursive resolvers that cache heavily visited domains.
  • Benefit: Reduces upstream bandwidth and improves response times for popular sites.
  • TTL: Respects authoritative TTL but may impose minimums.

5. Recursive Resolver Cache

  • Scope: The DNS resolver you configure (e.g., Cloudflare 1.1.1.1, Google 8.8.8.8)
  • Behavior: These public resolvers cache millions of records across their global anycast network.
  • Benefit: Even if your local caches miss, a major resolver likely has the answer.

TTL: Time-To-Live

Every DNS record includes a TTL value (in seconds) set by the domain owner:

example.com.  300  IN  A  1.2.3.4
;             ↑ TTL = 300 seconds (5 minutes)
  • Lower TTL (e.g., 60s): Faster propagation of changes; higher query load.
  • Higher TTL (e.g., 86400s = 24h): Lower load; slower propagation.

When making planned DNS changes (e.g., switching hosting providers), operators often lower the TTL beforehand to ensure caches expire quickly.

Demonstration: First Visit vs. Refresh

Using Chrome DevTools Network tab:

VisitDNS Lookup TimingExplanation
FirstVisible (~10–100ms)Browser has no cache; queries resolver
Second (refresh)AbsentBrowser cache hit; no DNS query needed

This demonstrates that caching happens immediately at the browser level. Day 30 CKA Video

Cache Invalidation and Flushing

When a DNS record changes, existing cached entries remain valid until their TTL expires. This is why DNS changes can feel “slow” to propagate — it’s not the servers being slow; it’s caches holding old data.

LayerHow to Flush
BrowserRestart browser or clear cache
OS (Linux)systemd-resolve --flush-caches
OS (macOS)sudo killall -HUP mDNSResponder
OS (Windows)ipconfig /flushdns
RouterReboot or access admin panel

Source