Octet

How do DNS TTLs and negative caching work, and how do you use them when you change a record?

Show the answer

Say this first

Every record has a TTL in seconds. A resolver that caches it counts the TTL down and serves the cached copy until it reaches zero, then asks again. "No such name" (NXDOMAIN) and "no record of that type" (NODATA) answers are cached too, for the smaller of the SOA record's own TTL and the SOA minimum field (RFC 2308). Before a planned change I lower the TTL at least one old TTL ahead, make the change, then raise it again.

Go deeper

Positive caching

Each resolver counts down on its own. Users behind different resolvers can see different answers for up to one full TTL after a change, and you cannot flush other people's resolvers.

Negative caching

A negative answer carries the zone's SOA in the authority section. With SOA ... 2026111001 7200 3600 1209600 3600 and an SOA TTL of 3600, a "does not exist" answer is cached for an hour. The classic trap: someone tests a name before it is created, the resolver caches NXDOMAIN, and the new record stays invisible to them for up to an hour.

Resolvers also cap TTLs. BIND by default keeps positive answers at most 1 week and negative answers at most 3 hours.

Migration recipe

WhenAction
Two days beforeLower the TTL from 86400 to 300. Wait at least 86400 seconds
Change windowChange the address. Old answers drain within 5 minutes
Day afterRaise the TTL again

Keep the old server answering, or proxying to the new one, until the old TTL has passed.

Flushing caches you do control

WhereCommand
BINDrndc flushname www.example.com
Unboundunbound-control flush www.example.com
Windows DNS serverClear-DnsServerCache
Windows clientipconfig /flushdns
Linux systemd-resolvedresolvectl flush-caches

Traps

  • Lowering the TTL at the same moment as the change.
  • Forgetting negative caching when a brand new name "does not exist" for some users.
How did you do?Book 9.3 Infrastructure Applications