AdguardTeam / dnsproxy

Simple DNS proxy with DoH, DoT, DoQ and DNSCrypt support
Apache License 2.0
2.44k stars 247 forks source link

Optimistic/Pessimistic cache #272

Closed iJorgen closed 2 years ago

iJorgen commented 2 years ago

I'm a bit confused... ;-) I read that "optimistic caching" in general means that it first tries online before using a cached result and "pessimistic caching" is using a cached result before going online. Is that correct for DNSproxy too?

If I have optimistic caching turned on and I can see the same queries in the DNS-log upstream several times within seconds if I start the same app on different devices. So it queries the upstream servers each time and don't primarily use the cache?! Would it be better to not use optimistic caching to make it use the cached results first?! I wanna do "pessimistic caching" to use the cache first.

Found a site describing it. Not about DNS, but the same principles... https://itnext.io/caching-in-a-pwa-when-to-use-optimistic-vs-pessimistic-d627a5943990

agneevX commented 2 years ago

Optimistic caching in dnsproxy returns a result from the cache to the client immediately (with a TTL of 10 if expired), then attempts to refresh the record. If it cannot refresh it, it continues to serve the stale record.

saint-lascivious commented 2 years ago

The optimism in context comes from serving possibly expired records on an "eh, whatever, YOLO" basis on the probability that the cached record will probably still be correct, while refreshing that record in the background if possible so even if it's wrong at that moment the next query won't be.

Various other implementations may also add finer controls determining what the flow should look like if that record can't be refreshed. Such as whether to drop it entirely or continue to serve the record you have as long as it's still being queried, what its TTL will be, etc.

iJorgen commented 2 years ago

I think I just found the answer under AdGuard Home... Seems like "Optimistic cache" still does an upstream-query every time even if the result is found/served from the cache and it's not a stale record. I thought it would only query upstream if it's a stale record to refresh the cache. The question is if it detects any changes upstream on this query and updates the local cache, or it's just a wasted DNS-query?! That would be really smart...

Does setting "cache-max-ttl" have an effect how long a stale record is served or it can live in the cache for weeks/months as long as it's not filled up?! It also means that the only way to reduce upstream queries is to increase "cache-min-ttl".

https://github.com/AdguardTeam/AdGuardHome/issues/4015