OpenWRT One: Enable DNS Stale Cache

If you want to enable the dnsmasq stale cache, you’ll likely be directed by internet instruction to enter:

option use_stale_cache '1296000'

… under the config dnsmasq heading in /etc/config/dhcp.

Except that doesn’t work. If you query the actual dnsmasq.conf file that dnsmasq uses:

grep stale /var/etc/dnsmasq.conf*

… it’ll return blank, because while dnsmasq (and uci) will allow you to set that entry, it doesn’t pick that entry up and enter it into the working dnsmasq.conf file.

In my case, that dnsmasq.conf file (dynamically generated from the uci settings) sits at:

/var/etc/dnsmasq.conf.cfg01411c

But… you can enable the dnsmasq stale cache.

In /etc/dnsmasq.conf, enter:

# Enable Stale Cache
use-stale-cache=1296000

… then restart dnsmasq:

/etc/init.d/dnsmasq restart

Now, to test it. Issue:

dig example.org @192.168.1.1

… which should return something like:

; <<>> DiG 9.20.30 <<>> example.org @192.168.1.1
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30382
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;example.org.			IN	A

;; ANSWER SECTION:
example.org.		3600	IN	A	104.20.26.136
example.org.		3600	IN	A	172.66.157.237

;; Query time: 20 msec
;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)
;; WHEN: Tue Nov 17 20:42:40 CST 2026
;; MSG SIZE  rcvd: 72

The relevant bits:

example.org.		3600	IN	A	104.20.26.136
example.org.		3600	IN	A	172.66.157.237

… I’ve got my Network >> DNS >> Limits tab >> Min cache TTL setting at 3600 seconds. That’ll count down to 0 at a rate of 1 per second.

Without the stale cache enabled, once that counter reaches 0, that domain is flushed from the dnsmasq cache, necessitating another fetch out to the external DNS servers on a subsequent query.

With the stale cache, dnsmasq will hold that domain in its database for the duration of the stale cache timeout (in my case, 1296000 seconds), and return that entry upon queries… then (once Min Cache TTL expires) it’ll update the entry by fetching from the external DNS servers, and reset that Min cache TTL (in my case, to 3600).

Now, what’s the advantage of that, you may ask?

Well, there’s that other relevant bit:

;; Query time: 20 msec

That is reduced to essentially 0 msec (ie: in the microsecond range) because dnsmasq is fetching from its database, rather than from the external DNS servers.

Subsequent queries will all show:

;; Query time: 0 msec

So to test it, do subsequent queries:

dig example.org @192.168.1.1

… and watch that 3600 count down to 0. Once it reaches 0, do another query… you should still see 0 msec query time (and you should see it reset Min Cache TTL (in my case, to 3600 seconds)). If you see that, your stale cache is working.

That’s not a lot of time, but consider, for example, gamers who require latency as low as possible. Further, it helps to temporally offset DNS queries such that fingerprinting users on the lan via their DNS lookups is more difficult.

The disadvantage is that if a website moves, it’ll take an hour before dnsmasq gets the new IP address… that’s an acceptable tradeoff to me, considering that short TTL times were originally devised to allow a website that is moving to get its new IP address out there pretty quickly… they just set their domain’s TTL to a really short time, then move to the new IP address, let that run for awhile so everyone gets the new IP address, then set their domain’s TTL higher… but nowadays, it’s used more for load balancing.

It’s not my (nor my dnsmasq’s) job to flog the external DNS servers with queries just because some CDN wants to load-balance. They should find a better means of load balancing.

1 Like

An alternative method, if you’ve already followed the procedure to enable DNS bit 0x20 encoding, is to issue:

uci set dhcp.@dnsmasq[0].extraconftext='do-0x20-encode\nuse-stale-cache=1296000'
uci commit dhcp
/etc/init.d/dnsmasq restart

… that’ll add the following to /etc/config/dhcp:

	option extraconftext 'do-0x20-encode\nuse-stale-cache=1296000'

… under the:

config dnsmasq

… heading… and in the /etc/dnsmasq.d/extraconfig.conf file, it’ll look like this:

do-0x20-encode
use-stale-cache=1296000

Since that /etc/dnsmasq.d/extraconfig.conf file already exists, we can add additional settings to it. Be sure to remove the use-stale-cache=1296000 setting from /etc/dnsmasq.conf (set up in the post above), or dnsmasq will go into a crash loop… dnsmasq doesn’t like being told twice what it’s supposed to do.

If you forget to remove that setting in /etc/dnsmasq.conf, you’ll see this in the logs:

daemon.crit: dnsmasq[1]: illegal repeated keyword at line 2 of /etc/dnsmasq.d/extraconfig.conf
daemon.crit: dnsmasq[1]: FAILED to start up

Of course, if you don’t have DNS bit 0x20 encoding enabled, you can still use this technique… just strip out the do-0x20-encode\n bit in the uci set command above.

uci set dhcp.@dnsmasq[0].extraconftext='use-stale-cache=1296000'
uci commit dhcp
/etc/init.d/dnsmasq restart

Long term, you can ascertain that your stale cache is working by issuing:

ubus call dnsmasq metrics

… which should return something like:

{
	"dns_cache_inserted": 101379,
	"dns_cache_live_freed": 0,
	"dns_queries_forwarded": 29735,
	"dns_auth_answered": 0,
	"dns_local_answered": 47691,
	"dns_stale_answered": 16742,
	"dns_unanswered": 0,
	"dnssec_max_crypto_use": 24,
	"dnssec_max_sig_fail": 0,
	"dnssec_max_work": 8,
	"bootp": 0,
	"pxe": 0,
	"dhcp_ack": 64,
	"dhcp_decline": 0,
	"dhcp_discover": 21,
	"dhcp_inform": 1,
	"dhcp_nak": 0,
	"dhcp_offer": 21,
	"dhcp_release": 0,
	"dhcp_request": 63,
	"noanswer": 0,
	"leases_allocated_4": 18,
	"leases_pruned_4": 2,
	"leases_allocated_6": 0,
	"leases_pruned_6": 0,
	"tcp_connections": 0,
	"dhcp_leasequery": 0,
	"dhcp_lease_unassigned": 0,
	"dhcp_lease_actve": 0,
	"dhcp_lease_unknown": 0
}

Note the entry: “dns_stale_answered”: 16742,