Chapter 3 · Lesson 14 of 22
Domains and HTTPS

TTL and DNS caching explained

TTL is the number of seconds a DNS answer may be cached, and every layer between you and the authoritative server (resolver, OS, browser) keeps its own copy until that time runs out.

By the end you will

Plan a DNS change around TTL, read remaining TTL with dig and flush local caches on Linux and macOS.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • dig installed
  • A domain you can edit DNS for

TTL (time to live) is how many seconds a DNS answer may be reused before it must be fetched again. Every record in a zone has one. The copy of the answer is kept by several layers, each with its own clock, which is why a change you made a minute ago can look live from one device and missing from another. This lesson goes deeper than DNS propagation and TTL, which covers the basics.

How it works#

A lookup passes through a chain of caches before it reaches the authoritative server:

  1. The application. Browsers keep a small DNS cache of their own.
  2. The operating system’s resolver, such as systemd-resolved on many Linux distributions or mDNSResponder on macOS.
  3. A recursive resolver run by your ISP, company or a public service. This is the biggest cache and serves many users.
  4. The authoritative nameserver that actually holds the zone.

When a recursive resolver fetches a record, it stores it with the TTL it received. Each time a client asks, it answers from the cache and counts the TTL down. When it reaches zero, the entry is dropped and the next question goes upstream. Lower layers may add their own limits or ignore very small or very large values, so the TTL is a request, not a guarantee.

Read the remaining TTL#

Query a public resolver twice a few seconds apart:

bash
dig @1.1.1.1 example.com A +noall +answer
sleep 10
dig @1.1.1.1 example.com A +noall +answer

The second answer shows a smaller number in the TTL column. That is the time left in that resolver’s cache. Querying the authoritative server shows the full configured TTL every time, since it is not counting down:

bash
dig @ns1.example.net example.com A +noall +answer

Be aware that large public resolvers are made of many servers, so repeated queries may hit different caches and show different remaining times.

Lower the TTL before a change#

The plan for moving a site to a new IP has a fixed order, and the timing matters:

  1. Note the current TTL, for example 86400 (one day).
  2. At least that long before the move, lower the TTL on the record, for example to 300.
  3. Wait until the old TTL has passed. Caches that stored the answer under the old, long TTL need that time to expire.
  4. Change the record to the new IP.
  5. Verify, and after things settle, raise the TTL again to reduce query load.

Lowering the TTL at the same moment as the change does nothing for caches that already hold the old value under the long TTL. This is the single most common mistake. See Point a domain at your server for the record you are changing.

Negative caching and the SOA#

Resolvers also cache failures. If someone looked up new.example.com before you created it, the resolver stored an NXDOMAIN answer (the name does not exist). That negative answer is cached for a time taken from the zone’s SOA record (RFC 2308 uses the lower of the SOA TTL and its minimum field). Read it with:

bash
dig example.com SOA +noall +answer

The last number in the data is the minimum. A long value means a record you create may stay invisible to some users for that long. If you plan a launch, create the record before anyone is told about the name, and keep the SOA minimum modest. See What is a DNS zone for the SOA fields.

Flush local caches#

Flushing only clears your own machine. It cannot touch your ISP’s resolver or anyone else’s.

On Linux with systemd-resolved:

bash
resolvectl statistics        # shows cache size and hits
sudo resolvectl flush-caches

On macOS (commands have changed between releases, these work on recent ones):

bash
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

Browsers keep their own cache. In Chrome and Edge you can open chrome://net-internals/#dns and clear the host cache. Restarting the browser also works. Test with dig instead of a browser when you want a clean answer, since dig skips the application cache.

Check it worked#

Compare three views and make sure they agree once caches have expired:

bash
dig @ns1.example.net example.com A +short   # authoritative, the truth
dig @1.1.1.1 example.com A +short           # a public resolver
dig example.com A +short                    # your configured resolver

If the first is correct and the others are old, caches are the reason, and waiting for the TTL is the fix. If the first is wrong, the edit never reached the authoritative zone.

Edge cases and gotchas#

  • Long TTL on NS records: nameserver records at the parent zone often have long TTLs that you cannot change. See DNS propagation myths.
  • Resolvers that ignore TTL: a few resolvers enforce a minimum or maximum, so a 60 second TTL may be kept longer than expected.
  • Every layer counts: flushing your OS cache does nothing if the resolver upstream still holds the old record.
  • Short TTL costs load: a very low TTL means more queries to your nameservers and slightly slower first lookups.
  • Different records, different TTLs: www and the apex can have different TTLs, so one can update before the other.
  • Negative cache surprise: testing a name before creating it can make the failure stick for a while on that resolver.

Troubleshooting#

SymptomLikely causeFix
Old IP still returned after the changeThe resolver cache holds the old answerQuery the authoritative server, then wait for the remaining TTL
New record shows NXDOMAINNegative caching from an earlier lookupWait for the SOA minimum, or test through a different resolver
Browser shows old site, dig shows new IPBrowser or OS cacheFlush the OS cache and restart the browser
Lowering TTL did not speed up the moveIt was lowered too lateNext time lower it a full old-TTL period ahead
resolvectl: command not foundThe system does not use systemd-resolvedFind the local cache service (nscd, dnsmasq) and restart it
Different networks see different IPsSeparate resolver caches expire at different timesExpected; verify with the authoritative answer

Quick reference#

bash
dig @1.1.1.1 example.com A +noall +answer    # remaining TTL at a resolver
dig @ns1.example.net example.com A           # full TTL at the source
dig example.com SOA +noall +answer           # negative caching time
sudo resolvectl flush-caches                 # Linux systemd-resolved
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder   # macOS

Next#

Continue with DNS propagation myths.

Check what you learned4 questions
1When should you lower a record's TTL before moving a site to a new IP?
2What does the TTL number in a dig answer show when you query a caching resolver?
3What is negative caching?
4Which command flushes the systemd-resolved cache on Linux?

Frequently asked questions

What is a good TTL for a DNS record?

Common values are 300 to 3600 seconds for records you may change, and longer for stable ones. Pick a value that matches how quickly you need to move traffic, and lower it ahead of planned changes.

How do I flush the DNS cache on Linux?

On systems with systemd-resolved, run resolvectl flush-caches. Other setups use their own cache, such as nscd or dnsmasq, which have their own flush or restart commands.

How do I flush the DNS cache on macOS?

Run sudo dscacheutil -flushcache followed by sudo killall -HUP mDNSResponder. The exact commands have varied between macOS releases, so check Apple's documentation if they have no effect.

Why does my new DNS record not show up yet?

Some cache still holds an earlier answer, possibly a negative one saying the name did not exist. Query the authoritative server directly to confirm the record exists, then wait for caches to expire.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios