KV Cache-Aware Routing Across LLM Replicas

Vladimir Lomtev•
KV Cache-Aware Routing Across LLM Replicas

When a model runs across multiple replicas, a standard load balancer spreads requests round-robin and breaks the benefit of prefix caching: the next turn in a conversation lands on a replica that does not have that history in cache. KV-cache-aware routing sends the request to the replica that already holds the longest matching prefix, and falls back to a less loaded replica when the cache-heavy replica is overloaded. This logic is provided by SGLang Model Gateway, llm-d, NVIDIA Dynamo, and gateways based on Gateway API Inference Extension.

Prepared by AI Studio by Vladimir Lomtev on November 16, 2026, for teams whose LLMs are served by two or more vLLM or SGLang replicas. The description of mechanisms and flags has been checked against project documentation; for Dynamo, we relied on the router page for version 0.8.1 and recommend cross-checking it against the documentation for your version. We did not run our own measurements; the calculations below are illustrative only.

Contents

Why a round-robin load balancer loses cache

Prefix caching in the engine saves prefill work if the beginning of a new request matches what has already been processed on the same replica. The mechanism is explained in detail in the article about KV-cache and prefix caching. Each replica's cache lives in its own video memory, and a neighboring replica cannot use it.

A simple example. There are four identical replicas and a round-robin load balancer. A user carries on a conversation over ten turns. Each next turn includes the full previous history, but it lands on the same replica as the previous one only about once in four times, or with a probability of around 25%. In the other three cases, the history is prefilling again from scratch. The longer the history and the more replicas there are, the more GPU time is spent repeating work.

The same picture applies to shared system prompts. If the first 3,000 tokens are the same for all requests, each of the four replicas keeps its own copy of that prefix. That is not a problem as long as there is enough memory, but when memory runs short, the prefix is evicted and recomputed in turn on each replica.

Three ways to route requests

Method How it works Pros Cons
Session affinity A hash of the user or conversation ID selects the replica Simple, works with any load balancer Does not account for shared prefixes or load; imbalances are possible
Approximate prefix-based routing The router keeps its own index of what it sent where Requires no changes to the engine The index drifts from the real cache after evictions
Exact KV-event-based routing The engine publishes events about block creation and eviction, and the router builds an index Knows the true cache state Requires engine events, tokenization, and separate infrastructure

Session affinity

The simplest option: an Nginx, Envoy, or your own gateway load balancer hashes session_id and always sends the conversation to one replica. For chats with long histories, this already delivers most of the benefit. There are two weak points: a popular client or agent can overload a single replica, and if a replica goes down, all of its sessions will move and lose cache.

Approximate index

The router remembers which prefixes it sent where and compares new requests against them. In the llm-d documentation , this estimator is described as lightweight and free of external dependencies: it splits the prompt into fixed-size blocks, by default 64 tokens, approximating them by characters, and scores them by the share of matching blocks. The documentation also notes that this index can diverge from the actual server state.

Exact index based on KV events

The engine itself reports which blocks it created and evicted. In exact mode, llm-d uses token IDs and events from the model servers via ZeroMQ and builds a global index from them. Before the event for a new block arrives, the next request with the same prefix could go to a different replica; for that case, there is speculative indexing, which adds a temporary entry immediately after the routing decision.

How to combine cache and load

If you send everything to wherever the cache is best, one replica will become hot and the others will sit idle. That is why all tools combine cache hits with a load estimate, and each does it in its own way.

NVIDIA Dynamo. On the Dynamo 0.8.1 KV router page , replica cost is calculated as kv_overlap_score_weight × blocks that need to be recomputed + active blocks, and the replica with the lower cost is chosen. The default weight is 1.0: higher means reducing prefill and time to first token matters more; lower means generation speed matters more. --router-temperature defaults to 0.0 and gives a deterministic choice of the best replica; values above zero enable probabilistic selection and smooth out imbalances.

SGLang Model Gateway. According to the gateway documentation , the cache_aware policy keeps a prefix tree for each replica, built from KV events, and sends the request to the replica with the longest match if it is not below the threshold. If there is no such replica or it has reached the admission limit, power_of_two kicks in: choose two random replicas and give the request to the less loaded one. The --affinity-mode balanced mode lets you divert requests away from an overloaded cache replica.

llm-d. In the exact routing guide , the prefix-cache-affinity-filter keeps a group of requests on warmed replicas as long as their load stays below the calibrated threshold peakPrefillThroughput. When the warmed replicas are saturated, the filter switches to the least loaded ones, and token-load-scorer chooses the replica with the lowest token-processing load.

Launching on SGLang: documentation example

Each replica publishes KV events and load information:

python -m sglang.launch_server \
  --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \
  --kv-events-config '{"publisher":"zmq","endpoint":"tcp://*:5557","topic":"kv"}' \
  --load-publish-endpoint auto \
  --port 8000

The router connects to replicas with the cache_aware:

sgl-router \
  --model-id meta-llama/Meta-Llama-3.1-8B-Instruct \
  --worker-urls http://node1:8000 http://node2:8000 \
  --chat-routing reorg \
  --policy cache_aware \
  --host 0.0.0.0 --port 30000

