Skip to content

investigate shuffle sharding #524

Closed
Closed

Description

@Geal

https://twitter.com/colmmacc/status/1034492056968736768?s=19
this is a kind of load balancing where client requests can be sent to various backend servers, but each client request can have a different set of backend servers. The idea is to avoid complete overlap: in the case of a client request that brings down the server, the client could retry the request and bring down another server, until it has destroyed its entire set, other client requests would still be sent to live servers because their sets do not overlap.

Activity

  1. added this to the milestone on Jul 29, 2021
  2. removed this from the milestone on Feb 18, 2022
  3. added this to the v0.17.0 milestone on Feb 18, 2022
  4. FlorentinDUBOIS commented on Sep 29, 2026

    @FlorentinDUBOIS
    Collaborator

    Maintainer decisions (2026-09-30)

    Grounded in Workload Isolation Using Shuffle Sharding (Colm MacCárthaigh, Amazon Builders' Library, 2019): the isolation gain comes from C(N,k) virtual shards versus N/k plain partitions, so it is negligible on small pools (N=4, k=2 gives 6 combinations, nearly every client overlaps) and grows fast from N≈8 (C(8,2)=28, 7× better than 4 partitions). Shards must be stable for a client; HRW top-k gives that, since a change of N only moves backends at the tail of a client's ranking.

    Current state on main: only the UDP datapath passes an affinity key to load balancing (next_available_backend_with_key(Some(flow_hash))); HTTP and TCP call it with None, so a cluster configured with MAGLEV or RENDEZVOUS silently degrades to round-robin.

    1. Scope, two PRs in sequence.
      • PR 1 — affinity key for HTTP/TCP: pass a client key to the consistent-hashing policies on the HTTP, HTTPS and TCP paths, so Maglev/HRW pin a client as documented.
      • PR 2 — shuffle sharding: restrict selection to the client's top-k backends of the HRW ranking.
    2. Client key. Default: the real client source IP (after proxy protocol). Per-cluster option to derive it from a named request header or cookie (HTTP/HTTPS only; TCP always uses the source IP). If the configured header/cookie is absent, fall back to the source IP.
    3. Shard size. Per-cluster percentage of the cluster's backends: k = max(2, ceil(pct × N)), capped at N (minimum 2 keeps a retry possible inside the shard). Hot-updatable.
    4. Activation threshold. Per-cluster min_backends, default 8 (the reference example of the paper). Below it the cluster behaves exactly as today.
    5. Compatibility by default. Shuffle sharding is off until the shard percentage is configured. When enabled, the default mode is fallback: if every backend of a client's shard is unavailable, selection spills over to the rest of the cluster. Strict isolation (no spill-over, 503 with a dedicated metric) is opt-in per cluster.
    6. Sticky sessions win. A sticky cookie naming a live backend is honoured even outside the client's shard; otherwise selection happens inside the shard.

    /goal prompt

    /goal Implement sozu-proxy/sozu#524 as decided in the maintainer comment of 2026-09-30: an affinity key for Maglev/HRW on HTTP, HTTPS and TCP (PR 1), then shuffle sharding over HRW top-k (PR 2).
    
    BASELINE (re-measure on current origin/main first; stop and report if a fact no longer holds):
    - Only UDP passes a key: `BackendMap::backend_from_cluster_id_with_key` / `BackendList::next_available_backend_with_key` (lib/src/backends.rs); HTTP/HTTPS/TCP pass `None`, and `Maglev`/`Rendezvous` (lib/src/load_balancing.rs) fall back to round-robin without a key.
    - Backend selection for HTTP/HTTPS goes through `BackendDialer::select_and_dial` (lib/src/protocol/mux/router.rs, implemented in lib/src/protocol/mux/mod.rs); TCP selects in lib/src/tcp.rs.
    - If the #1684 sequence (clock/RNG injection, G2 reservation at selection) has changed these signatures, follow the new shapes; do not reintroduce wall-clock or OS-RNG reads into selection.
    
    PR 1 — affinity key for HTTP/HTTPS/TCP:
    - Key = hash of the real client source IP (post proxy-protocol). Per-cluster option: derive it from a named request header or cookie (HTTP/HTTPS only), falling back to the source IP when absent. Hash without allocation on the request path.
    - Plumb the option through command.proto (Cluster), command/src/config.rs, state, CLI and display, hot-updatable; keep every existing cluster's behaviour byte-identical unless it uses MAGLEV/RENDEZVOUS (whose behaviour now matches the documentation — state this in the CHANGELOG as a fix).
    - Success: an e2e test per protocol showing that, with RENDEZVOUS, N requests from one client key hit one backend and different keys spread across backends; with the header option, two clients behind one source IP split by header. Per-request allocation count unchanged (test_allocations).
    
    GATE: stop after PR 1 is open and report; wait for the maintainer before PR 2.
    
    PR 2 — shuffle sharding:
    - Per-cluster `shard_percent` (off when unset), `shard_min_backends` (default 8), `shard_mode` = `fallback` (default) | `strict`.
    - Active only when the cluster has at least `shard_min_backends` backends; then k = max(2, ceil(shard_percent × N)), capped at N; the shard is the client key's top-k of the HRW ranking (stable under N changes).
    - Selection and retries stay inside the shard; a live sticky-session backend wins even outside it. When the whole shard is unavailable: `fallback` spills to the rest of the cluster, `strict` answers 503 and increments a dedicated counter (plus a counter for spill-overs in fallback mode).
    - Success: unit tests that shard membership is deterministic and stable when a backend is added/removed (only tail changes), that k and the threshold follow the formulas, that strict never selects outside the shard; e2e tests for fallback vs strict with the whole shard down; zero allocation added on the selection path.
    
    EVERY PR: one concern per PR from a worktree off origin/main; docs in the same change (doc/configure.md, load-balancing docs, LIFECYCLE.md where relevant) and CHANGELOG; new tests seen red before green; never weaken a check; performance first (no allocation or syscall added on the hot path, measured); `cargo +nightly fmt --all -- --check`, stable and MSRV clippy with -D warnings, `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked`, workspace tests, e2e suite, `cargo +nightly fuzz build` in `fuzz/`, and the four `check_doc_citations.py` modes all exit 0; signed-off and signed commits without generated-by trailers.
    
    STOP CONDITIONS: stop at the gate after PR 1; at most five attempts per failing check, then post the attempt ledger and stop; stop and report if a baseline fact no longer holds. End each run with `done=<bool> state=<step> prs=<n> blockedReason=<reason|none>`.
    
  5. FlorentinDUBOIS commented on Sep 30, 2026

    @FlorentinDUBOIS
    Collaborator

    Implemented in two parts:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions