Skip to content

[Serve] Route multiplexed requests by an ordered candidate model ID list - #67400

Open
rahulmansharamani14 wants to merge 6 commits into
ray-project:masterfrom
rahulmansharamani14:serve-candidate-multiplexed-model-ids
Open

rahulmansharamani14 wants to merge 6 commits into
ray-project:masterfrom
rahulmansharamani14:serve-candidate-multiplexed-model-ids

Conversation

@rahulmansharamani14

@rahulmansharamani14 rahulmansharamani14 commented Oct 25, 2026 •

Copy link
Copy Markdown

Description

Today a request can name only one multiplexed model. If the replica it reaches does not have that model loaded, the replica loads it, which is slow, even when the replica already has another model the caller would accept.

This PR lets the caller pass an ordered list of acceptable models instead:

handle.options(candidate_multiplexed_model_ids=["model:v3", "model:v2"]).remote()

Routing works as before. Once Ray picks a replica, the request runs on the first model in the list that the replica already has, or on the first model if it has none of them.

Related issues

Closes #65348 (design discussion is on the issue thread)

#67095 targets the same issue with a different approach from the one the thread settled on.

Additional information

Key changes

  • New candidate_multiplexed_model_ids option on DeploymentHandle.options.
  • The router picks the model from the list after it picks the replica.
  • New MultiplexMixin.rank_replicas_via_multiplex_candidates helper for custom routers.
  • Custom request router guide updated.

Design decisions

Settled

  • Routers only rank replicas. Ray picks the model after the replica is chosen.
  • A replica that already has a later model in the list serves it. Nothing forces the first model to load.

Needs maintainer agreement

  • The model is picked in router.py, so it works the same for every router. OK?
  • Setting one option clears the other. OK?
  • When a request is retried, should routing start again from the first model? This PR keeps the last one.

Out of scope: HTTP and gRPC requests, and local testing mode, which uses the first model.

Testing

python -m pytest python/ray/serve/tests/unit/test_router.py                 110 passed
python -m pytest python/ray/serve/tests/unit/test_handle_options.py          17 passed
python -m pytest python/ray/serve/tests/unit/test_default_impl.py             4 passed
python -m pytest python/ray/serve/tests/unit/test_request_metadata.py         2 passed
python -m pytest python/ray/serve/tests/unit/test_request_router.py           6 passed
python -m pytest python/ray/serve/tests/unit/test_pow_2_request_router.py   139 passed
python -m pytest python/ray/serve/tests/test_multiplex.py                    21 passed
  • Unit: picking the model for different replicas, retries, choose_replica(), broadcast(), and option validation. The new tests fail without the change.
  • Regression: existing tests pass unchanged, and requests with a single model ID behave as before.
  • End to end: a request uses a loaded later model instead of loading the first one, including when the best replica is full.

AI assistance

Written with AI assistance (Claude Code). I've reviewed every changed line.

Add `candidate_multiplexed_model_ids`, an ordered tuple of acceptable model
IDs, to the handle options and `RequestMetadata`.

`get_request_metadata` resolves the list to the caller's first choice, because
the replica-side multiplexing API takes a single model ID. Routers that don't
understand candidate lists, including the default router, then behave as if
that ID had been passed alone and need no changes.
EOFgit add python/ray/serve/_private/handle_options.py \
        python/ray/serve/_private/common.py \
        python/ray/serve/_private/default_impl.py \
        python/ray/serve/tests/unit/test_request_metadata.py \
        python/ray/serve/tests/unit/test_default_impl.py

git commit -s -F - <<'EOF'
[Serve] Carry candidate multiplexed model IDs through request metadata

Add `candidate_multiplexed_model_ids`, an ordered tuple of acceptable model
IDs, to the handle options and `RequestMetadata`.

`get_request_metadata` resolves the list to the caller's first choice, because
the replica-side multiplexing API takes a single model ID. Routers that don't
understand candidate lists, including the default router, then behave as if
that ID had been passed alone and need no changes.

Signed-off-by: rahulmansharamani14 <[email protected]>
…tions

Validate and normalize the caller's list, rejecting non-strings, empty IDs and
duplicates.

Setting either `multiplexed_model_id` or `candidate_multiplexed_model_ids`
clears the other, so a chained `options()` call can't combine two conflicting
routing instructions. Passing both in one call raises.

Signed-off-by: rahulmansharamani14 <[email protected]>
Rank replicas by an ordered candidate list so custom routers don't reimplement
it against private mixin state. A router using it must write the matched
candidate back to `RequestMetadata.multiplexed_model_id`.

Signed-off-by: rahulmansharamani14 <[email protected]>
Add an end-to-end test with a custom router that serves a warm later candidate
instead of cold-loading the caller's first choice, and document the helper in
the custom request router guide.

Signed-off-by: rahulmansharamani14 <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for candidate_multiplexed_model_ids in Ray Serve, allowing requests to specify an ordered list of acceptable multiplexed model IDs. This enables custom request routers to select a warm model replica from the candidates and avoid cold-start latencies. The changes include updates to request metadata, handle options, routing logic, documentation, and comprehensive unit and end-to-end tests. The review feedback focuses on improving type annotations to support both lists and tuples, updating error messages to reflect this flexibility, and fixing a logical bug where explicitly clearing multiplexed_model_id to an empty string fails to clear the candidate model IDs.

