Skip to content

[Serve][5/5] Enable direct streaming for Ray Serve LLM apps - #62670

Closed
eicherseiji wants to merge 115 commits into
ray-project:masterfrom
eicherseiji:ingress-bypass/4-llm-route
Closed

eicherseiji wants to merge 115 commits into
ray-project:masterfrom
eicherseiji:ingress-bypass/4-llm-route

Conversation

@eicherseiji

@eicherseiji eicherseiji commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Why this is needed

This PR introduces the Ray Serve LLM changes: an ingress request router that can pick a model replica, and a backend ASGI app that the chosen replica can serve directly. This PR is where the OpenAI/vLLM serving path plugs into those new Serve interfaces.

Summary

  • add LLMRouter as the LLM-side ingress request router that serves /internal/route and returns {"replica_id": ...} for a chosen backend replica
  • use the temporary DeploymentHandle._get_request_router() escape hatch so LLMRouter can reuse Serve's existing in-process replica picker instead of re-implementing replica selection logic
  • add a late-bound ASGI hook, __serve_build_asgi_app__, so LLMServer can build the vendored vLLM FastAPI app after async engine startup; Serve resolves it during replica ASGI initialization before lifespan startup
  • compose the direct-streaming app inside build_openai_app(...); this prototype validates direct streaming as single-model-only for now
# The direct-streaming app shape is selected internally when
# RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1.
app = build_openai_app({"llm_configs": [llm_config]})

Stack: #62667 -> #62680 -> #62668 -> #62669 -> this

Test plan

  • Added / updated LLM builder unit coverage for the composed direct-streaming app shape
  • python -m py_compile on the edited files
  • git diff --check
  • Repo commit hooks / linters on commit

Benchmark Results

Direct streaming, benchmarked at 17a635aa4f887a8afdf931e65a31a74bf507d400; current PR cleanup tip is 256599ac14ae7322b2222202e3c8d9a25923ae62. vLLM 0.19.0.

Workload: Qwen/Qwen3-0.6B-FP8, 8 H100 replicas, ISL=8000, OSL=50, 4096 requests, request-rate=inf.

Benchmark invocation:

cd /home/ray/default/llm-inference-stack/replica-scaling
python sweep_concurrency.py \
  --backend ray-serve-llm-direct-streaming \
  --label qwen-0.6b-fp8-h100-ray-direct-streaming-seiji-prhead-17a635-rr-8k50-c64-128-256 \
  --concurrency 64 128 256 \
  --isl-osl-pairs 8000:50

Equivalent vllm bench serve workload shape:

vllm bench serve \
  --backend openai \
  --base-url http://127.0.0.1:8000 \
  --model Qwen/Qwen3-0.6B-FP8 \
  --dataset-name random \
  --random-input-len 8000 \
  --random-output-len 50 \
  --num-prompts 4096 \
  --max-concurrency <64|128|256> \
  --request-rate inf \
  --percentile-metrics ttft,tpot,itl,e2el \
  --save-result

App deployment code:

import os

# For cluster runs, set this in the cluster/runtime env before workers start.
# In a local script, set it before importing ray.serve.llm.
os.environ["RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING"] = "1"

from ray import serve
from ray.serve.llm import LLMConfig, LLMServingArgs, ModelLoadingConfig, build_openai_app

llm_config = LLMConfig(
    model_loading_config=ModelLoadingConfig(
        model_id="Qwen/Qwen3-0.6B-FP8",
        model_source="Qwen/Qwen3-0.6B-FP8",
    ),
    engine_kwargs=dict(
        tensor_parallel_size=1,
        max_model_len=10000,
        gpu_memory_utilization=0.95,
        max_num_seqs=128,
        max_num_batched_tokens=16384,
        enable_prefix_caching=False,
        load_format="dummy",
        kv_cache_dtype="fp8",
    ),
    deployment_config=dict(
        autoscaling_config=dict(min_replicas=8, max_replicas=8),
        max_ongoing_requests=8192,
    )
)

app = build_openai_app(LLMServingArgs(llm_configs=[llm_config]))
serve.run(app)

Results:

Max concurrency Successful Failed Req/s Output tok/s Mean TTFT (ms) P99 TTFT (ms) Mean TPOT (ms) P99 TPOT (ms) Mean E2E (ms) P99 E2E (ms)
64 4096 0 122.20 6110.07 118.66 489.24 8.19 10.09 519.95 841.03
128 4096 0 149.83 7491.40 148.71 746.33 14.28 18.41 848.37 1389.91
256 4096 0 164.24 8211.87 231.20 1395.37 26.81 35.11 1544.79 2400.58

@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch from 0ab2c0c to 53b613f Compare April 16, 2026 17:25
@eicherseiji eicherseiji changed the title [LLM] Add /internal/route endpoint and load-aware routing for ingress bypass [LLM][4/4] Add /internal/route endpoint and load-aware routing for ingress bypass Apr 16, 2026

@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 implements an "ingress bypass" feature for Ray Serve LLM deployments, enabling HAProxy to route traffic directly to LLMServer replicas using Lua-based custom routing. This involves adding an ingress_bypass configuration, implementing a background load poller for least-connections routing, and updating HAProxy templates to support per-request routing decisions via an internal endpoint. Review feedback highlights several critical improvements: the load poller needs to dynamically handle scaling events, blocking ray.get calls in asynchronous contexts should be replaced with asyncio.gather to prevent event loop stalls, and JSON construction in both Python and Lua should be made more robust to prevent malformed responses or fragile parsing.

Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/haproxy.py Outdated
Comment on lines +880 to +881
local host = response:match('"host"%s*:%s*"([^"]+)"')
local port = response:match('"port"%s*:%s*(%d+)')

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.

medium

Parsing JSON using regular expressions in Lua is fragile and error-prone. If the JSON structure changes slightly (e.g., whitespace, field order), the routing will fail. While Lua doesn't have a built-in JSON parser, it's better to use a more robust parsing method if available in the HAProxy environment or ensure the response format is strictly controlled and documented.

@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch from 3491681 to 1e1cfdd Compare April 16, 2026 20:18
@eicherseiji eicherseiji changed the title [LLM][4/4] Add /internal/route endpoint and load-aware routing for ingress bypass [Serve][5/5] Add /internal/route endpoint and load-aware routing for ingress bypass Apr 16, 2026
@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch from 1e1cfdd to b85a2b9 Compare April 16, 2026 20:21
@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch 8 times, most recently from 269ac19 to 384d4a1 Compare April 18, 2026 02:02
@eicherseiji eicherseiji changed the title [Serve][5/5] Add /internal/route endpoint and load-aware routing for ingress bypass [Serve][5/5] Add LLM /internal/route and backend ASGI handoff Apr 20, 2026
@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch 6 times, most recently from 2466f8b to 2b140da Compare April 21, 2026 06:24
@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch 5 times, most recently from ed80ad7 to b64e550 Compare April 30, 2026 21:31
@eicherseiji
eicherseiji marked this pull request as draft May 6, 2026 19:37
@eicherseiji

Copy link
Copy Markdown
Contributor Author

🙃 tried to fix DCO

@eicherseiji
eicherseiji force-pushed the ingress-bypass/4-llm-route branch from 0efa14b to ff33f5b Compare May 6, 2026 19:40
@eicherseiji
eicherseiji marked this pull request as ready for review May 6, 2026 19:40
@eicherseiji

eicherseiji commented May 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Superseded by #63167.

@eicherseiji eicherseiji closed this May 6, 2026
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 serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants