Governance of Heterogeneous Caches for Million-Token Long-Context Inference

    This article examines why prefix cache hits do not guarantee correctness in long-context inference once models adopt hybrid attention. It breaks the cached state into five object types with distinct lifecycles and recovery conditions, then explains how allocators must track state shape, how four layers of hit validation work, and which four state-machine constraints keep reuse safe. It closes with practical advice on monitoring hit versus successful reuse, setting per-object watermarks, and validating checks with shadow traffic.

    1. Where the Problem Starts: A Prefix Cache Hit Is Assumed to Guarantee Correctness

    In long-context inference serving, prefix caching is almost a standard optimization: the key-value states of historical tokens are split into fixed-size pages, a page table maps them to physical GPU memory blocks, and requests reuse them directly whenever the prefix hashes match. This mechanism runs reliably on pure full-attention models, under the assumption that “attention semantics are consistent across every layer and history can be fully replayed.”

    Once a model introduces hybrid attention—some layers compress the full history, some use sliding windows, and some use sparse routing—the implicit rule that “a matching prefix means safe reuse” breaks down. The hit report is a boolean value, but whether computation can actually continue depends on what form the matched history is in at that moment.

    2. Five Cache Object Types: Different Lifecycles and Recovery Conditions

    Explicitly naming the heterogeneous states is the first step toward governing them. In engineering terms, they can be unified into five object types:

    • Compressed KV: a lossy summary from full-history layers, with the longest lifecycle, but it must stay version-consistent with the uncompressed portion.
    • Uncompressed tail: the exact state of the most recent tokens, rolling forward as generation advances and invalidated whenever the window slides.
    • SWA window: local layers retain only in-window history; out-of-window content is invisible to the current layer, so recovery must rebuild the boundary.
    • Indexer state: the directory structure that sparse routing layers depend on; if it is not synchronized, routing points to the wrong shard.
    • On-disk prefix: history offloaded to external storage once GPU memory capacity is exceeded; it can be hit but must be migrated back, subject to queuing and bandwidth constraints.

    An analogy is a library: finding the shelf number is only the beginning. You still need to confirm the book is on the shelf, the latest pages are bound, the borrowing card has not expired, the index catalog is synchronized, and the book has not been sent to a suburban warehouse. Only when all five are confirmed is the book truly ready to continue reading.

    3. Upgrading the Allocator’s Job: From Picking a Free Block to Recording Shape

    A traditional allocation action only picks a free GPU memory block for new tokens to write into. Under heterogeneous caching, the allocator must attach a shape label to every token segment; otherwise, prefix matching reports a hit by hash, but the actual read is missing the tail, the window, or the routing.

    Illustrative pseudocode:

    1. def alloc(tokens, layer_group):
    2. block = free_pool.take()
    3. block.state = classify(tokens, layer_group) # one of the five types or a combination
    4. block.meta = {
    5. "lcm_version": current_lcm(),
    6. "window_end": tokens.global_end,
    7. "index_synced": indexer.is_synced(),
    8. }
    9. return block

    4. Four Layers of Hit Validation: A Hit Does Not Mean Reusable

    1. Whole-block least common multiple alignment: page sizes across cache groups must use a least common multiple to avoid cross-group slice misalignment.
    2. Kernel binary interface consistency: operator signatures on the compression and decompression paths must match; version drift causes silent errors.
    3. State intersection across multiple cache groups: the intersection holds only when all layer groups are recoverable at the same time.
    4. Byte-level hardware alignment: offload and migration back are validated for byte alignment to prevent boundary data corruption.

    5. Four State-Machine Constraints

    • Admission limit: GPU memory and migration-back bandwidth jointly determine the concurrency limit; requests beyond it should queue rather than fall back to degraded reuse.
    • Five-state routing: each object type follows a different recovery path; a uniform “full reload” is prohibited.
    • Time-skew and state-pointer consistency: eviction may occur between the hit decision and the actual read, so a second confirmation using the state pointer version number is required.
    • Static topology constraint: in graph-capture mode the topology is fixed, so dynamic migration back must go through a pre-registered path.

    6. Practical Recommendations

    First, add instrumentation in the monitoring and logging service to distinguish “hit” from “successful reuse,” and expose them as two independent metrics. Then set capacity watermarks and eviction policies for each of the five object types. Finally, use shadow traffic to verify whether the validation layer is wrongly letting cases through. If you run load tests on general-purpose container and monitoring capabilities from platforms such as Baidu AI Cloud, these metrics can be wired directly into alerting to quickly locate the “hit but wrong result” path.