Sub topic 7
TTL, caching, and why changes seem slow
This chapter assumes you know what a recursive resolver is, how a name is resolved through the root, TLD, and authoritative servers, and what A, CNAME, and NS records do. You should also be comfortable running dig on a command line.
What TTL is
Every DNS record carries a TTL (time to live), a number of seconds. It tells any cache holding the record how long it may reuse the answer before asking again. The authoritative server sets the TTL. Caches only obey it.
In the zone file from the earlier chapter, $TTL 3600 gives every record without its own TTL a lifetime of one hour.
Where caching happens
An answer can be cached at several layers between your program and the authoritative server:
- The browser keeps its own short-lived cache.
- The operating system's stub resolver may cache answers.
- The recursive resolver, such as your ISP's or
1.1.1.1, caches answers for all of its users.
Each layer keeps its own countdown. A change at the authoritative server is invisible to a layer until that layer's copy expires.
Watching the TTL count down
The second column of a dig answer is the remaining TTL. Run the same query twice against a resolver:
dig @1.1.1.1 example.com A
sleep 10
dig @1.1.1.1 example.com AThe number drops by about ten between the two runs. That drop shows the resolver is serving a cached copy. When it reaches zero, the resolver fetches a fresh answer and the number resets to the full TTL.
To see the authoritative TTL, query a name server of the zone directly with dig @ns1.example.com example.com A. That answer always shows the full value.
Why "propagation" is a misleading word
People say a DNS change "propagates," as if the new data is pushed across the internet. Nothing is pushed. The authoritative server changes immediately, and each cache refreshes only when its own copy expires.
The worst case is roughly the old TTL. If a record had a TTL of 86400 (one day), some resolvers may serve the old value for up to a day after you edit it. Resolvers that cached it earlier will refresh earlier, so different users see the change at different times.
Negative caching
Resolvers also cache failures. If a name does not exist, the answer is NXDOMAIN, and the resolver remembers that for a time. The SOA record's last field sets this period (the negative-caching TTL, 3600 in our zone file). So if you query a name before you create it, you may keep seeing "not found" for up to an hour after you add it.
Planning a change
Pick the TTL according to how soon you may need to change the record:
| Situation | Suggested TTL |
|---|---|
| Stable record, rarely changes | 3600 to 86400 |
| Migration planned soon | 300 |
| Failover or frequently changing target | 60 to 300 |
A low TTL means more queries reach your name servers and slightly slower first lookups. A high TTL means fewer queries but slower changes.
A safe migration follows this order:
- At least one old-TTL period before the move, lower the record's TTL to 300.
- Wait for the old TTL to expire, so every cache now holds the short value.
- Change the record to the new address.
- Verify with
dig, then wait a few minutes for caches to refresh. - Raise the TTL again once the new setup is stable.
NS changes are slower
Changing name servers at your registrar updates the delegation held by the TLD servers. Those NS records usually have a TTL of 24 to 48 hours, and you do not control it from your own zone. Plan for a slower change than an ordinary A record edit.
Common errors
- Testing with a cached resolver. Your local machine may keep returning the old answer. Query the authoritative server with
@to see the true current value. - Forgetting that browsers cache too. Restarting the browser or flushing the OS cache may be needed even after DNS is correct.
- Editing the record but not the right zone. If the delegation points to different name servers than the ones you edited, nothing changes anywhere.
Exercise
A record for www has a TTL of 86400. You want to move the site to a new server tomorrow with minimal downtime. What should you do today, and why?
Show answer
Lower the record's TTL to a short value such as 300 today. Resolvers must first let the old 86400-second copies expire, so the lowering must happen at least a day before the move. Tomorrow, change the address; caches then refresh within about five minutes.
Exercise
You run dig @1.1.1.1 example.com A and the TTL shows 1412. Ten seconds later it shows 1402. What does this tell you?
Show answer
The resolver is serving a cached copy and counting down its remaining lifetime. It will ask the authoritative server again when the value reaches zero.