Repository navigation
fix(load-balancing): give a shared address the share of one backend - #1856
Merged
Merged
Conversation
Two backend ids at one address are distinct backends since #1839 (identity `(backend_id, address)`), but only HRW gave their address the share of one endpoint: round robin, random, least loaded, power of two and Maglev gave it one share per id (A,B@X + C@Y, equal weights: X took about 2/3 of the selections instead of 1/2). `BackendList` now hands every policy one candidate per address, after the tier, retry-exclusion and shard filters. The representative is the heaviest eligible id, the first in list order among equal weights (`outranks_at_address`), which is exactly the id HRW already picked, so HRW is unchanged and a down or backing-off id never hides an eligible sibling. The address weighs what its representative weighs. Maglev builds one table entry per distinct address, weighted by the heaviest of its ids. Shuffle sharding ranks and counts distinct primary addresses and puts every primary id of a selected address in the shard. A list without a shared address skips the collapse (flag maintained on the control plane); with one, the collapse is a quadratic scan of the candidates into a reserved scratch buffer, without allocating. Signed-off-by: Florentin Dubois <[email protected]>
…iants check_invariants now recomputes shares_address and asserts it matches, so a backend-list mutation that bypasses add_backend/remove_backend fails loudly instead of silently skipping the collapse. The CHANGELOG entry links #1856 and states that the keyless round-robin fallback of HRW and Maglev collapses too. Signed-off-by: Florentin Dubois <[email protected]>
The collapse compared every candidate with every other one and the shard ranking re-derived each address's representative by scanning the list, so one shared pair put every selection of the cluster on a quadratic path (release, round robin, 1000 backends: 3.9 us -> 2.5 ms). `refresh_siblings` now builds, on every list or configuration mutation, a sibling ring linking the ids of each address and each id's shard rank. The collapse walks only the rings of candidates that have a sibling, looking siblings up among the candidates by binary search, and compute_shard reads the precomputed ranks and walks rings to take in a shared address's other ids. A list without a shared address selects exactly as before; one with a shared pair is back to linear (1000 backends: 5.2 us). A test counts the shared-address work per selection and fails on a per-candidate scan. LEAST_LOADED and POWER_OF_TWO now compare an address's load as the sum of its ids' connection or request counts, walked through the same ring (`Candidates::siblings`), without allocating; CONNECTION_TIME still reads the representative's own average. Signed-off-by: Florentin Dubois <[email protected]>
`BackendList.backends` is public, so a library user may push or pop a backend without add_backend/remove_backend, leaving the sibling state of another length. The collapse is now skipped and compute_shard ranks every primary as its own address when that state's length differs from the list, and Candidates::with_siblings ignores a ring of another length, so a selection never indexes it out of bounds. Lists kept in sync select exactly as before. State the cost bound precisely: linear in the candidates plus, per shared address of g ids, up to g² ring steps (refresh_siblings: linear plus g² per shared address). Public docs of Candidates and Maglev no longer link the private outranks_at_address, which failed cargo doc under -D warnings. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 5, 2026
Since #1856 a BackendList derives its sibling ring, shuffle-sharding representatives and shared-address flag from the backend vector, and only add_backend/remove_backend refresh them (and the Maglev table). The field was public, so an embedder could push to or remove from the vector and leave that state stale. The field is now pub(crate) and BackendList::backends() returns a read-only slice. The one external reader in the workspace, the udp_health_descriptor_exhaustion integration test, uses the accessor. Two doctests on the accessor hold the guarantee: one reads the list, and an otherwise identical compile_fail,E0616 one pushes to the field from outside the crate. The length-mismatch guards from #1856 stay for in-crate misuse. Library API break, recorded in the CHANGELOG beside the BackendMap::max_failures removal (#1857). Signed-off-by: Florentin Dubois <[email protected]>
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.
Summary
Maintainer decision (2026-10-04): one address = one share. Two backend ids at the same address (distinct backends since #1839, identity
(backend_id, address)) now receive together the share of ONE backend under every policy, as HRW already gave them.Measured on
main(A,B@X + C@Y, equal weights, 20 000 selections throughBackendList): X share was RoundRobin 0.667, Random 0.666, LeastLoaded 0.667, PowerOfTwo 0.667, Maglev 0.668; HRW 0.5. After: 0.5 under all six.Rule
BackendListkeeps one candidate per address right before the policy runs (collapse_shared_addresses), i.e. after the tier, retry-exclusion and shard filters. A down / backing-off / excluded id therefore never hides an eligible sibling.weight), the first in list order among equal weights (outranks_at_addressinlib/src/load_balancing.rs). With equal weights this is "the first eligible id in list order". With unequal weights it is the heaviest, because that is what HRW has always returned (score-w/ln(h)is monotonic inwfor one address, ties keep the earlier backend); this keeps HRW's keyed path unchanged. The keyless round-robin fallback of HRW and Maglev collapses like RoundRobin. Flagging it because "first eligible" and "consistent with HRW" only agree at equal weights.k,shard_min_backends); a shard holding an address holds every primary id there.Candidates::siblings), no allocation.CONNECTION_TIMEreads the representative's own average (a latency, not a count).Cost
On every list or backend-configuration mutation (
add_backend, including the in-place upsert of weight/backup, andremove_backend),refresh_siblingsbuilds a sibling ring linking the ids of each address, a per-id precomputed shard rank (shard_representative), and the count of distinct primary addresses. Per selection:main(differential check below);ccandidates plus, per shared address ofgids, up tog²ring steps (each anO(log c)lookup);refresh_siblingsis linear plusg²per shared address; the shard takes in siblings by walking the ring. No allocation (a_selection_over_a_shared_address_allocates_nothing).Cost guard:
one_shared_pair_costs_a_selection_constant_workcounts shared-address work (SIBLING_STEPS: one per representative comparison or ring step, test-only counter) over 1000 backends + one shared pair, all six policies, sharded and not: ≤ 8 per selection. Seen RED with the per-candidate scan restored: "RoundRobin (sharded: false) spent 1002 steps on one shared pair".Micro-benchmark (
cargo test --release, release profile = LTO + codegen-units=1, RoundRobin, keyed selection, n distinct addresses + one duplicated id; same harness for all three columns; µs per selection):(main measured twice, range shown.) The remaining constant is the order-preserving copy of the candidates into the reserved scratch buffer, only when the cluster shares an address.
Differential vs main, no shared address: 40 backends (some backups), all six policies, sharded and not, 2000 keyed selections with load updates each — selection sequence hash
afccf483643b5111identical on main and on this branch.Tests
New
same_address_testsinlib/src/backends.rs(all six policies viaBackendList) plusmaglev_gives_a_shared_address_the_slots_of_one_backend:a_load_aware_policy_reads_the_load_of_the_whole_address, RED with the sibling sum zeroed);Commands
check_invariantsassertsshares_addressmatches the list (a mutation bypassingadd_backend/remove_backendfails loudly).cargo test -p sozu-lib --no-default-features --features crypto-ring,opentelemetry,splice,simd --lib→ 1636 passedRUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib→ cleanBackendList.backends(pushed/popped withoutadd_backend/remove_backend) never makes a selection panic: stale sibling state is ignored (a_list_edited_directly_still_selects_without_panicking, RED with the guards removed: index out of bounds).cargo +1.93.1 clippy --workspace --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings→ cleancargo +nightly fmt --all --check→ cleancargo test -p sozu-e2e --features opentelemetry,splice,simd -- affinity_key_tests backend_failover_tests shuffle_sharding_tests udp_tests→ 40 passedpython3 .github/scripts/check_doc_citations.py --root . --base origin/main→ OKDocs
hash_backend,CandidatesandMaglevcomments;doc/configure.md("One address is one share", shuffle-sharding section and table); CHANGELOG [Unreleased] / Changed (behaviour change for clusters with several ids at one address under any policy but HRW). No configuration or API change.