Skip to content

Every backend selection allocates a Vec of candidates, and the load-balancing trait requires it #1549

Closed

Description

@FlorentinDUBOIS

Measured against origin/main at f7491559dc0f749ee767e7a824124adf03358f8c.

Every backend selection allocates a fresh Vec, on every request, before any load-balancing policy
runs:

pub fn available_backends(&mut self, backup: bool) -> Vec<Rc<RefCell<Backend>>> {
    self.backends
        .iter()
        .filter(|backend| {
            let owned = backend.borrow();
            owned.backup == backup && owned.can_open()
        })
        .map(Clone::clone)
        .collect()
}

That is one Vec allocation plus M Rc refcount increments per selection, where M is the
number of healthy candidates. On the degraded paths it is worse: with no healthy primary the
selection retries with backup = true — a second Vec — and with none of those either it falls
through to a fail-open pass, a third.

sōzu's standing constraint is to stay maximally zero-copy on the hot and data path. Backend
selection runs once per proxied request, so this is squarely on the hot path, and it allocates on
every request regardless of policy.

It is structural, not incidental

The allocation is not a local choice inside available_backends that a refactor could quietly
drop. The trait every policy implements requires the owned Vec:

fn next_available_backend(
    &mut self,
    key: Option<u64>,
    backends: &mut Vec<Rc<RefCell<Backend>>>,
) -> Option<Rc<RefCell<Backend>>>;

lib/src/load_balancing.rs carries the trait declaration and six implementations of it. Removing
the allocation means changing that signature for all six, which is why it is filed here rather than
folded into the change that found it.

The cost is already documented in the tree, which is worth noting because it means it was seen and
accepted rather than missed: the PowerOfTwo doc's "Cost" section says that every selection, under
every policy, first builds the candidate set in BackendList::available_backends, cloning each
healthy backend into a fresh Vec, and that no policy here is sub-linear per request.

What a fix has to preserve

  • The candidate filter. backup == requested && can_open() is the selection's correctness, not
    its cost. Any replacement must apply it identically, including the backup and fail-open
    fallbacks.
  • Mutable policy state. Selection is &mut for real reasons — a round-robin cursor, the
    fail-open latch, PeakEWMA::observe, the Maglev table. A borrowed read-only view cannot select.
  • No allocation moved rather than removed. A fix that trades the per-selection Vec for a
    per-selection Rc::from or a cached Vec invalidated on every health change has not reduced
    anything on the steady state and may have made the control plane pay instead.

A shape worth measuring first

The policies need to iterate the candidates, not own them. An iterator over the filtered backends
— or a reusable scratch buffer owned by BackendList and cleared per selection, the pattern
HpackState already uses for its scratch buffers — would keep the filter and remove the
per-request allocation. The iterator form is the cleaner one but interacts with policies that index
into the candidates (RoundRobin's cursor, PowerOfTwo's two random picks); the scratch-buffer
form keeps the &mut Vec signature and only changes who owns it.

Measure before choosing: which policies index rather than iterate, how large M is on a realistic
cluster, and whether an existing Criterion bench in lib/benches/ captures this path.

Adjacent, smaller

BackendRegistry::id_for performs an Rc::from(str) per dial, inherited from #1517.

Found while measuring the second part of Q12 (#1340), whose proposed shape is allocation-neutral on
this path — it neither adds nor removes these allocations, and should not be asked to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions