DNS cache is actually a distributed database that maps domain names to IP addresses, but the "cache" part is the trick: it’s a local copy of that data to speed up lookups.
Let’s see it in action. Imagine you want to visit www.example.com. Your computer doesn’t immediately ask the entire internet where www.example.com lives. Instead, it first checks its own local DNS cache. If it finds the IP address (say, 93.184.216.34) in its cache, it uses that immediately and saves you the trip to a DNS server. This is super fast.
If the IP address isn’t in the local cache, your computer then asks your router or your ISP’s DNS server. These servers also have their own caches. If they have the record, they’ll tell your computer, and importantly, they’ll also store it in their cache for a certain amount of time (the TTL, or Time To Live). If no one has it cached, the request goes up the chain to authoritative DNS servers, which are the ultimate source of truth for a domain. Once the IP is found, it’s returned to your computer, and that IP address gets added to your local cache, along with the TTL.
This caching mechanism is what makes browsing the web feel zippy. Without it, every single website visit would involve multiple network hops to find the IP address, leading to noticeable delays. Your operating system, your web browser, and your router all maintain their own DNS caches to minimize these lookups.
So, when do you need to flush it? The primary reason is when a website’s IP address has changed, but your local cache (or your ISP’s cache) still holds the old, incorrect information. This is like having an old phone number for a friend saved in your contacts – you’re trying to reach them at the wrong place.
Here’s how to flush the DNS cache on different operating systems:
Windows:
- Open Command Prompt as administrator.
- Type
ipconfig /flushdnsand press Enter. This command tells the Windows operating system’s DNS Resolver service to clear its cache, forcing it to look up fresh DNS records on the next request.
macOS:
- Open Terminal.
- For macOS 10.10.4 and later: Type
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponderand press Enter. You’ll be prompted for your administrator password. Thedscacheutil -flushcachecommand clears the directory service cache, andkillall -HUP mDNSResponderrestarts the Multicast DNS Responder process, which handles DNS lookups and caching.
Linux: The method varies depending on your distribution and DNS caching service.
- If you’re using
systemd-resolved(common on newer distros like Ubuntu 18.04+):- Open Terminal.
- Type
sudo systemd-resolve --flush-cachesand press Enter. This command directly instructs thesystemd-resolvedservice to empty its DNS cache.
- If you’re using
dnsmasq:- Open Terminal.
- Type
sudo service dnsmasq restartand press Enter. Restarting thednsmasqservice will clear its cache.
- If you’re using
nscd:- Open Terminal.
- Type
sudo service nscd restartand press Enter. Restarting the Name Service Cache Daemon (nscd) will clear its cached DNS entries.
Routers: Your home router also maintains a DNS cache. To flush it, you typically need to restart the router. This usually involves unplugging its power for about 30 seconds and then plugging it back in.
A common situation where flushing is necessary is after migrating a website to a new server or changing its DNS records. The old IP address might remain cached by your computer, your ISP, or even recursive DNS servers for hours or days, depending on the TTL set for those records. Flushing your local cache forces your system to query for the most up-to-date record.
The surprising thing about DNS caching is how much it relies on the Time To Live (TTL) value. This is a setting on the DNS record itself, indicating how long a DNS resolver should cache that record. A low TTL (e.g., 60 seconds) means changes propagate very quickly but can lead to more frequent DNS lookups. A high TTL (e.g., 86400 seconds, or 24 hours) reduces DNS traffic but means changes can take a long time to become universally effective. When you flush your cache, you’re essentially telling your local machine to ignore its stored TTL and fetch a fresh record, which will then be cached again according to its new TTL.
Once you’ve flushed your local cache, the next step in troubleshooting DNS issues often involves checking your router’s cache or even using online tools to see how propagation is affecting other users.