KV-Cache-Aware Routing for LLM Serving

Prefix caching only pays off when the next request lands on the replica that already holds the cache. How KV-cache-aware routing keeps it warm at scale.

You turned on prefix caching, watched the cache hit rate climb on a single replica, and shipped it. Then you scaled to four replicas behind a load balancer and the hit rate fell off a cliff. The feature still works. The problem is that your load balancer has no idea the cache exists.

This post is for engineers running more than one copy of an open model behind an endpoint, usually on Kubernetes, who have already turned on automatic prefix caching and want it to keep paying off as they add replicas. Cache-aware routing is the piece that connects the two. It is also a good example of how an optimization that is obviously correct on one machine stops being obvious the moment there are several.

The gap between one replica and many

Prefix caching works because a lot of requests share a leading chunk of tokens. A long system prompt, a few-shot block, a tool schema, the retrieved documents in a RAG call, the earlier turns of a conversation: all of that is the same tokens every time, and the KV cache built during prefill can be reused instead of recomputed. On one GPU, the engine keeps those blocks around and the second request skips straight to the new tokens.

A load balancer breaks that assumption without meaning to. Round-robin and least-connections both treat replicas as interchangeable, because for a stateless web service they are. An LLM replica is not stateless. It carries a GPU’s worth of KV cache that is specific to the traffic it has already seen. When the balancer sends the follow-up turn of a conversation to a different replica than the first turn, that replica has never seen the prefix, so it prefills the whole thing again. Spread across N replicas, a shared prefix tends to get recomputed on all N of them, and your carefully warmed cache is mostly cold misses.

Left: a round-robin balancer fans three requests that share a prefix S across three replicas, and each one recomputes S, so every lookup is a miss. Right: a cache-aware router sends all three to the replica that already holds S, so the prefix is prefilled once and reused. A note warns that pure affinity turns that replica into a hotspot, so real routers blend a cache score with a load score.

The fix is to make the router care which replica holds which cache, and to send a request where its prefix is most likely to already live. That is the whole idea. The two hard parts are figuring out where the cache is and keeping that knowledge from turning one replica into a hotspot.

Two ways to know where the cache lives

There are two families of implementation, and the difference is about who tracks the cache state.

The first is approximate, or history-based. The router keeps its own guess of what each replica has cached, usually a radix tree per worker built from the requests it has already sent there. It never asks the workers anything. When a request comes in, the router walks its trees, finds the worker whose tree shares the longest prefix with the new request, and sends it there. This is what the SGLang router does: it maintains an approximate tree per worker from request history, which means no cache-state query on the hot path, and it evicts least-recently-used branches to bound memory. The cost is that the guess can drift. If a worker evicts a prefix under memory pressure, the router’s tree still thinks it is there, and you route to a cold cache believing it is warm.

The second is event-based, or precise. Each worker publishes what it actually caches, and the router maintains a global index of which KV blocks live on which worker. In llm-d, vLLM pods emit KV cache events over ZeroMQ, and an indexer component ingests them so the scheduler can score pods by real cache contents rather than a guess. NVIDIA Dynamo tracks cached blocks per worker the same way and scores each worker by how many blocks of the incoming request it already holds. This matches the true state and survives evictions correctly, at the price of more moving parts and a little lag between an eviction and the index catching up.

Two router designs side by side. On the left, an approximate router keeps a radix tree per worker built from past requests and never queries the workers: cheap and no worker traffic, but the guess drifts after an eviction. On the right, an event-based router keeps a global KV index that the workers feed with cache events: it matches the real cache state and survives evictions, at the cost of more moving parts and event lag.

Neither is strictly better. Approximate routing is almost free to run and needs nothing wired into the workers, which makes it a good default when a single router sees all the traffic. Event-based routing is what you want when cache churn is high, when several gateways share the same pool, or when a wrong guess is expensive enough to justify the plumbing.

Locality versus load

Here is the part that bites people who implement this themselves. If you route purely by cache affinity, you build a hotspot. Every request that shares the popular system prompt piles onto the one replica that cached it first, that replica’s queue grows, and your p99 latency gets worse than it was under dumb round-robin. You optimized for cache hits and lost on the thing cache hits were supposed to help.

So every serious router does not route by cache alone. It scores each candidate replica on two axes, cache overlap and current load, and picks the best combined score. A replica with a perfect prefix match but a long queue can lose to a replica with a partial match that is sitting idle, because waiting behind a full batch costs more than redoing some prefill.

A new request is scored against three replicas. Replica A has the warmest cache but is saturated, so its high cache score is cancelled by a heavy load penalty. Replica C is idle but has no cache overlap. Replica B has partial overlap and a short queue, wins the combined score, and gets the request. Below, the scoring formula: score equals a cache weight times prefix overlap minus a load weight times replica load.

