folbricht / routedns

DNS stub resolver, proxy and router with support for DoT, DoH, DoQ, and DTLS
BSD 3-Clause "New" or "Revised" License
476 stars 63 forks source link

how to always serve cached response #377

Closed bdantas closed 7 months ago

bdantas commented 7 months ago

I'm a happy user of routedns in my home's Tiny Core Linux-powered wireless router. This is my current config file:

# Configuration for a local proxy with cache, forwarding everything via DNS-over-TLS

[resolvers.mullvad-dot]
address = "194.242.2.6:853"
server-name = "family.dns.mullvad.net"
protocol = "dot"

[groups.mullvad-cached]
type = "cache"
resolvers = ["mullvad-dot"]
cache-flush-query = "flush.cache."
backend = {type = "memory", size = 10000, filename="/opt/routedns.cache", save-interval = 1800}

[listeners.local-udp]
address = "192.168.30.1:53"
protocol = "udp"
resolver = "mullvad-cached"

[listeners.local-tcp]
address = "192.168.30.1:53"
protocol = "tcp"
resolver = "mullvad-cached"

In the interest of making my LAN users happy with very snappy page load times, I'm trying to make dns queries be always answered from cache even if cache entry is stale based on TTL. Is there a way to configure routedns so that cached records are always served optimistically, while the record is refreshed from upstream resolver and cached again in parallel?

cbuijs commented 7 months ago

Check the cache-prefetch-trigger and cache-prefetch-eligible to keep the cache ready and up-2-date.

Example

bdantas commented 7 months ago

Yes! Keeping the cache fresh by prefetching nearly-stale entries is an even better strategy than what I had in mind. Thank you.

folbricht commented 7 months ago

You may also want to look at perhaps adding a TTL-Modifier in front of the cache (between cache and resolver), that let's you set a min-TTL for all records, giving you more control over when the refresh happens.

bdantas commented 7 months ago

Thanks, folbricht. I edited my config file accordingly. Am I correct that with this config I ensure cache entries will have initial TTL of at least an hour, all cache entries will be eligible for prefetch, and prefetch happens when entry has 10 minutes or less of TTL remaining? Is this a reasonable config for general household internet usage?

[resolvers.mullvad-dot]
address = "194.242.2.6:853"
server-name = "family.dns.mullvad.net"
protocol = "dot"

[groups.mullvad-updated-ttl]
type = "ttl-modifier"
resolvers = ["mullvad-dot"]
ttl-min = 3600

[groups.mullvad-cached]
type = "cache"
resolvers = ["mullvad-dot"]
cache-flush-query = "flush.cache."
cache-prefetch-trigger = 600
cache-prefetch-eligible = 3000
backend = {type = "memory", size = 10000, filename="/opt/routedns.cache", save-interval = 1800}

[listeners.local-udp]
address = "192.168.10.1:53"
protocol = "udp"
resolver = "mullvad-cached"

[listeners.local-tcp]
address = "192.168.10.1:53"
protocol = "tcp"
resolver = "mullvad-cached"
folbricht commented 7 months ago

Almost, just one small issue, this config bypasses the ttl-modifier, you just need a small change to the cache config to actually use the modifier

[groups.mullvad-cached]
type = "cache"
resolvers = ["mullvad-updated-ttl"]
...
bdantas commented 7 months ago

Ah, I understand how it works. Thank you.

routedns is amazing. Thank you for creating it. Happy hacking!