KV Cache Heterogeneous Governance for Million-Token Contexts
This article examines why prefix-based KV cache reuse breaks down in long-context LLM serving once models adopt hybrid attention and contexts reach million-token scale. It defines five distinct cache objects, explains how the allocator must evolve from free-block lookup into a state machine, and presents four validation layers and four consistency checks that separate cache hit rate from actual recoverability. Readers will find practical guidance for building observable, regression-testable cache governance on cloud infrastructure.
1. Where the Problem Starts: A Prefix Hit Does Not Mean a Usable Cache
Long-context inference serving has long relied on one simple rule: requests that share a prefix can reuse the historical key-value cache directly. Paged cache management splits token history into fixed-size pages and maps them to physical blocks through a page table, so requests with an identical prefix skip redundant prefilling. This mechanism has run stably on general-purpose inference frameworks for a long time, but it carries an implicit assumption: the cache representation of every layer and every segment of history is homogeneous.
That assumption no longer holds once a model introduces hybrid attention (some layers compress the full history, some use sliding windows, some use sparse routing) and the context length reaches the million-token range, where a single GPU can no longer hold the entire history in memory. At that point, a “hash hit” only proves that the token sequence matches; it does not prove the cache state is recoverable. If the serving layer still applies a single rule, you get normal hit reports paired with incorrect generation.
2. Five Cache Objects: Lifecycles and Recovery Conditions
Heterogeneous caches must be modeled explicitly as five object types, each with a different write timing, eviction policy, and recovery path.
- Compressed KV: produced by full-history compression layers, long-lived, reusable across requests; recovery requires the compressed block to be complete and consistent with the current model configuration.
- Uncompressed tail: exact key-values for the most recent tokens, kept to preserve near-range precision; once evicted it must be re-prefilled and cannot be approximated.
- SWA window: sliding-window layers retain key-values only inside the window, so data outside it is naturally absent; recovery depends on aligning the window position with the offset of the current sequence.
- Indexer state: the index and routing information maintained by sparse routing layers; if it is out of sync, attention reads the wrong pages.
- On-disk prefix: history swapped out because GPU memory was insufficient; a hit still requires asynchronous migration back, leaving a window in which the data has “not yet arrived.”
A schematic state description can be written as:
class CacheState:compressed_kv: BlockHandle # reusable, integrity must be verifiedtail: BlockHandle # must be recomputed if missingswa_window: tuple[int, int] # aligned with the sequence offsetindexer: IndexHandle # must stay in sync with the page tableon_disk: DiskHandle | None # hit, but migration back is pending
3. Upgrading the Allocator from “Find a Free Block” to a State Machine
The traditional allocation action simply picks free physical blocks for new tokens, and prefix matching is a parallel hash lookup. Once cache objects become heterogeneous, the allocator must record the current representation of each token segment; otherwise the matching stage reports a hit while the read stage finds missing pieces, and the error surfaces only late in decoding.
The allocator therefore needs to maintain a “representation tag + recovery cost” for each segment and to route requests at admission time: segments that can be reused directly take the fast path, segments that need migration back enter an asynchronous queue, and a missing tail triggers partial recomputation. All state transitions should be observable, which makes inflated hit rates easier to diagnose.
4. Four Validation Layers: Can a Hit Actually Continue the Computation?
- Whole-block consistency: validate compressed blocks as complete units to avoid reusing half a block.
- Kernel and ABI consistency: the cache layout must match the version of the compute kernel.
- State intersection across cache groups: under hybrid attention, take the intersection of the states of all groups; any missing group counts as a miss.
- Byte-level alignment: cross-device or cross-precision scenarios require byte-aligned validation to prevent misaligned reads.
5. Four Consistency Checks That Must Never Be Violated
- Admission limit: cap concurrent swap-in and swap-out to avoid GPU memory and bandwidth thrashing.
- Five-state routing: every request must be explicitly assigned to one of the five states.
- Atomicity of validation and use: prevents the state from being rewritten between the hit decision and the actual read.
- Static topology constraints: static graph execution requires stable cache addresses and shapes, so dynamic migration back must reserve fixed slots.
6. Implementation Recommendations
On the cloud, you can combine a container platform to host inference instances, object storage to hold swapped-out prefixes, and monitoring and alerting to track hit-quality metrics. The key is to measure “hit rate” and “recoverability rate” separately: the former reflects scheduling efficiency, while only the latter relates to correctness. It is worth building metrics and stress-test cases for each of the five states, covering three triggering scenarios: exceeding GPU memory, cross-request reuse, and hybrid attention, forming a regression-testable validation loop.
