Skip to content

[Serve] Preserve model affinity for multiplexed HAProxy ingress - #66585

Closed
eicherseiji wants to merge 2 commits into
ray-project:masterfrom
eicherseiji:seiji/haproxy-model-mux-router
Closed

eicherseiji wants to merge 2 commits into
ray-project:masterfrom
eicherseiji:seiji/haproxy-model-mux-router

Conversation

@eicherseiji

@eicherseiji eicherseiji commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

@serve.multiplexed works through direct HTTP and gRPC ingress, but generic HTTP traffic through HAProxy currently selects an ingress replica before that replica reads serve_multiplexed_model_id. The model therefore loads correctly on whichever replica HAProxy picked, but repeated requests do not get Serve's normal warm-model affinity and can cause duplicate loads and eviction churn.

What

  • Automatically attach a private ingress request router when an HTTP ingress deployment statically uses @serve.multiplexed under HAProxy.
  • Forward the multiplexed model ID to that router and select through the ingress deployment's existing Serve handle with choose_replica(_reserve=False).
  • Return the selected replica ID to HAProxy and restore the model ID through the existing trusted-router-header path before the original request reaches that replica.
  • Apply the ordinary request-router contract to ingress request routers: every request for a router-bearing application consults the router, independent of HTTP method.
  • Forward all end-to-end request headers to the ingress request router. HAProxy excludes only connection/framing fields such as Host, Content-Length, Connection, Transfer-Encoding, and headers named by Connection; it regenerates the internal request's framing itself.
  • Enable the existing multi-replica affinity test under HAProxy.

Generic Serve model multiplexing carries its model ID in serve_multiplexed_model_id, so this change does not buffer, copy, or parse the client request body. The ingress router receives the normal end-to-end headers, including the model ID, while HAProxy continues streaming the original request directly to the selected replica. Request-body forwarding remains controlled independently for body-aware routers.

The model header receives one additional normalization step: HAProxy accepts the underscore and hyphen spellings, selects one canonical value for the routing decision, removes conflicting copies, and restores the value returned through the trusted router response. This ensures the router and selected replica cannot observe different model IDs.

Why not route entirely inside HAProxy?

balance hdr(serve_multiplexed_model_id) with consistent hashing would be smaller, but it is not equivalent to Serve's multiplexing policy. It cannot prefer the replicas that actually report a model loaded, place a cold model on a replica with the fewest loaded models, or react correctly to LRU eviction. It can consequently retain stale affinity, overload one replica's model cache, and reshuffle placement during scaling.

Publishing loaded-model sets to HAProxy and selecting in Lua could preserve those semantics, but model loads and evictions are dynamic. Doing this without frequent HAProxy configuration reloads would require a new runtime-map/control-plane mechanism. The internal router reuses the existing model-location propagation and routing policy instead.

OpenAI multi-model routing is separate: its model name is in the JSON body, so that path must inspect enough of the body to extract model. This PR addresses generic @serve.multiplexed, whose routing key is an HTTP header.

Scope

This restores cache-aware selection for HTTP ingress. Direct gRPC ingress already propagates multiplexed model IDs correctly, but this HTTP ingress-request-router mechanism does not apply to the gRPC frontend.

Static @serve.multiplexed usage is detected at application build time. A multiplexed wrapper created dynamically inside a replica constructor cannot be detected before the application is deployed and is not auto-routed by this change.

The automatic router is HAProxy-only. With the Python proxy, every request already flows through the ingress deployment's ordinary Serve request router, so deploying the peer ingress-router adapter would be redundant and unused.

Tests

  • Focused pre-commit passed, including Ruff, Black, mypy, and Pyrefly.
  • Added build tests for automatic router attachment and the non-HAProxy boundary.
  • Extended the real-HAProxy router test to verify that POST and GET both use the ingress router, end-to-end headers are forwarded, hop-by-hop headers are filtered, the model header is normalized/restored, and the selected replica is pinned.
  • Validated the generated Lua template with the packaged HAProxy binary.
  • Enabled test_multiplexed_e2e under HAProxy to cover warm-model affinity across two replicas.
  • Focused pytest/Bazel execution was attempted locally, but the available compiled Ray extension predates this checkout and cannot import the current source tree. CI execution is required before this draft is marked ready.

Duplicate search

Searched open Ray PRs for HAProxy/direct-ingress model multiplexing and ingress-router work. The merged #65678 enables model-ID propagation and the merged #65679 adds multiplex support to RoundRobinRouter; neither makes generic HAProxy ingress consult Serve's model-aware router. No open duplicate was found.

AI assistance

AI assistance was used for implementation and testing. The generated changes require submitter review before this draft is marked ready.

@eicherseiji eicherseiji self-assigned this Sep 29, 2026
@eicherseiji eicherseiji added the go add ONLY when ready to merge, run all tests label Sep 29, 2026
@eicherseiji

Copy link
Copy Markdown
Contributor Author

Superseded by native stack #66591: #66588 contains the ingress request-router application code, and #66589 contains the HAProxy dispatch/header changes.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant