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.

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 servers with queries just because some CDN wants to load-balance. They should find a better means of load balancing.