Caching and TTLs
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.
Before you start
Section titled “Before you start”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.
Ask twice and compare the number
Section titled “Ask twice and compare the number”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.
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:~$ Steps (8)
- Type
dig web.lab.test +noall +answer - Press Enter
- Output:
web.lab.test. 300 IN A 10.77.20.80 - Type
sleep 3 - Press Enter
- Type
dig web.lab.test +noall +answer - Press Enter
- 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.
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:~$ Steps (10)
- Type
sudo tcpdump -n -i eth0 port 53 - Press Enter
- Output:
[sudo] password for learner: - Type the password (the terminal shows nothing)
- Press Enter
- Output:
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes - In the second terminal on laptop, look up the name twice with a short pause. Then return here and stop the listener.
- 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) - Press Ctrl + C
- 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.
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:~$ Steps (33)
- Type
netlab shell laptop - Press Enter
- Output:
[sudo] password for learner: - Type the password (the terminal shows nothing)
- Press Enter
- Type
dig web.lab.test +noall +answer - Press Enter
- Output:
web.lab.test. 300 IN A 10.77.20.80 - Type
exit - Press Enter
- Output:
logout - Type
netlab break dns-change - Press Enter
- Output:
Fault dns-change applied. - Type
netlab shell laptop - Press Enter
- Type
dig web.lab.test +noall +answer - Press Enter
- Output:
web.lab.test. 295 IN A 10.77.20.80 - Type
dig @10.77.20.54 web.lab.test +noall +answer - Press Enter
- Output:
web.lab.test. 300 IN A 10.77.20.81 - Type
sleep 305 - Press Enter
- Type
dig web.lab.test +noall +answer - Press Enter
- Output:
web.lab.test. 300 IN A 10.77.20.81 - Type
exit - Press Enter
- Output:
logout - Type
netlab fix - Press Enter
- 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.
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.
Ask for a name that is missing
Section titled “Ask for a name that is missing”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.
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:~$ Steps (8)
- Type
dig nope.lab.test +noall +comments +authority - Press Enter
- 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 - Type
sleep 3 - Press Enter
- Type
dig nope.lab.test +noall +comments +authority - Press Enter
- 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.
Mistakes to recognize
Section titled “Mistakes to recognize”“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.
Good to know
Section titled “Good to know”Two resolvers can store a record at different moments. Use the time each one stored it to work out when each copy runs out.
Try it yourself
Section titled “Try it yourself”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.
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:~$ Steps (8)
- Type
dig web.lab.test +noall +answer - Press Enter
- Output:
web.lab.test. 300 IN A 10.77.20.80 - Type
sleep 6 - Press Enter
- Type
dig web.lab.test +noall +answer - Press Enter
- 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.