Sub topic 7
TTL, caching, and why changes seem slow
You change a DNS record, reload the page, and see the old site. A colleague sees the new one. This chapter explains why, using the working model of TTL and caching. It assumes you know what a recursive resolver is, how a query moves from the root to the TLD to the authoritative servers, and how to read basic record types (A, CNAME, NS, SOA).
What TTL is
Every DNS record carries a TTL (time to live), a number of seconds. The authoritative server attaches it to each record in its answer. It tells any cache that holds the record how long it may reuse it without asking again.
In a zone file, the TTL is the number between the name and the record class:
$ORIGIN example.com.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2025010101 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
3600 ) ; negative-caching TTL
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 192.0.2.10
@ IN AAAA 2001:db8::10
www 300 IN A 192.0.2.10
ns1 IN A 192.0.2.53
ns2 IN A 192.0.2.54Here $TTL 3600 sets the default of one hour. The www record overrides it with 300 seconds. Records without their own value use the default.
Two details matter later:
- TTL belongs to each record set (all records with the same name and type), not to the zone as a whole. Records in one set should share one TTL.
- The TTL is a limit on reuse, not a schedule for refresh. Nothing is pushed to caches when you edit a record.
Where caching happens
A name lookup passes through several caches before it reaches your authoritative server.
- The application. Browsers keep their own short-lived DNS cache. Long-running programs, such as a Java service, may cache lookups for the life of the process.
- The operating system. A local stub resolver service may cache answers (for example
systemd-resolvedon Linux, or the DNS Client service on Windows). - A router or local forwarder. Home routers often run a small caching forwarder.
- The recursive resolver. This is usually run by your ISP, your company, or a public service. It serves many users, so its cache has the largest effect.
Only the authoritative server holds the truth. Every other layer holds a copy that may be out of date by up to its remaining TTL.
How a cache counts down
When a recursive resolver gets an answer with TTL 3600, it stores the record and starts a countdown. Each time a client asks, the resolver returns the cached record with the remaining TTL. When the countdown reaches zero, the entry expires. The next query triggers a fresh lookup.
You can watch this with dig. The second column of each answer line is the TTL.
dig @1.1.1.1 example.com A
sleep 10
dig @1.1.1.1 example.com AThe second query shows a TTL about ten seconds lower than the first, because the resolver answered from cache. Some large public resolvers are spread across many servers, so repeated queries can land on different caches and show TTLs that jump around.
This has a direct consequence. If you change a record at 12:00 and a resolver cached the old one at 11:59 with TTL 3600, that resolver may keep serving the old value until 12:59. A resolver that never cached it will fetch the new value immediately. Different users see different answers during that window.
Why "propagation" is a misleading word
People say a change is "propagating" across the internet. That suggests the new record is being pushed outward. It is not. The authoritative servers have the new data almost at once. What takes time is the expiry of old copies in caches you do not control.
Two separate delays can apply:
- Between your primary and secondary name servers. Secondary servers copy the zone from the primary. The SOA
serialmust increase so that secondaries notice the change, and the SOArefreshinterval (or a NOTIFY message from the primary) controls how soon they check. Many managed DNS providers handle this for you. - Between the authoritative servers and the caches. This is governed by the TTL that was in effect when each cache last fetched the record.
Edits to NS records at the registrar behave differently, and are covered below.
Planning a change around TTL
The TTL that matters is the one in the old record, because that is what caches already hold. Lowering the TTL at the same moment you change the value does not help caches that stored the old record with a long TTL.
The standard procedure for a planned move, such as switching to a new host:
- Note the current TTL, for example 86400 (one day).
- At least one full old TTL before the move, lower the TTL on the records to something short, such as 300. Leave the value unchanged.
- Wait until the old TTL has passed. Now every cache holds a copy that expires within five minutes.
- Change the record value to the new address.
- Verify from several resolvers (see below).
- After the new setup is stable, raise the TTL again.
Choosing a TTL
There is no single correct value. It is a trade-off between speed of change and load, latency, and resilience.
| TTL | Typical use | Benefit | Cost |
|---|---|---|---|
| 60 to 300 seconds | Records you may change quickly: failover, migrations, load-balancing | Fast cutover | More queries, slightly slower first lookups, more dependence on your DNS provider being up |
| 1800 to 3600 seconds | Ordinary website and mail records | Good balance | A mistake takes up to an hour to clear |
| 86400 seconds | Rarely changing records such as NS and MX | Few queries, resilient to short DNS outages | A change takes a day to clear everywhere |
Some resolvers apply their own limits. A resolver may enforce a minimum (raising a very low TTL) or a maximum cap, so a TTL of 5 seconds is not always honoured exactly. Do not design a system that requires second-level accuracy from DNS.
Negative caching
Caches also remember that a name or record does not exist. An NXDOMAIN answer (the name does not exist) or a NODATA answer (the name exists but has no record of that type) is cached too. The duration comes from the SOA record's last field, the negative-caching TTL (capped by the SOA record's own TTL), as defined in RFC 2308.
This causes a common surprise. You visit new.example.com before creating the record, and the lookup fails. You then create the record, and your own machine or resolver keeps saying the name does not exist until the negative entry expires. In the zone above, that is up to 3600 seconds.
The practical rule: create the record first, then test it. If you tested too early, flush your local caches or query a resolver that has not seen the failed name.
Caching of delegations
NS records and their glue address records are cached as well. When you change name servers at your registrar, two TTLs are in play:
- The parent (the TLD registry) publishes the delegation NS records, and its TTL is often 1 to 2 days. Registries set this, not you.
- The child (your own zone) also holds NS records, with a TTL you control.
A resolver may keep using the old name servers until the cached delegation expires. This is why moving a domain between DNS providers can take up to 48 hours to settle for some users, even though your records have a short TTL. To reduce risk, keep the old provider's zone running, with the same records, until the delegation TTL has passed.
Checking what is happening
Compare what the authoritative server says with what a resolver says. First find the authoritative servers, then query one directly. A query to an authoritative server shows the true value and its full configured TTL.
dig example.com NS +short
dig @ns1.example.com example.com A +noall +answer
dig @1.1.1.1 example.com A +noall +answer
dig @8.8.8.8 example.com A +noall +answerHow to read the results:
- If the authoritative answer is wrong, the change was not saved or not published. Cache is not the issue.
- If the authoritative answer is right but a resolver shows the old value, wait for the remaining TTL on that resolver's answer. The number tells you how long.
- If different resolvers disagree, they cached at different times. This is normal during the window.
Your own program can show a further layer. The operating system resolver and any application caching sit between your code and the network:
import socket
for family, _, _, _, sockaddr in socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM):
print(family.name, sockaddr[0])If dig shows the new address but this script prints the old one, the operating system cache or a hosts-file entry is responsible. Python itself does not cache here, but other runtimes may.
Common errors
- Lowering the TTL and changing the value together. Caches holding the old long-TTL copy are not affected. Lower first, wait, then change.
- Testing before creating the record. This fills caches with a negative answer.
- Forgetting the SOA serial on a self-managed primary and secondary setup. Secondaries do not transfer an unchanged serial, so they keep serving the old zone.
- Blaming the browser too early. Browsers, the OS, and a VPN's own resolver each add a cache. Check with
digagainst a named resolver to separate them. - Leaving very low TTLs forever. A TTL of 60 on every record multiplies query volume and makes your site more exposed to a DNS provider outage, because caches cannot ride through it.
- Switching providers before the delegation TTL has passed. Shutting down the old zone early sends part of your audience to servers that no longer answer.
Exercise
Your site's A record has a TTL of 86400 and you must move it to a new server tomorrow at 09:00 with at most five minutes of mixed answers. When should you lower the TTL, to what, and when do you change the address?
Show answer
Lower the TTL to 300 at least 24 hours before the move, so by 09:00 tomorrow or earlier (for example 09:00 today or before), because caches may hold a copy fetched just before the lowering for up to 86400 seconds. Leave the address unchanged until that old TTL has passed. At 09:00 tomorrow, change the address. Caches then pick up the new value within 300 seconds.
Exercise
You create shop.example.com after trying to visit it, and your machine still reports that the name does not exist 20 minutes later. The SOA negative-caching TTL is 3600. What is happening, and what can you do?
Show answer
The failed lookup was cached as a negative answer (NXDOMAIN) for up to 3600 seconds. The record exists on the authoritative server, but the cache has not expired. Flush the local and OS cache, or query a resolver that has not cached the failure, and in future create the record before testing.
Exercise
dig @ns1.example.com example.com A returns 192.0.2.99, but dig @1.1.1.1 example.com A returns 192.0.2.10 with a TTL of 1200. What should you expect, and when?
Show answer
The authoritative server has the new value. The resolver is serving a cached old value and will keep doing so for about 1200 more seconds (20 minutes). After that it will fetch 192.0.2.99.