AIO Apex
en

TokenFlow Gateway

TokenFlow Gateway is a token-aware gateway and load balancer for self-hosted LLMs. It weighs every request in tokens before it reaches a GPU and admits it only when the target backend's KV-cache can hold it, instead of counting connections.

By Mohammad Safari · Last updated

Why connection counting fails for LLM serving

Nginx and HAProxy balance by connections. An inference server such as vLLM does not run out of connections; it runs out of KV-cache. To a classic proxy a 10-token prompt and a 10,000-token prompt are the same request, so a burst of long-context traffic passes straight through round-robin balancing and arrives as an out-of-memory error, a latency cliff or a crashed inference engine.

How the gateway decides

  1. Weigh before dispatch. Each request is tokenized at the edge; its weight is the prompt tokens plus its max_tokens budget, which is the KV-cache the backend must reserve.
  2. Admit atomically. A request is forwarded only after a Redis Lua check-and-reserve secures that weight against the backend's live capacity. The check is atomic across every worker and replica, so concurrent workers cannot over-admit.
  3. Queue instead of crash. When nothing fits, requests wait in a Redis priority queue and drain as capacity frees; a 503 is returned only after a configurable timeout.
  4. Never compute twice. Deterministic requests are served from an exact (Redis) or semantic (pgvector) cache without touching a GPU.

Routing reads vLLM's Prometheus telemetry: heavy prompts go to the backend with the most free KV-cache, light prompts are bin-packed onto busier nodes to keep headroom for the next heavy one. Reservations carry a time-to-live, so a crashed worker can only make the gateway more conservative until the reservation expires.

Measured: an overload burst

200 concurrent heavy requests, about six times the total admission capacity of two backends (32,768 tokens each), against a cluster of 48 gateway workers. The first version kept reservations in each worker's memory; the current version moves them into Redis.

MetricAtomic Redis admissionPer-worker memory (earlier)
Succeeded / failed (HTTP)200 / 0200 / 0
Queued by the gateway1680
Peak concurrent heavy requests per backend16 (the capacity cap)200 on one backend
Backend split116 / 84200 / 0

Both versions report 200 of 200 requests succeeding. The earlier one did so by sending roughly 402,000 tokens of KV demand to a single 32,768-token backend at once: the failure the gateway exists to prevent, hidden behind a healthy success rate. With atomic admission, 48 workers produce the same admission schedule as a single process.

Measured: gateway overhead

ScenarioConnectionsRequests/s (avg)p50p99
Cache-hit path1,00030,61830 ms40 ms
Full proxy path50018,59825 ms38 ms
Full proxy path10012,4327 ms12 ms

Zero errors, timeouts or non-2xx responses were recorded across more than 2.5 million requests. The atomic reservation adds two to three Redis round trips per proxied request: free at saturation, about 2 ms at the median under low concurrency.

What these numbers do and do not show

The benchmarks ran on one AMD EPYC 9254 bare-metal server (24 cores, 48 threads) with the gateway under PM2 in cluster mode, and Redis, Postgres, the load generator and mock vLLM backends on the same host over loopback. The mock backends are deliberate: they isolate the gateway's own overhead. These are not GPU generation throughput figures, and the gateway has not been measured in front of production GPU traffic.

Sources