[Serve] Allow app-defined health routes to bypass `max_ongoing_requests`
Description
Every request to a route on a @serve.ingress FastAPI app counts toward the deployment's max_ongoing_requests. A health route such as GET /healthz therefore waits for a free slot while the replicas are saturated. External health checkers (load balancers, Kubernetes probes) time out on it and mark a working application as down.
Repro on Ray 2.54.1 and FastAPI 0.116.0:
import asyncio
import threading
import time
import requests
from fastapi import FastAPI
from ray import serve
app = FastAPI()
@serve.deployment(max_ongoing_requests=2)
@serve.ingress(app)
class Api:
@app.get("/work")
async def work(self):
await asyncio.sleep(6)
@app.get("/healthz")
async def healthz(self):
return "ok"
def timed_get(path):
start = time.time()
status = requests.get(f"http://127.0.0.1:8000{path}").status_code
print(path, status, f"{time.time() - start:.2f}s")
serve.run(Api.bind())
timed_get("/healthz")
for _ in range(2):
threading.Thread(target=timed_get, args=("/work",)).start()
time.sleep(1)
timed_get("/-/healthz")
timed_get("/healthz")/healthz 200 0.65s
/-/healthz 200 0.03s
/work 200 6.04s
/work 200 6.04s
/healthz 200 5.40sThe replica logs GET /healthz 200 2.7ms for the last call. The rest of the 5.40s is spent waiting for a free slot.
The built-in routes do not cover this case. The proxy's /-/healthz reports whether the proxy is draining, has a route table and, on worker nodes, knows a running replica. The replica's /-/healthz exists only with direct ingress, which RAY_SERVE_ENABLE_DIRECT_INGRESS leaves off by default. It returns the result of the controller's last check_health call. Neither runs a request through the application. #33979 asked for a per-application route that calls check_health. It was closed in a backlog sweep for inactivity.
Two possible shapes:
- An option on
@serve.ingressor@serve.deploymentthat lists routes exempt frommax_ongoing_requests, e.g.exempt_routes=["/healthz"]. - A per-application
/{route_prefix}/-/healthzthat callscheck_healthoutside the request semaphore.
Documented guidance on the recommended pattern would also work.
Use case
A FastAPI ingress deployment forwards each request to model deployments through handles. Each forwarded call takes several seconds. A container health check calls the app's /healthz, which reports serve.status(). With the default max_ongoing_requests=5, five concurrent calls left /healthz unroutable. The health check failed. The container supervisor then restarted the service, which failed every in-flight request. Raising max_ongoing_requests to 200 only moves the threshold. A separate health deployment does not exercise the ingress deployment that serves the traffic.
Source: ray-project/ray