AdGuardHome icon indicating copy to clipboard operation
AdGuardHome copied to clipboard

biased average upstream response time when optimistic caching is enabled

Open az0 opened this issue 5 months ago • 3 comments

Prerequisites

Platform (OS and CPU architecture)

Linux, ARMv7

Installation

Custom package (OpenWrt, HomeAssistant, etc; please mention in the description)

Setup

On one machine

AdGuard Home version

0.107.74

Action

I enabled optimistic caching in AGH.

Expected result

I expected that "Average upstream response time for the last 24 hours" on my AGH dashboard (i.e., existing functionality) would calculate an unbiased average that represents all DNS query results used by AGH.

Actual result

The response times are abnormally too high (i.e., too slow) by the order of ballpark 2x to 4x.

Additional information and/or screenshots

When AGH processes a request that is a cache hit, it performs a normal DNS lookup upstream: these are the only kinds of requests that are used to calculate the average. This works fine when optimistic caching is disabled in the configuration.

When optimistic caching is enabled and AGH processes an optimistic cache hit, the AGH server returns an answer immediately while starting a background thread to refresh the stats. However, the results of this DNS query are not used to calculate the "average upstream response time."

I'm not familiar with your codebase or go, but the issue may be here involving the cloned context.

 if dctxCache.optimistic && expired {
        go p.shortFlighter.resolveOnce(minCtxClone, key, p.logger)
    }

And maybe here because IsCached is true.

func (u *unit) add(e *Entry) {
    for _, s := range e.UpstreamStats {
        if s.IsCached || s.Error != nil {
            continue
        }
        // ... record upstream response time
    }
}

The reason that the AGH dashboard shows strangely high averages is because the two scenarios involve different populations of hostnames.

  • Optimistic caching hits tend to be hostnames that are common both on AGH's LAN but also universally common. For example: google.com, windowsupdate.com, itunes.apple.com. The upstream has these cached too, so upstream processes them fast

  • AGH cache misses tend to be rarer hostnames like duokao.net and dwr.virginia.gov. Upstream probably does not have these cached, so it responds slower.

The statistics calculated with current behavior neither serves to answer either of these useful questions:

  • How fast are upstreams?
  • How long are downstreams waiting for AGH to resolve queries?

Therefore, the current statistics are more harmful than helpful.

az0 avatar Jun 15 '26 00:06 az0

I can reproduce this. I’ll forward it to the dev team.

GulnazSalakhova avatar Jun 15 '26 11:06 GulnazSalakhova

Hello @az0, and thank you for the thorough report. This is actually a non-trivial feature request, as implementing it would require significant changes to the external library (dnsproxy). We'll consider implementing it in a future release.

EugeneOne1 avatar Jul 09 '26 15:07 EugeneOne1

thank you for the thorough report.

You're welcome

This is actually a non-trivial feature request, as implementing it would require significant changes to the external library (dnsproxy).

I understand.

By the way, I enabled parallel requests, and I assume that it counts only the fastest response. However, it would be interesting to have more visibility into parallel requests, like how often each upstream wins. This isn't an official request for that feature, though, but perhaps it would be related to the optimistic caching issue.

We'll consider implementing it in a future release.

Thank you for considering that, and thank you for the helpful software!

az0 avatar Jul 11 '26 22:07 az0