[Serve][RFC] Optimize deployment responses by stitching connections in a separate thread
Description
Description
An ingress or router that returns a child deployment's output passes every chunk through its own Python. On a synthetic LLM stream (one m5.8xlarge, mock engine at 2.3 ms per token), today's OpenAI ingress reaches 12% of direct streaming's req/s at concurrency 256 and 4.5% at 640 streams.
Proposal: a handler that returns a child's response unawaited forwards it. Serve sends the child's call over HTTP and copies the child's response bytes to the parent's caller, so the parent's code, middleware and per-chunk Python objects stay off the response path. Awaiting the response still materializes it, as today; returning it unawaited fails today.
@serve.deployment
class Gateway:
def __init__(self, model: DeploymentHandle):
self.model = model
async def __call__(self, request: Request):
# Returned unawaited, so Serve forwards the model's stream.
return self.model.options(stream=True).generate.remote(await request.json())
With a Python copy, forwarding reaches 97% of direct streaming's req/s at concurrency 256 and 94% at 640 streams. TTFT is 48 ms against 31 ms, and 185 ms against 85 ms.
Plan
- [ ] HTTP callers (HAProxy and the Serve HTTP proxy), plain handlers:
- [ ] Routing groundwork: https://github.com/ray-project/ray/pull/66643
- [ ] Forwarding transport
- [ ] End-to-end tests
- [ ] Least-held replica picks
- [ ] Serve HTTP proxy calls the parent over HTTP instead of through a handle
- [ ]
DeploymentResponseandObjectRefarguments travel by reference - [ ] Native copy thread
- [ ] Deployment handle callers
- [ ] FastAPI and other ASGI ingress, including
OpenAiIngress - [ ] gRPC callers
Out of scope
- Parents that change, add to or combine the child's output, or need their response middleware to run.
- WebSockets: the handler sends on the socket and returns nothing.
- gRPC client-streaming and bidirectional RPCs.
- A Ray
ObjectRefreturned by the handler.
Use case
No response
@johntaylor-cell Are any of these open for contribution?
@johntaylor-cell Are any of these open for contribution?
Unfortunately not yet, still reviewing design