Skip to content

Caching and TTLs

Module
How DNS Finds Services
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS, netlab 1.1, GNU Bash 5.2.21, tcpdump 4.99.4
On this page

You change a DNS record. One person gets the changed address and another still gets the old one. Compare the two answers and find the number that tells you how long the old one can last.

Keep DNS mode on as in Names, zones, and records. Exit the machine shells and run netlab reset from the VM before each experiment in this lesson. A reset clears stored answers so you can see what happens on the first lookup.

After a reset, open two VM terminals. In the first, enter dns and run sudo tcpdump -n -i eth0 port 53. Enter your Ubuntu password when asked. Leave the listener running for both lookups.

In the second terminal, enter laptop and run dig web.lab.test +noall +answer. Run sleep 3 to pause for three seconds, then repeat the lookup. Return to the listener and press Ctrl+C. Read the answers on laptop first. Compare the number immediately after the name.

Ask twice and compare the remaining seconds
learner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		300	IN	A	10.77.20.80learner@laptop:~$ sleep 3learner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		296	IN	A	10.77.20.80learner@laptop:~$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, networking lab (netlab 1.1), GNU Bash 5.2.21. Both lookups give the same address. The second answer has fewer seconds left.
Steps (8)
  1. Type dig web.lab.test +noall +answer
  2. Press Enter
  3. Output: web.lab.test. 300 IN A 10.77.20.80
  4. Type sleep 3
  5. Press Enter
  6. Type dig web.lab.test +noall +answer
  7. Press Enter
  8. Output: web.lab.test. 296 IN A 10.77.20.80

The first answer gives 300 seconds. The second gives a lower number. Your remaining count will depend on how long you wait. The time to live (TTL) of a record tells you how many seconds a stored answer may still be used. A cache is a place to keep answers for reuse. The resolver dns kept the first answer and returned the remaining time in its second reply.

Listen while the resolver uses the stored answer

Section titled “Listen while the resolver uses the stored answer”

Read the listener output from the same run. Find the question from laptop that begins each lookup. The source port on laptop changes. The destination port on dns stays at 53.

Listen across the first lookup and the cached lookup
learner@dns:~$ sudo tcpdump -n -i eth0 port 53[sudo] password for learner: tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes14:15:45.016455 IP 10.77.10.10.39697 > 10.77.20.53.53: 4107+ [1au] A? web.lab.test. (53)
14:15:45.016613 IP 10.77.20.53.52803 > 10.77.20.54.53: 63553+ [1au] A? web.lab.test. (53)
14:15:45.016736 IP 10.77.20.54.53 > 10.77.20.53.52803: 63553*- 1/1/1 A 10.77.20.80 (90)
14:15:45.016836 IP 10.77.20.53.53 > 10.77.10.10.39697: 4107* 1/1/1 A 10.77.20.80 (90)
14:15:49.052754 IP 10.77.10.10.39197 > 10.77.20.53.53: 34601+ [1au] A? web.lab.test. (53)
14:15:49.052857 IP 10.77.20.53.53 > 10.77.10.10.39197: 34601 1/0/1 A 10.77.20.80 (57)^C6 packets captured
6 packets received by filter
0 packets dropped by kernellearner@dns:~$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, networking lab (netlab 1.1), GNU Bash 5.2.21. One listener records the trip to the zone server and then a reply from the cache.
Steps (10)
  1. Type sudo tcpdump -n -i eth0 port 53
  2. Press Enter
  3. Output: [sudo] password for learner:
  4. Type the password (the terminal shows nothing)
  5. Press Enter
  6. Output: tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
  7. In the second terminal on laptop, look up the name twice with a short pause. Then return here and stop the listener.
  8. Output: 14:15:45.016455 IP 10.77.10.10.39697 > 10.77.20.53.53: 4107+ [1au] A? web.lab.test. (53) 14:15:45.016613 IP 10.77.20.53.52803 > 10.77.20.54.53: 63553+ [1au] A? web.lab.test. (53) 14:15:45.016736 IP 10.77.20.54.53 > 10.77.20.53.52803: 63553*- 1/1/1 A 10.77.20.80 (90) 14:15:45.016836 IP 10.77.20.53.53 > 10.77.10.10.39697: 4107* 1/1/1 A 10.77.20.80 (90) 14:15:49.052754 IP 10.77.10.10.39197 > 10.77.20.53.53: 34601+ [1au] A? web.lab.test. (53) 14:15:49.052857 IP 10.77.20.53.53 > 10.77.10.10.39197: 34601 1/0/1 A 10.77.20.80 (57)
  9. Press Ctrl + C
  10. Output: 6 packets captured 6 packets received by filter 0 packets dropped by kernel

