Skip to content

[serve][LLM] Add pick-only fast path to AsyncioRouter for LLM ingress - #63517

Merged
kouroshHakha merged 6 commits into
ray-project:masterfrom
kouroshHakha:kh/upstream-decouple-primitives
May 20, 2026
Merged

kouroshHakha merged 6 commits into
ray-project:masterfrom
kouroshHakha:kh/upstream-decouple-primitives

Conversation

@kouroshHakha

@kouroshHakha kouroshHakha commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

In LLMs direct streaming, we can take a shortcut and make a conscious trade off for using the latest known state of available replicas to do the routing and do not do the least loaded check via rpc calls and slot reservation in the application code. This will be slow. instead we can assume the replica has capacity and have a graceful fallback to least loaded at the HAProxy layer. In other words, with this pr, we only make the best effort to pick the optimal replica according to the router, not a guaranteed one.

@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 the choose_replica and dispatch APIs to DeploymentHandle, allowing for manual replica selection and slot reservation. The LLM ingress router has been refactored to use these APIs, delegating routing decisions to the deployment's request router. Feedback indicates that experimental debugging code in replica_wrapper.py which bypasses slot reservation and release logic needs to be reverted. Furthermore, a bug was identified in router.py where a ReplicaWrapper object is incorrectly indexed, potentially causing a TypeError.

Comment thread python/ray/serve/_private/router.py Resolved
Introduce an internal ``_reserve=False`` opt-out on
``AsyncioRouter.choose_replica`` that short-circuits the replica-side
``reserve_slot`` actor RPC and the rejection-retry loop. The configured
``RequestRouter`` still drives replica ordering; only the per-pick
capacity reservation is skipped.

Use the new fast path from ``LLMRouter._pick_replica`` so the LLM
ingress router (which delegates the actual request to HAProxy and never
dispatches via the Serve handle) avoids the extra RPC + retry that was
introducing burstiness compared to the prior local round-robin
implementation.

``ReplicaSelection._slot_token`` is now ``Optional[str]``; ``dispatch``
explicitly rejects a selection that was created without a reservation,
and ``release_slot`` is a no-op in that case.

Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
@kouroshHakha
kouroshHakha force-pushed the kh/upstream-decouple-primitives branch from c4364bf to 2560c17 Compare May 19, 2026 18:41
@kouroshHakha kouroshHakha changed the title [Serve][LLM] Decouple routing primitives — upstream from llm-routing-apply-decouple-primitives-deep-dive [serve][LLM] Add pick-only fast path to AsyncioRouter for LLM ingress May 19, 2026
Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
…pick

Avoids positional indexing on the inner rank sequence; short-circuits at
the first replica found across all ranks.

Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
@kouroshHakha kouroshHakha added the go add ONLY when ready to merge, run all tests label May 19, 2026
@kouroshHakha
kouroshHakha marked this pull request as ready for review May 19, 2026 19:04
@kouroshHakha
kouroshHakha requested review from a team as code owners May 19, 2026 19:04
Covers: selection has no slot token, reserve_slot is never called on the
replica, and dispatch on a pick-only selection raises.

Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue llm labels May 19, 2026
…plica

_pick_replica now passes _reserve=False to handle.choose_replica; the
test's captured-kwargs assertion needs to match.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
@jeffreywang88

Copy link
Copy Markdown
Contributor

Heads up that llm-gpu CI test is failing on master. Here's the fix: #63526.

@kouroshHakha
kouroshHakha merged commit d89c11d into ray-project:master May 20, 2026
3 checks passed
alexandrplashchinsky pushed a commit to alexandrplashchinsky/ray-alex that referenced this pull request May 29, 2026
…ray-project#63517)

Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Co-authored-by: Jeffrey Wang <[email protected]>
Signed-off-by: Alexandr Plashchinsky <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ray-project#63517)

Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Co-authored-by: Jeffrey Wang <[email protected]>
Signed-off-by: 400Ping <[email protected]>
jeffreywang88 added a commit that referenced this pull request Oct 3, 2026
…66489)