Comment thread python/ray/serve/handle.py Resolved
Comment thread python/ray/serve/handle.py Resolved
Comment thread python/ray/serve/handle.py Resolved
Comment thread python/ray/serve/handle.py Resolved
Comment thread python/ray/serve/handle.py Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue docs An issue or change related to documentation community-contribution Contributed by the community labels Nov 8, 2026
@johntaylor-cell

Copy link
Copy Markdown
Contributor
  1. Write-back in choose_replicas can't pick the right candidate (your open question 3). The rank a request is served from is decided later, when _select_from_candidate_replicas skips full replicas:

    • Returning all ranks (the guide's ThroughputAwareRequestRouter): A has v7 and is full, B has v6. The request goes to B with v7, a cold load.
    • Returning only the best rank (CandidateAwareRouter): A has v6 and is full, B is idle. The request waits on A.

    Suggest resolving in _choose_replica_for_request once a replica is assigned: the first candidate it has loaded, else candidates[0]. Routers then only rank, and the FIFOMixin index never goes stale. The default router would also serve a later candidate when it lands on a replica holding one.

  2. rank_replicas_via_multiplex(replicas, m) is rank_replicas_via_multiplex_candidates(replicas, [m]); make it delegate.

  3. +1 gemini: .options(multiplexed_model_id="") leaves the candidates set, so the request still sends candidates[0]. Drop and multiplexed_model_id from the elif, and add a chaining case for {"multiplexed_model_id": ""}.

  4. +1 gemini on the hints: options() accepts tuples, but mypy and pyrefly reject options(candidate_multiplexed_model_ids=("a", "b")) in caller code. Use Union[List[str], Tuple[str, ...], DEFAULT], and widen _normalize_candidate_multiplexed_model_ids to match.

  5. explicit_id_wins_over_candidates in test_default_impl.py tests a state options() can't produce; drop it.

@harshit-anyscale harshit-anyscale added the go add ONLY when ready to merge, run all tests label Nov 10, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Routers only rank replicas. The router picks the served candidate once the replica is known, on every send path. Also accept tuples in the type hints, clear the candidates when multiplexed_model_id is set to an empty string, make rank_replicas_via_multiplex delegate to the candidates helper, and skip the custom-router end-to-end tests under HAProxy.

Signed-off-by: rahulmansharamani14 <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 105f61a. Configure here.

replica = await self._active_request_router._choose_replica_for_request(
pr, is_retry=is_retry
)
self._resolve_multiplexed_model_id(pr.metadata, replica)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retry desyncs FIFO multiplex model index

Medium Severity

_resolve_multiplexed_model_id mutates RequestMetadata.multiplexed_model_id on the reused PendingRequest after a replica is chosen. On rejection, reset_future plus lazy model-index cleanup leave the request under both the original and resolved keys, so FIFO matching can hand it a replica chosen for another model. apply_multiplex_routing on retry also searches for the previously resolved ID instead of the first candidate, including on the default router.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 105f61a. Configure here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. Every key the request sits under is one of its own candidates, so it still runs a model it accepts. I proposed resetting the model ID to the first candidate before a retry, and will add it if the reviewer agrees.

@rahulmansharamani14

Copy link
Copy Markdown
Author

@johntaylor-cell I addressed all five points in 105f61a and merged master.

Point 1 was the real gap. A router cannot know which replica a request ends up on, because Ray skips full replicas after ranking. So I moved the model pick to right after the replica is chosen.

  1. I put the pick in router.py rather than _choose_replica_for_request, because CapacityQueueRouter overrides that method and would skip it. It runs for remote(), choose_replica(), and broadcast(). I also turned your first example into an end-to-end test.
  2. I made rank_replicas_via_multiplex delegate to the new helper. Its existing tests pass unchanged.
  3. Fixed, and I added a chaining test for multiplexed_model_id="".
  4. Both options() signatures accept tuples.
  5. Removed.

The earlier premerge failure came from my end-to-end test, because HAProxy rejects a custom router on the ingress deployment. I skipped both tests under HAProxy, the same way test_round_robin_router.py does.

The two remaining open points, where the pick runs and mutual exclusion, are under "Needs maintainer agreement" in the description.

@rahulmansharamani14

Copy link
Copy Markdown
Author

One open design question on retries. Say a request has [v7, v6], replica A holds v6, and replica B holds v7. B is full, so the request lands on A and gets v6, and then A rejects it. The retry still routes by v6, so it tries A again even if B has freed up, and the FIFO index now holds the request under both v7 and v6.

I would reset the model ID to the first candidate before a retry, so it routes by v7 like a retried pinned request. OK to add that here?

@johntaylor-cell

Copy link
Copy Markdown
Contributor

Thanks @rahulmansharamani14. Here are my comments and answer:

  1. Retry: yes, reset to candidates[0] before re-routing. choose_replica() already retries by candidates[0], but remote() retries by the candidate picked for the replica that rejected it. The reset makes them match and keeps the request under one FIFO key. Please also assert in test_multiplexed_model_id_resolved_again_on_retry that the retry routes by v7; its current asserts pass either way.
  2. Drop the resolve call in the _reserve=False branch and test_choose_replica_no_reserve_resolves_multiplexed_model_id. No pick-only caller sets candidates.
  3. Add handle.options(candidate_multiplexed_model_ids=("v7", "v6")) to check_handle_typing.py to lock in the tuple hints.
  4. Picking the model in router.py and having each option clear the other both look right.

This branch has not been deployed

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

Labels

community-contribution Contributed by the community docs An issue or change related to documentation go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Serve] Support passing an ordered list of model_ids to controllers

3 participants