DNS caching with a timeline of TTL expiry
Trace independent DNS cache deadlines, remaining TTL and cached NXDOMAIN answers, then separate ordinary expiry from serve-stale policies and application connections.

A DNS TTL gives cached data a lifetime measured from the response that supplied it. It is not a global countdown that begins when you edit a record. If you are learning why two users can receive different addresses after the same DNS change, draw a separate acquisition and expiry time for each cache.
This lesson assumes you know that an A record contains an IPv4 address. We use two independent recursive-resolver caches, one fixed question, and a single authoritative view that changes instantly in the model. The addresses are illustrative values in the documentation range. No public domain is queried or changed by the exercise.
Distinguish authority from a cached answer
An authoritative server answers from the zone data it serves. A recursive resolver obtains answers on a client’s behalf and can reuse cached information. A fresh cache lookup avoids another query to the authority. Therefore an authoritative change does not automatically contact every resolver that previously received an answer.
RFC 1035’s resource-record format describes TTL in seconds and gives zero a special meaning: use for the current transaction, without retaining it for subsequent cache reuse. Its implementation discussion also distinguishes relative TTL values in responses from absolute expiry times stored internally. Our model follows that useful representation by recording expires = acquisition_time + ttl.
The term fresh here means unexpired under the cache’s TTL rule. It does not mean the record still equals the authority’s latest value. An answer can remain valid for ordinary cache reuse after the authority has changed. Conversely, a resolver may evict it earlier or apply a shorter local retention limit. TTL is not a promise that every cache keeps every record until the final second.
Give each resolver its own clock position
At time zero, resolver A obtains address 192.0.2.10 with TTL 300. Its copy expires at time 300. Resolver B first obtains the same answer at time 80, so its copy expires at time 380. Both received the same TTL, but their expiry times differ because acquisition happened at different moments.
At time 100, our authority changes the address to 192.0.2.20 and changes the TTL of new answers to 60. This does not rewrite either cached entry. At time 299, A can still return the old address with one second remaining. At time 300, A’s next query fetches the new address, while B can return the old address with 80 seconds remaining.
Those answers disagree without either cache violating the model. There is no single event called “the DNS has propagated” that this pair of TTL fields can identify. You can ask narrower questions: which authority serves which data, what a particular recursive resolver returns, how much TTL remains, and whether an application’s own state still uses an earlier result.
Run the timeline without a wall clock
The code uses supplied integer times, making expiry boundaries reproducible. Every cache instance represents one fixed DNS question. A miss calls a supplied authority function; a hit returns the stored answer and remaining lifetime. The cache refuses time reversal so an accidental out-of-order test cannot extend a lifetime.
class OneQuestionCache:
def __init__(self):
self.entry = None
self.last_time = float("-inf")
def lookup(self, now, authority):
if now < self.last_time:
raise ValueError("model time must not go backward")
self.last_time = now
if self.entry is not None:
answer, expires = self.entry
if now < expires:
return answer, expires - now, "cache"
answer, ttl = authority(now)
if ttl < 0:
raise ValueError("negative TTL")
self.entry = (answer, now + ttl) if ttl > 0 else None
return answer, ttl, "lookup"
def authority(now):
if now < 100:
return "192.0.2.10", 300
return "192.0.2.20", 60
a = OneQuestionCache()
b = OneQuestionCache()
assert a.lookup(0, authority) == ("192.0.2.10", 300, "lookup")
assert b.lookup(80, authority) == ("192.0.2.10", 300, "lookup")
assert a.lookup(299, authority) == ("192.0.2.10", 1, "cache")
assert a.lookup(300, authority) == ("192.0.2.20", 60, "lookup")
assert b.lookup(300, authority) == ("192.0.2.10", 80, "cache")
assert b.lookup(380, authority) == ("192.0.2.20", 60, "lookup")
The strict now < expires comparison treats the expiry instant as a miss. More importantly, a hit does not replace the expiry with now + original_ttl. If each cache hit restarted a 300-second lifetime, a popular old answer could remain indefinitely despite continuous use. Returning the remaining lifetime also lets a downstream cache avoid granting another full original lifetime to data that is already old.
Real resolution includes query names, types and classes, referrals, aliases, transport failures and potentially DNSSEC validation. This class implements none of those. Its authority function returns immediately, and it assumes no prefetch, eviction, stale-answer policy or inconsistent authoritative replicas. The explicit limits let the small program test lifetime arithmetic rather than pretend to be a resolver.
Creating a record can also encounter an older cached answer
Caching is not limited to successful address answers. A resolver can remember that a name does not exist. Suppose a query at time zero receives NXDOMAIN with a negative lifetime of 120 seconds, and the name is created at time 50. A cache hit at time 119 can still return the earlier negative answer. The first refresh in this example occurs at time 120.
RFC 2308 specifies the negative lifetime using the smaller of the SOA record’s TTL and its MINIMUM field. With values 600 and 120, the result is 120 seconds. This is the negative-caching meaning established by that RFC, not the older interpretation of SOA MINIMUM as a floor for every positive record’s TTL.
def new_name_authority(now):
if now < 50:
soa_ttl, soa_minimum = 600, 120
return "NXDOMAIN", min(soa_ttl, soa_minimum)
return "192.0.2.30", 60
c = OneQuestionCache()
assert c.lookup(0, new_name_authority) == ("NXDOMAIN", 120, "lookup")
assert c.lookup(119, new_name_authority) == ("NXDOMAIN", 1, "cache")
assert c.lookup(120, new_name_authority) == ("192.0.2.30", 60, "lookup")
calls = []
def uncached_authority(now):
calls.append(now)
return "192.0.2.40", 0
zero = OneQuestionCache()
assert zero.lookup(10, uncached_authority)[2] == "lookup"
assert zero.lookup(10, uncached_authority)[2] == "lookup"
assert calls == [10, 10]
The second example reuses the same one-question clock model; it does not implement general negative-cache indexing. In the protocol, NXDOMAIN concerns a nonexistent name, while NODATA means the name exists but lacks the requested record type. RFC 2308 describes different reuse keys for them: name and class for the former, and name, type and class for the latter. An absent AAAA answer must not automatically erase a known A answer.
A timeout is also not proof that a name does not exist. Distinguish an authoritative negative answer from inability to obtain an answer. Collapsing those outcomes into one “not found” state can make diagnostics misleading and can apply an inappropriate cache policy. The final assertions demonstrate TTL zero separately: two queries at the same model time still make two authority calls.
TTL expiry is not an unconditional deletion rule
The simple model stops ordinary reuse at expiry. Modern resolvers can also support serve-stale behavior. RFC 8767 updates the older TTL rules to allow retaining expired data and using it under a resiliency policy when refresh cannot be completed. It separates retention after expiry from the ordinary unexpired lifetime and describes timers for client response, refresh attempts and maximum stale retention.
That exception is a reason to inspect a resolver’s actual configuration when diagnosing an old answer. It is not permission to assume every resolver serves stale data or to ignore TTL altogether. Nor does a fresh lookup necessarily fix inconsistent authoritative data: if different authoritative servers still serve different versions, the result depends on which one answers and what cache state follows.
Plan a change around the old lifetime
Lowering TTL at the same moment as an address change only affects answers fetched afterward. For a planned migration, lower it in advance and allow the previously issued longer lifetimes to age out before relying on the shorter interval. The exact lead time depends on the old data and serving arrangements. A smaller TTL usually means less ordinary cache reuse and more refresh work; it does not guarantee immediate client reconnection.
A browser, application or connection pool may already hold an address or an established connection independently of the recursive resolver’s current answer. DNS expiry does not send a command that moves an existing TCP connection to a new endpoint. Keep DNS observations and connection behavior as separate evidence when explaining why traffic still reaches an old server.
For an exercise, add a third cache that first queries at time 99. Predict its expiry before running the code. Then change the authority’s new TTL to zero and check that repeated post-expiry queries perform repeated lookups. Finally add a counter to the authority function: it should show that cache hits do not contact the authority and do not refresh the stored deadline.