BLOG / ROUTING
Cache locality versus load balance in LLM serving
A warm replica can save prefill work and still make a request wait longer. A simple latency budget explains when cache affinity should give way.
A request arrives with a long prefix that one replica has already processed. Sending it there looks obvious. Then you notice that replica is busy and another is ready to work.
A cache hit gives the router a budget for extra waiting: the prefill time it avoids. Spend more than that budget in a queue and the hit becomes a latency loss. Cache affinity should therefore be conditional, with an escape route when the warm worker becomes too busy.
What locality actually saves
In a replicated serving system, prefix state may be local to each worker. A matching prefix can let that worker skip recomputing its cached keys and values. The uncached suffix still needs processing, and its attention can depend on the cached prefix.
The cache must be compatible with the request: token identity, model state, positional interpretation, and relevant execution settings matter. Two prompts that mean the same thing do not necessarily share a reusable prefix.
SGLang's v0.4 router description illustrates a practical combination of prefix affinity and load balancing. That combination matters more than maximizing a cache-hit counter in isolation.
We will consider first-token latency on otherwise comparable replicas. Completion latency adds decode speed and output length to the decision.
Put the alternatives on one clock
For candidate replica r, write a simple estimate:
First-token time(r) ≈ queue wait(r)
+ remaining prefill(r)
+ other first-token overhead(r)
Queue wait includes the delay until the request can make useful progress. Remaining prefill must reflect the actual cached prefix and the worker's expected contention. The final term includes differences such as transport and first-token delivery.
The following values are hypothetical estimates, not measurements:
| Candidate | Queue wait | Remaining prefill | Other overhead | First-token estimate |
|---|---|---|---|---|
| Warm replica, light queue | 80 ms | 40 ms | 20 ms | 140 ms |
| Cold replica | 20 ms | 260 ms | 20 ms | 300 ms |
| Warm replica, heavy queue | 350 ms | 40 ms | 20 ms | 410 ms |
The cache saves 220 ms of prefill. In the first comparison, it costs only 60 ms of extra waiting. In the second, it costs 330 ms. The same prefix match produces opposite decisions.
With equal overheads, the warm replica wins when its additional queue wait is smaller than the prefill time it avoids. This is a decision rule for the example, not a claim that those times are easy to predict.
A request count is a rough load signal
Ten short requests and ten long-context generations place different demands on a worker. Their remaining output lengths, active KV state, and prefill backlog can all differ.
A router does not need a perfect simulation of the engine. It does need to acknowledge what its load estimate misses. Useful inputs include queued prefill tokens, active sequences, recent service times, and memory pressure. Their value depends on how quickly they become stale and how much collecting them costs.
There is also feedback. Sending repeated prefixes to the same worker warms its cache, which attracts more matching requests, which can turn locality into a hotspot. A popular prefix may deserve deliberate replication even though that duplicates cached state.
This feedback makes routing a policy over future cache placement as well as the current request. Perfectly equal request counts can erase useful locality; unconditional affinity can concentrate a popular prefix on an overloaded worker.
Give affinity an escape condition
Suppose the warm and cold estimates differ by 5 ms, while recent prediction errors are 40 ms. The arithmetic produces a winner, but the evidence does not support a precise decision. Rapidly changing routes on differences that small can create churn without a dependable latency benefit.
A practical policy can separate four questions:
- Eligibility: can this worker serve the request, and is the prefix state compatible?
- Overload: does a hard queue or memory limit rule out the worker regardless of locality?
- Advantage: does avoided prefill exceed the extra queueing and other overhead?
- Confidence: is that advantage large enough to justify changing the existing affinity?
The confidence margin is a policy choice informed by observed prediction error, not a fixed constant supplied by this example. An overload limit remains an escape condition even when the estimates are uncertain.
Cache information can be wrong too. An approximate cache map may still show a prefix after eviction. Log predicted matches and actual reuse separately; otherwise an apparently successful routing policy may repeatedly pay cold-prefill costs.
A hit counter cannot validate the policy
For each selected route, retain its predicted queue and prefill costs, the cache match expected, and the latency actually observed. This makes estimation errors visible. It does not reveal what would have happened on the unchosen worker.
To compare policies, replay the same arrival and prefix distributions in controlled runs with a consistent cache warmup procedure. Include skewed popularity and bursts. Each policy should build its own cache state: that feedback is part of its behavior. Separate those results from single-decision tests against a fixed cache snapshot.
Compare tail first-token latency by request class, useful completed work, and recomputed prefill tokens. A higher hit rate accompanied by worse latency is evidence that the policy bought locality too expensively.
This budget targets first-token latency. For completion-time objectives, extend it through decode: a warm worker can start first and finish last. The objective determines which saved work is worth waiting for.
What to remember
- Cache affinity exchanges avoided prefill for potentially longer queueing.
- Treat avoided prefill as a budget for extra queueing, with a hard overload escape condition.
- Require a meaningful predicted advantage before changing affinity; validate predictions against actual reuse and latency.
- Evaluate routing under popularity skew, bursts, and the latency objective you actually serve.