The first lookup takes four lines. The resolver dns asks ns for the record and passes back the answer. The second lookup takes two lines. The question from laptop is followed by a reply from dns. The listener sees no trip to ns for that lookup.

Change a record while the old answer is stored

Section titled “Change a record while the old answer is stored”

Stop the listener and exit both machine shells before resetting. Enter laptop, look up the web address, then exit. On the VM run netlab break dns-change. It changes the record on ns while dns keeps its stored answer. Enter laptop again. Ask through the usual setting, then use @ to ask ns directly.

Predict. Right after the change, which server gives the new answer?

After comparing the first two answers, wait about five minutes. Use the remaining count printed by dns and give it a few extra seconds. Ask through dns again. Exit laptop and run netlab fix from the VM to restore the original lab.

Compare answers before and after the stored answer expires
learner@rootvolt:~$ netlab shell laptop[sudo] password for learner: learner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		300	IN	A	10.77.20.80learner@laptop:~$ exitlogoutlearner@rootvolt:~$ netlab break dns-changeFault dns-change applied.learner@rootvolt:~$ netlab shell laptoplearner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		295	IN	A	10.77.20.80learner@laptop:~$ dig @10.77.20.54 web.lab.test +noall +answerweb.lab.test.		300	IN	A	10.77.20.81learner@laptop:~$ sleep 305learner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		300	IN	A	10.77.20.81learner@laptop:~$ exitlogoutlearner@rootvolt:~$ netlab fixFault dns-change removed.learner@rootvolt:~$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, networking lab (netlab 1.1), GNU Bash 5.2.21. The stored and direct answers differ after the change. A lookup after the wait picks up the changed address.
Steps (33)
  1. Type netlab shell laptop
  2. Press Enter
  3. Output: [sudo] password for learner:
  4. Type the password (the terminal shows nothing)
  5. Press Enter
  6. Type dig web.lab.test +noall +answer
  7. Press Enter
  8. Output: web.lab.test. 300 IN A 10.77.20.80
  9. Type exit
  10. Press Enter
  11. Output: logout
  12. Type netlab break dns-change
  13. Press Enter
  14. Output: Fault dns-change applied.
  15. Type netlab shell laptop
  16. Press Enter
  17. Type dig web.lab.test +noall +answer
  18. Press Enter
  19. Output: web.lab.test. 295 IN A 10.77.20.80
  20. Type dig @10.77.20.54 web.lab.test +noall +answer
  21. Press Enter
  22. Output: web.lab.test. 300 IN A 10.77.20.81
  23. Type sleep 305
  24. Press Enter
  25. Type dig web.lab.test +noall +answer
  26. Press Enter
  27. Output: web.lab.test. 300 IN A 10.77.20.81
  28. Type exit
  29. Press Enter
  30. Output: logout
  31. Type netlab fix
  32. Press Enter
  33. Output: Fault dns-change removed.
Check your answer

The server ns gives 10.77.20.81 as soon as you ask it after the change. The resolver dns still gives 10.77.20.80 at first. After the stored answer runs out, the next lookup through dns gives 10.77.20.81 too.

In this run, the old answer has 295 seconds left. The direct answer from ns shows 300. Your remaining count will differ.

When the changed address becomes available Both lanes share a proportional axis in seconds. At zero, dns stores 10.77.20.80 for 300 seconds. The record changes around 5 seconds. A client asking ns can then get 10.77.20.81. A client asking dns gets 10.77.20.80 until expiry at 300 seconds. The lookup after the wait gets 10.77.20.81 from dns. 0 s: Stored for 300 s 5 s: Change 300 s: Expires Lookup after wait A client asking ns 10.77.20.81 from the change A client asking dns 10.77.20.80 until expiry 10.77.20.81 after expiry
When the changed address becomes available Both lanes share a proportional axis in seconds. At zero, dns stores 10.77.20.80 for 300 seconds. The record changes around 5 seconds. A client asking ns can then get 10.77.20.81. A client asking dns gets 10.77.20.80 until expiry at 300 seconds. The lookup after the wait gets 10.77.20.81 from dns. 0 s: Stored for 300 s 5 s: Change 300 s: Expires Lookup after wait A client asking ns 10.77.20.81 from the change A client asking dns 10.77.20.80 until expiry 10.77.20.81 after expiry
Both lanes share a time axis. The zone server gives the changed address at the change, and the resolver can give it after the stored answer expires.

