Sub topic 9
Caching and TTL
This chapter assumes you know how a lookup travels from a stub resolver to a recursive resolver and on to the root, TLD and authoritative servers. It also assumes you can run dig in a terminal. Here we look at how answers are reused and how long they stay valid.
Why DNS caches
A full lookup can involve several round trips to different servers. Repeating that work for every request would be slow and would overload the root, TLD and authoritative servers. So resolvers store answers and reuse them. Caching happens in several places:
- The application, such as a browser.
- The operating system's stub resolver.
- The recursive resolver, which is the largest and most useful cache because many clients share it.
TTL: who decides how long
Every resource record carries a TTL (time to live), a number of seconds. The authoritative server sets it in the zone file. In the zone file from the earlier chapter, $TTL 3600 gives each record a default of one hour.
A cache may keep the record for at most that long. Each time the record is served from cache, the cache reports the remaining time, so the number counts down.
dig @8.8.8.8 example.com A
dig +noall +answer example.com AIn each answer line, the number before IN is the remaining TTL. Run the same query twice a few seconds apart and the number drops. When it reaches zero, the resolver discards the record and asks upstream again. The new answer comes back with the full TTL.
Two resolvers may show different remaining TTLs, or different data, for the same name. Each one cached the answer at a different moment.
A timeline of one cached record
Negative caching
Resolvers also cache failures. If a name does not exist, the server returns NXDOMAIN. If the name exists but has no record of the requested type, the answer is empty (called NODATA). Both can be cached.
The lifetime of a negative answer comes from the SOA record. The zone's SOA record includes a negative caching TTL, which is the last number in the example zone (300). In practice the resolver uses the smaller of that value and the SOA record's own TTL.
This has a common consequence. If you query a name before you create it, resolvers may remember "does not exist" for several minutes after you add the record.
Choosing a TTL
| TTL | Effect |
|---|---|
| Short (30 to 300 s) | Changes take effect quickly. More queries reach your authoritative servers. |
| Long (3600 s or more) | Fewer queries and faster lookups. Changes take longer to appear. |
A common practice is to lower the TTL ahead of a planned change, such as moving a server to a new IP address. Wait at least the old TTL, make the change, then raise the TTL again.
Common errors
- Changing a record and expecting instant results. Caches that already hold the old answer keep serving it until their TTL runs out.
- Lowering the TTL at the same moment as the change. Caches holding the old record still have the old, long TTL, so lower it in advance.
- Testing with a cached resolver. Query the authoritative server directly with
dig @ns1.example.com example.com Ato see the true current data. - Forgetting local caches. The browser and operating system may hold an answer after the resolver has refreshed.
Some resolvers enforce a minimum or maximum TTL of their own, so the TTL you set is a request, not a guarantee.
Exercise
You change the A record for www.example.com from 192.0.2.10 to 192.0.2.20. The record's TTL was 3600 seconds. At worst, how long might some users still be sent to the old address, and what should you have done beforehand to shorten that time?
Show answer
Up to 3600 seconds (one hour), because a resolver that cached the old answer just before your change keeps it for the full TTL. Beforehand, lower the TTL (for example to 300) and wait at least the old 3600 seconds so every cache holds the short TTL. Then make the change.
Exercise
You run dig +noall +answer example.com A twice, 30 seconds apart. The first answer shows a TTL of 300 and the second shows 270. What does this tell you?
Show answer
The answer came from a cache and the TTL is counting down. The resolver stored the record and reports the time left. When it reaches 0, the resolver fetches a fresh copy.