Readiness is checked with the request curl http://localhost:30000/readiness, until it returns HTTP 200. If the router is listening on all interfaces, close the port with network rules or place an authenticated gateway in front of it.

Running in Kubernetes: llm-d and Gateway API Inference Extension

Gateway API Inference Extension is the official Kubernetes project that turns gateways with ext-proc and Gateway API support, such as Envoy Gateway, kgateway, and GKE Gateway, into inference gateways. The replica set is described by the InferencePool resource, and the choice of a specific replica is made by an Endpoint Picker that receives model server metrics. Among the routing signals, the project mentions prefix cache state and the presence of LoRA adapters. For production, the documentation suggests using a ready-made Endpoint Picker, such as the llm-d router.

In llm-d, for the precise mode, each vLLM server is launched with --kv-events-config, which specifies the publisher zmq, the address, and the topic with the pod IP and port. The guide lists limitations worth knowing in advance:

  • one router replica — load accounting in token-load-scorer is local to the process, and multiple active routers would only see part of the load;
  • the index is not fault-tolerant — a router started after the replicas does not know about prefixes cached before the event replay window;
  • hits are not published — vLLM reports a block only when it computes it again, so cache hits do not update the index;
  • tokenization puts load on the replicas' CPU and competes with serving.

Hot spots and how to avoid them

Cache-aware routing amplifies skew if traffic has a strong shared prefix. For example, a corporate assistant with one long system prompt and dozens of departments: all requests will be drawn to the replica where that prompt is already cached.

What helps:

  • limit the share of load that a cache replica can receive through the tool's load-balancing settings;
  • add randomness to the selection, like --router-temperature in Dynamo;
  • keep a short shared prefix warm on all replicas, and route by the longer unique part — the conversation history or documents;
  • check request distribution and load across replicas in monitoring, not just total throughput.

How to measure the effect

vLLM publishes counters that show whether the cache is working. According to the metrics list vllm:prefix_cache_queries counts the requested tokens, and vllm:prefix_cache_hits counts the tokens found in the cache. Hit rate on a replica:

sum by (instance) (rate(vllm:prefix_cache_hits_total[10m]))
/
sum by (instance) (rate(vllm:prefix_cache_queries_total[10m]))

The _total suffix in Prometheus may differ depending on the version; check the output of /metrics. Experiment order:

  1. Record a baseline with the current load balancer: hit rate, p50 and p95 time to first token, and the load on each replica.
  2. Apply realistic load: multi-turn conversations with pauses, shared system prompts, agent chains. The methodology is described in the article on load testing a local LLM.
  3. Enable session stickiness and repeat the test.
  4. Enable cache-aware routing and repeat the test.
  5. Compare not only the average time to first token, but also the load distribution across replicas and the tail latency.

When you don't need it

  • One replica: there is nowhere to route, so prefix caching in the engine is enough.
  • Short independent requests with no shared part: there will be almost no hits.
  • Long responses with short prompts: the bottleneck is generation, not prefill.
  • No monitoring: without hit and load metrics, you won't know whether the setup helped.

FAQ

How is this different from offloading the KV cache to shared storage? Routing sends the request where the cache already exists. Shared storage lets a replica load someone else's cache. They can be combined; offloading is covered in the article on offloading the KV cache to RAM and NVMe.

Is session stickiness in Nginx enough? For chats with long histories and dozens of users, often yes. Precise routing pays off when there are many replicas, many shared prefixes, and uneven load.

Do you need Kubernetes? For llm-d and Gateway API Inference Extension — yes. SGLang Model Gateway and Dynamo can also run outside Kubernetes.

What should you do if a replica fails? Its sessions will move to other replicas and go through prefill again. Build this into your capacity reserve and test the failure scenario.

How Vladimir Lomtev's AI studio helps with routing between replicas

Vladimir Lomtev's AI studio deploys LLMs on company servers and integrates them with enterprise systems. For multiple replicas, this means:

  • use metrics to estimate how much prefill is being lost because of the current load balancing;
  • choose an approach: session stickiness, the SGLang router, llm-d, or Dynamo — based on your engine and infrastructure;
  • run a comparative load test on real conversations before and after the change;
  • set up monitoring for cache hits and replica load.

The first step is to choose one service with multiple replicas, record the current time to first token and cache hit rate, and decide what improvement will count as success. Discuss the task.

Bottom line: start by binding to the session

Routing by KV cache preserves the benefits of prefix caching, which a round-robin load balancer destroys. Start by binding the conversation to a replica, measure the hit rate and time to first token, and move to event-level KV-aware routing when you have enough replicas, shared prefixes, and load imbalance to justify the extra infrastructure.

Request an audit

Share your contact details and we will follow up.

← All articles

Comments (0)

Loading comments…

Leave a comment
No registration required

Book a strategy call
for agentic operations

Tell us which workflow you want to improve. We will map feasibility, risks, and the fastest MVP path.

By submitting, you agree to our privacy policy

Contacts

Global Operations

Serving U.S. clients remotely
with private cloud and on-prem options

Strategy calls by request

We respond after reviewing your workflow context.

info@agentsunrise.pro

For partnership inquiries

Have a proposal?

Write to us in messengers

© 2026 AgentSunrise