Repository navigation
reverseproxy: fix broken reservoir sampling in random_choose policy - #7873
Merged
steadytao merged 2 commits intoJul 11, 2026
Merged
Conversation
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
approved these changes
Jul 11, 2026
steadytao
left a comment
Member
There was a problem hiding this comment.
LGTM. This correctly applies reservoir sampling over available upstreams and the regression covers the original random_choose 2 failure.
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
random_choosepolicy 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 slotj = rand(i+1), which can evict an earlier candidate while leaving another slot nil — andjwas 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 plainrandom(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 inleastRequestsrather 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.