The exact formula differs by project but the shape is the same. llm-d’s default scheduling profile combines a prefix-cache scorer with a queue scorer, with the prefix-cache term weighted heavily, and adds safeguards so one pod cannot be overwhelmed. Dynamo scores workers by cached-block overlap against projected load and picks the cheapest, with an optional temperature that adds randomness so ties do not always break the same way. The SGLang router goes further and switches strategy outright: it routes by cache affinity while the workers are balanced, and falls back to shortest-queue once the gap between the busiest and least busy worker crosses a threshold.

The two weights are the real knob. Push toward cache and you get higher hit rates and more hotspot risk. Push toward load and you get smoother utilization and more recompute. There is no universal setting, because it depends on how skewed your prefixes are and how tight your latency target is.

What it looks like in the real stacks

You rarely build this yourself. Three places give it to you, at different levels of abstraction.

The vLLM production stack ships a router in front of a set of vLLM replicas with several routing policies, including round-robin, session-ID affinity, prefix-aware, and KV-cache-aware. Prefix-aware routing forwards a request to the node whose memory pool already holds the precomputed prefix. KV-cache-aware routing, backed by LMCache, sends each request to the instance with the highest cache hit rate, so it follows the cache even across the tiers LMCache can spill into. The project’s launch write-up reports prefix-aware routing plus KV-cache sharing cutting response delay several times over and raising throughput; those are the maintainers’ own numbers on their own setups, so read them as a direction, not a guarantee.

llm-d sits one layer up as a Kubernetes-native stack built on the Gateway API Inference Extension, a Kubernetes SIG project that teaches a standard gateway like Envoy or Istio to make inference-aware routing decisions. The gateway pauses each request, asks an endpoint-picker over gRPC which pod to use, and forwards it there. The picker reads its candidate pods from an InferencePool and runs the filter-score-pick pipeline described above. This is the path to reach for when you already run on Kubernetes and want routing to be part of the platform rather than a bespoke proxy. Session affinity, incidentally, is the cheap first version of all this: pin a conversation to a replica with a sticky session or consistent hashing and you get multi-turn locality for free, without any cache introspection at all. It just cannot help across different sessions that happen to share a system prompt.

Dynamo and the SGLang router are the other two mature options, the former as part of NVIDIA’s distributed serving framework and the latter as a lightweight router for SGLang workers. If you are picking, the honest filter is what you already run: the vLLM stack if your replicas are vLLM, the SGLang router if they are SGLang, llm-d if you want it native to the gateway, and Dynamo if you are already in that ecosystem or running disaggregated serving where routing and the prefill-decode split need to cooperate.

Tradeoffs and failure modes

Cache-aware routing is not free, and it does not help every workload.

It does nothing if there is no shared prefix. If every request is unique, there is no cache to be aware of, and the routing overhead is pure cost. The win scales with how much your traffic overlaps, so measure your prefix hit rate before assuming this is the problem.

It fights with autoscaling. A freshly scaled-up replica has a cold cache, so a cache-aware router will route around it, which means it stays cold and idle exactly when you added it to shed load. Good implementations give new replicas a grace period or lean on the load term to send them traffic anyway; naive ones leave you paying for a GPU that does nothing.

The approximate variant drifts. Because the router’s model of the cache is a guess, an eviction it did not see leads to a confident route into a cold cache. This is usually fine in aggregate and occasionally bad for a specific request, which is the kind of tail behavior that is annoying to debug because the route looks correct.

It adds a dependency on the hot path. The router is now a stateful thing between your clients and your models, holding either a set of radix trees or a live index fed by every worker. That is more to run, more to monitor, and one more place a bad deploy can take down the whole endpoint. Session affinity avoids most of this, which is why it is worth trying first.

When I would reach for it

The order I actually use is cheapest-first. Turn on continuous batching and prefix caching on each replica, because those are the big single-GPU wins and they cost nothing at the routing layer. Then, if you run more than one replica and your traffic has real prefix overlap, add session affinity so multi-turn conversations stick to a replica. Reach for full cache-aware routing when session affinity is not enough, which happens when the overlap you are missing is across sessions rather than within them: a shared system prompt, a common few-shot block, the same retrieved context showing up in many users’ requests.

That cross-user overlap is exactly the profile behind Archi, the retrieval copilot I worked on for CMS operations at CERN. Operators ask different questions, but the prompts carry the same instruction block and often pull in the same popular logbook entries and tickets, so a lot of the leading tokens are identical across users. Scatter those across replicas and you recompute the shared part again and again; route them by cache locality and you prefill it once. Building dedicated inference for open models is the same shape of problem at the platform level, where the routing layer is one of the few places you can claw back GPU time without touching the model at all. The question I always ask first is whether the overlap is really there. When it is, cache-aware routing is the tool built for it. When it is not, a sticky session and a plain load balancer will serve you better and keep your stack smaller.


Image credit: diagrams by Muhammad Hassan Ahmed, released under CC0. No third-party images were used.