Skip to content

fix(load-balancing): give a shared address the share of one backend - #1856

Merged
FlorentinDUBOIS merged 4 commits into
mainfrom
fix/lb-same-address-identity
Oct 4, 2026
Merged

FlorentinDUBOIS merged 4 commits into
mainfrom
fix/lb-same-address-identity

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

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 through BackendList): 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

  • Collapse point: BackendList keeps 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.
  • Representative: the heaviest eligible id (clamped weight), the first in list order among equal weights (outranks_at_address in lib/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 in w for 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.
  • Collapsed weight: the representative's weight (= max eligible weight). Maglev builds its table over the full configured set, so there it is the max over all ids at the address, health aside.
  • Maglev: one table entry per distinct address (rebuild time, control plane).
  • Shuffle sharding: ranks and counts distinct primary addresses (k, shard_min_backends); a shard holding an address holds every primary id there.
  • LeastLoaded / PowerOfTwo: compare an address's load as the SUM of its ids' connection (or request) counts, eligible or not, walked through the sibling ring (Candidates::siblings), no allocation. CONNECTION_TIME reads the representative's own average (a latency, not a count).
  • Placement still keys on the address: renames without moves are stable.

Cost

On every list or backend-configuration mutation (add_backend, including the in-place upsert of weight/backup, and remove_backend), refresh_siblings builds 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:

  • no shared address: the collapse is skipped, shard ranking reads the precomputed flags — selection is byte-identical to main (differential check below);
  • shared addresses: one pass over the candidates; only ids with a sibling walk their ring and look siblings up by binary search — linear in the c candidates plus, per shared address of g ids, up to g² ring steps (each an O(log c) lookup); refresh_siblings is linear plus g² 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_work counts 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):

backends main (0f5430c) PR v1 (2c8d2c5) now (872e05d)
100 0.33–0.38 33.6 0.60
500 1.7–2.4 695 2.97
1000 3.9–4.5 2490 5.22
1000, sharded 25% 42.8–52.1 5003 48.7

(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 afccf483643b5111 identical on main and on this branch.

Tests

New same_address_tests in lib/src/backends.rs (all six policies via BackendList) plus maglev_gives_a_shared_address_the_slots_of_one_backend:

  • share ≈ 1/2 (bounds 0.47–0.53, seeded RNGs, deterministic keys) — RED on main for RoundRobin/Random/LeastLoaded/PowerOfTwo/Maglev, HRW already green;
  • first id represents at equal weights; heaviest at unequal weights (HRW green before and after);
  • a down id yields to its eligible sibling;
  • shard counts a shared address once and holds both its ids — RED on main (3 addresses for k=2);
  • LeastLoaded/PowerOfTwo read the summed address load (a_load_aware_policy_reads_the_load_of_the_whole_address, RED with the sibling sum zeroed);
  • Maglev table slot count per address (505/1009 equal, 757/1009 for 300 vs 100) — RED on main (673).

Commands

  • check_invariants asserts shares_address matches the list (a mutation bypassing add_backend/remove_backend fails loudly).
  • cargo test -p sozu-lib --no-default-features --features crypto-ring,opentelemetry,splice,simd --lib → 1636 passed
  • RUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib → clean
  • A directly edited BackendList.backends (pushed/popped without add_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 → clean
  • cargo +nightly fmt --all --check → clean
  • cargo test -p sozu-e2e --features opentelemetry,splice,simd -- affinity_key_tests backend_failover_tests shuffle_sharding_tests udp_tests → 40 passed
  • python3 .github/scripts/check_doc_citations.py --root . --base origin/main → OK

Docs

hash_backend, Candidates and Maglev comments; 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.

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
FlorentinDUBOIS merged commit 4ab6784 into main Oct 4, 2026
29 checks passed
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]>
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.

1 participant