Why Your Off-Grid Cabin Needs a Local DNS Cache When the Link Drops
When the uplink at a remote cabin dies, almost nothing in the building actually stops working, but it all feels broken. The reason is usually DNS: every app waits for a resolver that can no longer reach anyone, and each one waits for several seconds before giving up.
A local caching resolver that is allowed to serve expired answers turns that into a much milder failure. Here is how to build one with Unbound, and what it cannot do.
What actually happens during an outage
Most devices point at the router, and the router forwards to something upstream (your ISP, 1.1.1.1, whatever). When the link is down, that forwarder has nothing to forward to. Depending on the router, you get one of these:
- silence, so the client waits for its own timeout
- a
SERVFAILafter a few seconds - an old cached answer, if the router happens to be a decent cache
glibc's stub resolver defaults to a 5 second timeout and 2 attempts per nameserver. So a dead resolver can cost a single lookup 10 seconds or more, and a web page with a dozen hostnames feels frozen even though the thing you wanted may be sitting on the LAN or in a browser cache.
Why the TTL works against you out here
DNS records expire on a timer set by whoever runs the zone. Plenty of sites use 60 to 300 seconds. On a reliable connection that is fine, because refetching is cheap. On a flaky rural link, an expired record is a lookup you cannot make, so the answer you had two minutes ago is now useless, even though the address almost certainly has not changed.
Quick detour: this is why short TTLs are a bit of a luxury good. They exist so operators can move traffic fast during failures, which is a fine goal for them and an annoying one for you.
A local resolver that serves stale answers
Unbound can keep expired records and hand them out when it cannot refresh them. This is "serve-stale", described in RFC 8767. The config is short:
# /etc/unbound/unbound.conf.d/cabin.conf
server:
interface: 0.0.0.0
access-control: 192.168.0.0/16 allow
access-control: 127.0.0.0/8 allow
cache-size: 128m
msg-cache-size: 64m
rrset-cache-size: 128m
# refresh popular records before they expire
prefetch: yes
# never trust a TTL below 5 minutes
cache-min-ttl: 300
# keep records for a day past expiry
serve-expired: yes
serve-expired-ttl: 86400
# try upstream for 1s, then fall back to the stale answer
serve-expired-client-timeout: 1000
serve-expired-reply-ttl: 30
forward-zone:
name: "."
forward-tls-upstream: yes
forward-addr: 1.1.1.1@853#cloudflare-dns.com
forward-addr: 9.9.9.9@853#dns.quad9.net
Adjust the access-control ranges to your own LAN. The forward-zone is optional; leave it out and Unbound will resolve recursively from the root, which works but sends more packets over a link you may be paying for by the gigabyte.
What each knob does
prefetch: when a record is close to expiry and gets queried, Unbound refreshes it in the background, so busy names stay warm.cache-min-ttl: overrides tiny TTLs. The Unbound docs advise keeping this modest (an hour at most), because a long floor means you follow changes slowly.serve-expired-ttl: how long past expiry a record may still be served.serve-expired-client-timeout: how long Unbound tries to refresh before answering with the stale copy. With the link up, answers stay fresh; with it down, clients wait about a second, not ten.serve-expired-reply-ttl: the TTL put on stale replies, so clients ask again soon and pick up the real answer once the link returns.
Setting the client timeout to 0 would answer stale immediately and refresh afterwards. That is snappier, but you risk serving a changed address for one extra query. For a cabin, I would take the one second wait.
Point the clients at it
Hand out the cache's address via DHCP, and make it the only nameserver. Listing a second, public resolver as a "backup" quietly defeats the whole thing, because clients may use it and then sit through its timeout.
Check it works:
dig @192.168.1.2 example.org | grep -E 'Query time|ANSWER:'
dig @192.168.1.2 example.org | grep 'Query time'
The second query should report 0 msec, because it came from cache. To test the outage path, unplug the uplink (or block upstream with a firewall rule), wait past the TTL, and query a name you looked up earlier. You should get an answer after roughly a second with a TTL of 30.
Where this stops helping
A cache only knows what it has seen. A name you have never looked up gets no help, and after a reboot the cache is empty, so a power cut followed by a dead link is the worst combination. Unbound can save and restore its cache:
unbound-control dump_cache > /var/lib/unbound/cache.dump
unbound-control load_cache < /var/lib/unbound/cache.dump
Run the dump from a timer and load it from a systemd unit after Unbound starts. It needs remote-control enabled in the config, and the dump is plain text, so it lists every site you visited. Treat that file as private.
It also cannot rescue anything that needs the actual internet. A resolved address for a server you cannot reach is still unreachable. What you gain is that LAN names, cached services and anything with local state stop adding a ten second stall on top.
The clock trap
Hang on, there is a nasty sibling to this. If you enable DNSSEC validation and the cabin's box has no battery-backed clock, a cold boot with no NTP can leave it believing it is 1970 or the last shutdown time. Signatures then look invalid or not yet valid, and everything returns SERVFAIL.
Fixes include a cheap RTC module, a GPS or local time source, or using a time daemon that steps the clock before Unbound starts. It is less glamorous than the cache config, but it is the one that bites at 2am after a power cut.
Add your own names while you are there
The same Unbound can answer for the LAN, so the printer, the battery monitor and the Home Assistant box resolve without leaving the building:
server:
local-zone: "cabin.lan." static
local-data: "pi.cabin.lan. IN A 192.168.1.10"
local-data: "solar.cabin.lan. IN A 192.168.1.20"
These never expire and never need the uplink, which is exactly the property you want for monitoring the equipment that keeps the lights on.