Skip to content

feat: allow per-request spread via SpreadCtxKey context override - #7

Closed
ecordell wants to merge 1 commit into
mainfrom
spread-override
Closed

ecordell wants to merge 1 commit into
mainfrom
spread-override

Conversation

@ecordell

Copy link
Copy Markdown
Contributor

Problem

Spread is a single value in the balancer config, so every key gets the same number of candidate backends. A caller that detects a hot key cannot spread only that key across more backends. Raising the global spread dilutes cache locality for every key to protect a few.

Change

A request can carry a uint8 value at SpreadCtxKey in its context. The picker then uses that spread instead of the spread from the balancer config. The picker ignores a value of zero or of any other type.

Also, this aligns the not-enough-members fallback with its comment:

  • A spread that exceeds the count of ready members now uses all ready members. Before, the pick collapsed to a single member.
  • A pick against an empty ring queues the RPC. The clamp needs an explicit guard for this case, because FindN(key, 0) returns an empty result and no error.

Tests

Four new tests: the override picks as the equivalent config spread would, a zero override uses the config spread, a spread above the member count uses all members, and a pick against an empty ring queues instead of panicking. The empty-ring test failed with a panic before the guard. The suite passes with -race.

A request can carry a uint8 value at SpreadCtxKey in its context. The
picker then uses that spread instead of the spread from the balancer
config. This lets a caller widen the candidate set for one key, for
example to spread a measured-hot key across more backends, without a
change to the global spread.

Also align the not-enough-members fallback with its comment:

- A spread that exceeds the count of ready members now uses all ready
  members. Before, the pick collapsed to a single member.
- A pick against an empty ring queues the RPC. The clamp needs an
  explicit guard for this case, because FindN(key, 0) returns an empty
  result and no error.
@ecordell

Copy link
Copy Markdown
Contributor Author

I realized this is not that useful - if you want to dynamically increase spread, it's easier to multiply at the shard key level (i.e. instead of foo pick randomly between foo|1 and foo|2)

@ecordell ecordell closed this Sep 30, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant