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
- Weigh before dispatch. Each request is tokenized at the edge; its weight is the prompt tokens plus its
max_tokensbudget, which is the KV-cache the backend must reserve. - 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.
- 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.
- 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.
| Metric | Atomic Redis admission | Per-worker memory (earlier) |
|---|---|---|
| Succeeded / failed (HTTP) | 200 / 0 | 200 / 0 |
| Queued by the gateway | 168 | 0 |
| Peak concurrent heavy requests per backend | 16 (the capacity cap) | 200 on one backend |
| Backend split | 116 / 84 | 200 / 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
| Scenario | Connections | Requests/s (avg) | p50 | p99 |
|---|---|---|---|---|
| Cache-hit path | 1,000 | 30,618 | 30 ms | 40 ms |
| Full proxy path | 500 | 18,598 | 25 ms | 38 ms |
| Full proxy path | 100 | 12,432 | 7 ms | 12 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
- Source code and deployment guide: github.com/mosafariuk/TokenFlow-Gateway
- Full benchmark method, environment and raw output: benchmarks/RESULTS.md
- Related upstream work on vLLM: merged pull requests