## Problem
`choose_replica(_reserve=False)` picks a replica and returns without
calling the request router's `on_request_routed` hook. Pick-only callers
send the request themselves. For example, the Serve LLM ingress router
[returns the picked
replica](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/core/ingress/router.py#L314-L319)
to HAProxy, and HAProxy forwards the request. For these callers the pick
is the routing decision, but stateful routing policies never see it.

## Solution
Call `on_request_routed(pending_request, replica_id, None)` on the
pick-only path. There is no `ReplicaResult` on this path, so the base
class now types `result` as `Optional[ReplicaResult]` and documents when
it is `None`. Update the `CapacityQueueRouter` docstring, which said
that the hook runs only for the rejection protocol. The reserved path
does not change.

Add a unit test: a `_reserve=False` selection calls `on_request_routed`
once, with the request args and `result=None`, and exiting the context
does not call it again.

Add a Serve-only HAProxy test in the same shape as
`test_multiplexed_routing_retry` (#66170): an ingress router picks a
backend replica with `_reserve=False`, and HAProxy forwards the request
to it. The backend policy keeps its state only in `on_request_routed`
(it ranks the last routed replica last). Four requests must reach both
replicas. Without this change, all four go to the same replica.

Add a manual GPU release test (`release/llm_tests/prefix_router_test`,
mirrors `llm_tests/kv_router_test`) that reproduces this bug on a real
cluster: 4 direct-streaming `LLMServer` replicas routed by
`PrefixCacheAffinityRouter`, asserting that unrelated prompts spread
across every replica and repeated prompts pin to one. Both checks call
`choose_replica(_reserve=False)` directly, so they exercise the real
routing decision without spending GPU time on inference.

## Implication for LLM applications
With direct streaming (`RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1`), the
LLM ingress router picks replicas with `_reserve=False`.
`PrefixCacheAffinityRouter` inserts prompts into its prefix tree only in
[`on_request_routed`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_aware_router.py#L414-L418),
so on this path the tree stays empty. No prompt matches, so every
request falls back to
[`get_smallest_tenants()`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_tree.py#L546-L562).
That call returns all replicas tied at 0 characters, in the same order
each time, and the pick-only path takes the first one. All traffic goes
to one replica, and there is no prefix-cache affinity.

We saw this on a 5-replica deployment with the same image and the same
prompts:

| Ingress | Load | Requests per replica | Prefix tree |
|---|---|---|---|
| Default | 100 unique prompts + 1 smoke test, concurrency 1 | 20 / 20 /
20 / 20 / 21 | Filled |
| Direct streaming | 100 unique prompts + 1 smoke test, concurrency 1 |
101 / 0 / 0 / 0 / 0 | Empty |
| Direct streaming | 20 shared prefixes, concurrency 16 | 1,000 / 0 / 0
/ 0 / 0 | Empty |

## Additional information
- `PrefixCacheAffinityRouter.on_request_routed` waits for the tree actor
with `ray.get`. The reserved path already calls it. The pick-only path
now calls it once per selection.
- Related: #63517 added the pick-only path, and #66170 added its
selection retry loop.
- The GPU release test is `frequency: manual`, matching
`llm_serve_kv_router`; it has not been run on a real cluster from this
environment.

---------

Signed-off-by: Dhakshin Suriakannu <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Co-authored-by: Jeffrey Wang <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…ay-project#66489)

## Problem
`choose_replica(_reserve=False)` picks a replica and returns without
calling the request router's `on_request_routed` hook. Pick-only callers
send the request themselves. For example, the Serve LLM ingress router
[returns the picked
replica](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/core/ingress/router.py#L314-L319)
to HAProxy, and HAProxy forwards the request. For these callers the pick
is the routing decision, but stateful routing policies never see it.

## Solution
Call `on_request_routed(pending_request, replica_id, None)` on the
pick-only path. There is no `ReplicaResult` on this path, so the base
class now types `result` as `Optional[ReplicaResult]` and documents when
it is `None`. Update the `CapacityQueueRouter` docstring, which said
that the hook runs only for the rejection protocol. The reserved path
does not change.

Add a unit test: a `_reserve=False` selection calls `on_request_routed`
once, with the request args and `result=None`, and exiting the context
does not call it again.

Add a Serve-only HAProxy test in the same shape as
`test_multiplexed_routing_retry` (ray-project#66170): an ingress router picks a
backend replica with `_reserve=False`, and HAProxy forwards the request
to it. The backend policy keeps its state only in `on_request_routed`
(it ranks the last routed replica last). Four requests must reach both
replicas. Without this change, all four go to the same replica.

Add a manual GPU release test (`release/llm_tests/prefix_router_test`,
mirrors `llm_tests/kv_router_test`) that reproduces this bug on a real
cluster: 4 direct-streaming `LLMServer` replicas routed by
`PrefixCacheAffinityRouter`, asserting that unrelated prompts spread
across every replica and repeated prompts pin to one. Both checks call
`choose_replica(_reserve=False)` directly, so they exercise the real
routing decision without spending GPU time on inference.

## Implication for LLM applications
With direct streaming (`RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1`), the
LLM ingress router picks replicas with `_reserve=False`.
`PrefixCacheAffinityRouter` inserts prompts into its prefix tree only in
[`on_request_routed`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_aware_router.py#L414-L418),
so on this path the tree stays empty. No prompt matches, so every
request falls back to
[`get_smallest_tenants()`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_tree.py#L546-L562).
That call returns all replicas tied at 0 characters, in the same order
each time, and the pick-only path takes the first one. All traffic goes
to one replica, and there is no prefix-cache affinity.

We saw this on a 5-replica deployment with the same image and the same
prompts:

| Ingress | Load | Requests per replica | Prefix tree |
|---|---|---|---|
| Default | 100 unique prompts + 1 smoke test, concurrency 1 | 20 / 20 /
20 / 20 / 21 | Filled |
| Direct streaming | 100 unique prompts + 1 smoke test, concurrency 1 |
101 / 0 / 0 / 0 / 0 | Empty |
| Direct streaming | 20 shared prefixes, concurrency 16 | 1,000 / 0 / 0
/ 0 / 0 | Empty |

## Additional information
- `PrefixCacheAffinityRouter.on_request_routed` waits for the tree actor
with `ray.get`. The reserved path already calls it. The pick-only path
now calls it once per selection.
- Related: ray-project#63517 added the pick-only path, and ray-project#66170 added its
selection retry loop.
- The GPU release test is `frequency: manual`, matching
`llm_serve_kv_router`; it has not been run on a real cluster from this
environment.

---------

Signed-off-by: Dhakshin Suriakannu <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Co-authored-by: Jeffrey Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests llm serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants