Skip to main content
DNS Explained – Complete Beginner to Advanced Guide
CHAPTER 09 Beginner

DNS Caching and Performance

Updated: May 15, 2026
20 min read

# CHAPTER 9

DNS Caching and Performance

1. Introduction

The Domain Name System processes trillions of queries every single day. If every query for google.com required a 5-step journey across the globe to interrogate Root and TLD servers, the internet would grind to an agonizing halt. The secret to DNS's incredible speed is Caching—the temporary memorization of IP addresses. However, caching introduces a complex problem: if an IP address changes, how long does the internet remember the *old* address? In this chapter, we will explore the mechanisms of DNS Caching, master the critical concept of TTL (Time to Live), and learn how to flush local caches to resolve outdated network traffic.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define the concept of DNS Caching and its necessity for internet speed.
  • Identify the three primary layers of caching (Browser, OS, Resolver).
  • Understand the function of the TTL (Time to Live) metric.
  • Strategize TTL configuration during web server migrations.
  • Execute commands to flush the local Operating System DNS cache.

3. Beginner-friendly Explanations

The Sticky Note (Caching): Imagine you ask your coworker for the office Wi-Fi password. They tell you it's "Admin123". You write it on a sticky note and put it on your monitor. For the next month, you don't ask your coworker; you just look at the sticky note (Caching).

The Expiration Date (TTL): What if the IT department changes the password? You will keep trying "Admin123" and failing, because your sticky note is outdated. To fix this, when your coworker gives you the password, they add an instruction: *"Use this password, but throw this sticky note away in 24 hours."* This expiration date is the TTL (Time to Live). It forces you to eventually ask for the password again, ensuring you get the updated version.

4. The Three Layers of Caching

As we saw in the previous chapter, DNS is cached at multiple levels:
  1. 1. Browser Cache: Chrome, Safari, and Firefox hold DNS records in memory (usually for a few minutes) to speed up browsing between tabs.
  1. 2. OS Cache: Windows, macOS, and Linux maintain a system-wide cache. If Chrome asks for netflix.com, the OS caches it so that if Spotify asks for netflix.com a minute later, the OS responds instantly without using the network.
  1. 3. Resolver Cache (ISP/Public): The 8.8.8.8 server caches records for millions of users based strictly on the TTL defined by the domain owner.

5. TTL (Time to Live) Explained

Every single DNS record (A, CNAME, MX) has a TTL value attached to it, measured in seconds.
  • TTL: 300 = 5 minutes
  • TTL: 3600 = 1 hour
  • TTL: 86400 = 24 hours

When a Resolver asks your Authoritative Server for your IP address, your server hands over the IP *and* the TTL. It says: *"Here is my IP. Memorize it, but delete it from your memory in 3600 seconds."*

6. TTL Strategy: The Server Migration

Understanding TTL is the most important skill for a DevOps engineer migrating a website to a new server. The Bad Migration: Your A Record points to Server A with a TTL of 24 hours. You launch Server B. You change the A Record to Server B. *The Disaster:* Because the TTL was 24 hours, Resolvers worldwide will continue sending customers to the old Server A for an entire day!

The Professional Migration:

  1. 1. Monday: You lower the TTL on Server A from 24 hours down to 5 minutes (300).
  1. 2. Tuesday: You wait 24 hours for the old, long caches around the world to officially expire and pick up the new 5-minute rule.
  1. 3. Wednesday: You change the A Record to Server B. Because Resolvers are now caching it for only 5 minutes, the entire globe switches to the new server almost instantly.
  1. 4. Thursday: You raise the TTL back to 24 hours to optimize performance.

7. Command Examples: Flushing the Cache

If you are developing a website and your computer refuses to load the new IP address, you must violently clear your Operating System's "sticky notes."
bash
12345678
# Windows: Flush the DNS Cache
ipconfig /flushdns

# macOS: Flush the DNS Cache
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

# Linux (systemd-resolved): Flush the DNS Cache
sudo resolvectl flush-caches

8. Best Practices

  • Standard TTL Values: For a stable website that rarely changes IPs, a high TTL (12 to 24 hours) is best practice. It reduces the load on your Authoritative Nameservers and marginally speeds up the internet for returning users. Only use low TTLs (5 minutes) for active migrations or highly dynamic cloud environments (like Load Balancers that change IPs frequently).

9. Common Mistakes

  • Ignoring Propagation Delays: Beginners often change a DNS record and immediately complain to customer support that the website is broken. They fail to realize that if their old TTL was 48 hours, it is mathematically impossible for the global internet to see the new IP address until that 48-hour cache expires on the thousands of Resolvers globally. This phenomenon is known as "DNS Propagation."

10. Mini Project: Inspect Real-World TTLs

Let's see how long massive tech companies cache their domains.
  1. 1. Open your terminal.
  1. 2. Run: dig google.com
  1. 3. Look in the "ANSWER SECTION". You will see a number (e.g., 300 or 112) right before the "IN A" record. This is the remaining TTL in seconds!
  1. 4. Run dig google.com again immediately. You will notice the number has counted down. You are watching the cache timer expire in real-time!

11. Practice Exercises

  1. 1. If an A Record has a TTL of 86400, how long (in hours) will a Recursive Resolver cache the IP address before checking the Authoritative Server again?
  1. 2. Explain why a network engineer must lower the TTL of a domain 48 hours *before* initiating a critical server migration.

12. MCQs with Answers

Question 1

In the context of DNS, what does TTL (Time to Live) dictate?

Question 2

Which command is used on a Windows operating system to instantly clear the local DNS cache?

13. Interview Questions

  • Q: Explain the concept of DNS Propagation. Why does it take time for DNS changes to reflect globally?
  • Q: Walk me through the exact, step-by-step TTL strategy you would use to migrate a high-traffic e-commerce website to a new cloud provider with zero downtime.
  • Q: If a user cannot access a newly migrated website, but you can verify the global DNS records are correct, what local troubleshooting step should the user perform?

14. FAQs

Q: Can I force Google's 8.8.8.8 Resolver to flush its cache of my domain? A: Yes! Google provides a public "Flush Cache" webpage (Google Public DNS Cache Flush). You type in your domain name, and Google will instantly delete its cached record and fetch the newest IP from your Authoritative server. Cloudflare provides a similar "Purge Cache" tool for 1.1.1.1.

15. Summary

In Chapter 9, we tackled the double-edged sword of DNS Performance: Caching. We learned that while multi-layered caching at the Browser, OS, and Resolver levels is essential for internet speed, it requires strict expiration management. We mastered the concept of TTL (Time to Live), identifying it as the critical timer that dictates caching behavior. By understanding how to strategically manipulate TTL values during server migrations and how to manually flush local OS caches, we gained the ability to execute seamless infrastructure upgrades without suffering prolonged DNS propagation delays.

16. Next Chapter Recommendation

You understand how to manage records and TTLs. Now it is time to connect the domain to an actual web server. Proceed to Chapter 10: DNS and Web Hosting.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·