Managing Heterogeneous KV Cache at Million-Token Context Scale
This article explains why a high prefix cache hit rate does not guarantee correct output in long-context LLM inference, especially under hybrid attention architectures. It breaks down five distinct cache states—compressed KV blocks, uncompressed tails, sliding window attention state, indexer state, and on-disk prefixes—and shows how each requires separate recovery checks. It also covers upgrading the allocator into a state machine, four validation layers, four consistency constraints, and practical recommendations for separating hit rate from recoverable hit rate.
1. The Starting Problem: Hit Rate Goes Up, Output Starts Breaking
In long-context inference services, prefix caching is almost standard: historical token key-value states are split into fixed-size blocks, a page table maps logical blocks to physical GPU memory blocks, and requests with identical prefixes reuse existing blocks to skip redundant forward computation.
This mechanism relies on an implicit assumption: if the prefix is identical, the cache can be reused directly. That assumption holds approximately when every layer of the model has the same attention pattern. New hybrid attention architectures break it—different layers use full-history compression, sliding windows, and sparse routing, so the same logical prefix leaves completely different physical traces across layers.
This produces a class of failures that are hard to diagnose. The cache is judged a hit by hash, but at read time the uncompressed tail segment is missing, the in-window state is missing, or the routing index is missing. Generated output shows typos, repetition, and semantic drift—while the hit rate metric still looks healthy.
One conclusion must be stated up front: prefix cache hit rate is not a correctness metric. It only describes how much was reused, not whether the reuse is recoverable. In heterogeneous cache scenarios, a higher hit rate may actually mean more incorrect reuse.
2. An Analogy: From “Checking a Shelf Number” to “Confirming Five Things”
A hit in a traditional single-form cache is like finding a shelf number in a library and taking the book directly. A hit in a heterogeneous cache requires confirming five things after finding the number:
- Is the book still on the shelf (is the compressed key-value state complete)?
- Are the latest pages bound (is the uncompressed tail in place)?
- Has the borrowing card expired (does the sliding window coverage remain valid)?
- Is the index catalog synchronized (is the routing/index state consistent)?
- Was it sent to an off-site warehouse and not yet returned (has the prefix evicted to external storage been migrated back)?
The recovery conditions, lifecycles, and validation methods for these five items are independent of each other. If any one is missing, this “hit” cannot be used for direct computation.
3. Separate Governance for Five Cache States
1. Compressed KV Blocks
Produced by full-history compression layers, these blocks have the largest granularity and the highest reuse value, but they are a “lossy summary.” They can only carry coarse-grained historical information and cannot replace nearby details. The validation point is block integrity: the whole block is usable; missing any part makes it unusable.
2. Uncompressed Tail
The most recent tokens usually retain original precision, and the length changes continuously as generation progresses. Its lifecycle is not synchronized with compressed blocks, making it the most common source of “hit but missing data.” In implementation, the tail should be grouped separately and excluded from whole-block hash reuse.
3. Sliding Window Attention State
Sliding window layers retain only the key-values inside the window, and the window rolls forward as positions advance. When reusing across requests, you must verify whether the window interval covers the current query position. Content outside the window is inherently unrecoverable.
4. Indexer State
Sparse routing layers rely on index structures to decide “which historical blocks to read.” When the index is not synchronized, the data retrieval path is wrong even if all key-value blocks are present. This state should be committed in the same batch as the key-value blocks; asynchronous backfill is not allowed.
5. On-Disk Prefix
Prefixes that exceed GPU memory capacity are evicted to external storage, and migration back takes time. Hit determination must distinguish “logical existence” from “physical readiness.” If not ready, it should be treated as a miss or placed in a waiting queue.
4. The Allocator Must Be Upgraded to a State Machine
A traditional allocation action does one thing: pick an empty block for new tokens and write into it. Under heterogeneous caching, the allocator must also record the current form of each token segment; otherwise hash matching reports a hit while the actual read is missing pieces.
Illustrative logic:
def admit(req):seg = req.segmentsif not (seg.compressed_lcm_ok() # compressed blocks are whole-block alignedand seg.tail_ready() # tail is in placeand seg.swa_covers() # window covers the query positionand seg.indexer_synced() # routing index is synchronizedand seg.ondisk_promoted()): # on-disk prefix has been migrated backreturn MISS_OR_WAITreturn HIT
5. Four Validation Layers and Four Consistency Constraints
For a hit to be truly usable, it must pass four validation layers: whole-block alignment of compressed blocks, consistency between operator and runtime conventions, intersection of states across multiple cache groups, and byte-level hardware alignment.
The allocator state machine must not violate four constraints: admission limit control, five-state routing, timing consistency between checking and execution (to avoid state being overwritten after determination), and the address stability requirement imposed by fixed topology in static graph mode.
6. Implementation Recommendations
First, manage by state groups before discussing reuse; separate hit rate from “recoverable hit rate” in statistics; use synchronous commit for tail and index states; always perform explicit migration for on-disk prefixes. If you build an external prefix pool based on a container platform and object storage, factor migration latency into the admission decision. With these four points in place, cache reuse in long-context scenarios can save compute without sacrificing correctness.