Start the timeline when dns stores the first answer. The old answer has 295 of its 300 seconds left just after the change. That puts the change near 5 seconds. The last lookup follows the 305-second wait.

After the repair, enter laptop. Ask for nope.lab.test twice, with a short pause. Use +noall +comments +authority to keep the status and the section containing the SOA.

Ask twice for a missing name
learner@laptop:~$ dig nope.lab.test +noall +comments +authority;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 9370
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; AUTHORITY SECTION:
lab.test.		300	IN	SOA	ns.lab.test. hostmaster.ns.lab.test. 1795960166 1200 180 1209600 300learner@laptop:~$ sleep 3learner@laptop:~$ dig nope.lab.test +noall +comments +authority;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 45934
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; AUTHORITY SECTION:
lab.test.		296	IN	SOA	ns.lab.test. hostmaster.ns.lab.test. 1795960166 1200 180 1209600 300learner@laptop:~$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, networking lab (netlab 1.1), GNU Bash 5.2.21. Both replies say NXDOMAIN, and the number before IN on the SOA line falls.
Steps (8)
  1. Type dig nope.lab.test +noall +comments +authority
  2. Press Enter
  3. Output: ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 9370 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ;; AUTHORITY SECTION: lab.test. 300 IN SOA ns.lab.test. hostmaster.ns.lab.test. 1795960166 1200 180 1209600 300
  4. Type sleep 3
  5. Press Enter
  6. Type dig nope.lab.test +noall +comments +authority
  7. Press Enter
  8. Output: ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 45934 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ;; AUTHORITY SECTION: lab.test. 296 IN SOA ns.lab.test. hostmaster.ns.lab.test. 1795960166 1200 180 1209600 300

A negative answer says the requested name or record is missing. Here NXDOMAIN says the name is missing. The number immediately before IN on the SOA line is lower in the second reply. The resolver dns remembered the result. In this capture, the last field on the SOA line sets how long the name can be remembered as missing, matching the first reply’s TTL of 300 seconds.

“DNS changes spread across the Internet.” Old answers expire separately. In the replay, dns picks up the change when it looks up the record again after expiry.

“This is packet TTL.” The number here counts seconds. In The reply must return, the number on a packet drops as it crosses routers.

Two resolvers can store a record at different moments. Use the time each one stored it to work out when each copy runs out.

Suppose a resolver stores a record with a TTL of 120 seconds at 10:00:00. The record changes at 10:00:50. Work out when the old answer expires if it stays cached for its full lifetime.

Reset the lab and enter laptop. Look up web.lab.test with the short answer options. Wait about six seconds. Predict the number the second lookup will show, then repeat the same command.

Solution

The cached copy expires at 10:02:00. The cutoff depends on when the resolver stored the answer. The time of the change does not affect it. In the lab, expect about six fewer seconds on the second lookup. Your number depends on the time between requests.

Check the countdown with a longer pause
learner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		300	IN	A	10.77.20.80learner@laptop:~$ sleep 6learner@laptop:~$ dig web.lab.test +noall +answerweb.lab.test.		293	IN	A	10.77.20.80learner@laptop:~$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, networking lab (netlab 1.1), GNU Bash 5.2.21. Two lookups with a six-second pause show the drop in remaining time.
Steps (8)
  1. Type dig web.lab.test +noall +answer
  2. Press Enter
  3. Output: web.lab.test. 300 IN A 10.77.20.80
  4. Type sleep 6
  5. Press Enter
  6. Type dig web.lab.test +noall +answer
  7. Press Enter
  8. Output: web.lab.test. 293 IN A 10.77.20.80
  • Compare the remaining seconds before comparing changed answers.
  • The resolver can reuse a stored answer while its time remains.
  • A missing name can be remembered too.

Next, Investigating a lookup separates an answer from silence.