Skip to content

reverseproxy: fix broken reservoir sampling in random_choose policy - #7873

Merged
steadytao merged 2 commits into
caddyserver:masterfrom
TowyTowy:fix/random-choose-reservoir
Jul 11, 2026
Merged

steadytao merged 2 commits into
caddyserver:masterfrom
TowyTowy:fix/random-choose-reservoir

Conversation

@TowyTowy

Copy link
Copy Markdown
Contributor

The random_choose policy is meant to implement power-of-d-choices: sample d available upstreams uniformly, then pick the least-loaded of the sample. The sampling loop was not a correct reservoir sample (Algorithm R): it never filled the first k reservoir slots unconditionally — every candidate was written to a random slot j = rand(i+1), which can evict an earlier candidate while leaving another slot nil — and j was derived from the upstream's pool index rather than the number of available upstreams seen, skewing the sample whenever down upstreams precede up ones.

The practical effect is that the least-load comparison frequently never happens. Most visibly, with two upstreams and random_choose 2 (the canonical power-of-two-choices setup), half of all requests go to the more-loaded upstream even when it is saturated and the other idle — measurably identical to plain random (simulated over 100k selections: 50.2% to a backend with 100 in-flight requests vs. an idle one). With 3 upstreams and one down, 33% of requests go to the saturated backend. The nil slots this leaves behind were also the root cause of the panic in #3810, which was patched by skipping nils in leastRequests rather than by fixing the sampling.

This PR replaces the loop with a standard Algorithm R reservoir sample over available upstreams: fill the first k slots, then replace a random slot with probability k/n. Every available upstream is now sampled with uniform probability k/n (verified by simulation) and the reservoir always holds min(k, available) candidates. Includes a regression test that is deterministic after the fix (with 2 available upstreams and choose=2, the least-loaded upstream must always win) and fails immediately before it.

Disclosure per CONTRIBUTING: this bug was found, and the patch and tests were written, with the assistance of an LLM (Claude, by Anthropic), under human direction and review. I've verified the fail-before/pass-after locally and can explain every line.

TowyTowy and others added 2 commits July 12, 2026 00:41
The random_choose selection policy is meant to implement
power-of-d-choices: sample d available upstreams uniformly, then pick
the least-loaded of that sample. The sampling loop, however, was not a
correct reservoir sample (Algorithm R): it never filled the first k
reservoir slots unconditionally, instead writing every candidate to a
random slot j = rand(i+1), which can evict an earlier candidate while
leaving another slot nil. It also derived j from the upstream's index
in the pool rather than from the number of available upstreams seen,
skewing the sample whenever unavailable upstreams precede available
ones.

As a result the reservoir frequently held fewer than min(k, available)
upstreams, so the least-load comparison often never happened. Most
notably, with two upstreams and 'random_choose 2' (the canonical
power-of-two-choices setup), half of all requests were routed to the
more-loaded upstream even when it was saturated and the other idle --
identical behavior to plain 'random'. The nil slots this leaves behind
were also the cause of the panic reported in caddyserver#3810, which was patched
by skipping nils in leastRequests rather than by fixing the sampling.

Replace the loop with a standard Algorithm R reservoir sample over the
available upstreams: fill the first k slots, then replace a random slot
with probability k/n. Every available upstream is now sampled uniformly
and the reservoir always holds min(k, available) candidates.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This correctly applies reservoir sampling over available upstreams and the regression covers the original random_choose 2 failure.

@steadytao
steadytao merged commit b2be548 into caddyserver:master Jul 11, 2026
26 checks passed
@steadytao steadytao added this to the v2.11.5 milestone Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants