One of the devices on my network is a helium miner and the success of that depends on quickly resolving the DNS query.
When the pi resolves the query from cache it is very fast and the mining is successful, but when it goes to localhost it is only sometimes fast enough, and when it uses an external upstream DNS service it is too slow.
The query only ever contacts one URL, so is there a way to permanently cache this address?
Not quite, as caching of replied DNS records is controlled by TTLs as issued by a given domain's authoritative DNS servers.
But it would be possible to shadow a public domain by creating a Local DNS record for it within Pi-hole.
This would be a permanent entry, though there's a serious twist:
If the public DNS record ever changes, your local network will never pick up that change - it will continue to use the shadow local DNS record, i.e. your clients may thus be talking to a wrong IP.
Alternatively, you may configure Pi-hole (or an upstream unbound
, if that happens to be your Pi-hole's localhost upstream) to serve stale cached records with an artificially lowered TTL while the domain is resolved anew in the background.
Could you give me some more information on how the second option might work?
I do use unbound locally....
For unbound
, you may add the following to your existing server
configuration:
server:
prefetch: yes
serve-expired: yes
serve-expired-ttl: 3600 # one hour, in seconds
Note that the initial server
above is just included for completeness. It will be already present in your existing unbound
configuration file, so you just insert the indented three lines into your file's server
section.
Alternatively, you could create a custom dnsmasq configuration file for Pi-hole (e.g. /etc/dnsmasq.d/42-serve-expired.conf
) to the same effect, with the following contents:
use-stale-cache=3600
This comes at the expense of returning expired and potentially wrong cached records as well as less efficient cache utilisation, since old data cannot be flushed when its TTL expires.
The TTL of the stale replies will be 0 or 30 seconds (depending on your version of unbound
), so a client would keep using stale data for 30 seconds at most before sending a new DNS request, by which time Pi-hole/unbound
would have refreshed the record with current data.