Hybrid Architecture Cache Redesign: The Critical Path to Faster Prefill
This article explains why prefix caching, a standard optimization in pure-attention models, breaks down in hybrid architectures that alternate full-attention layers with linear or state layers. It contrasts addressable KV caches with non-splittable recurrent state caches, outlines four common failure modes of prefix reuse, and presents a reusable design combining block-aligned addressing, cost-aware eviction, joint KV-state consistency, parallel prefill scheduling, and cache-hit-aware request queuing. Readers will learn where the prefill throughput gains come from, how to validate improvements, and why the results depend heavily on workload characteristics.
1. Background: Why Hybrid Architectures Became the Default for Long-Context Workloads
Over the past two years, a hybrid architecture that alternates full-attention layers with linear or state layers has been widely adopted in model design. The motivation is straightforward: full-attention layers handle precise global information retrieval, while linear layers—including state space models and gated linear attention variants—replace the ever-growing key-value cache with a fixed-size recurrent state. This yields more stable per-step cost and a smaller memory footprint during decoding.
This architecture is especially attractive for coding agent workloads. A typical agent session includes a system prompt, repository retrieval snippets, multi-turn tool call results, and histories of failed attempts and self-correction. Context length easily reaches tens of thousands to hundreds of thousands of tokens, and each turn incrementally appends to the same very long prefix. “Cheap decoding and bounded memory” hits the pain point exactly.
But solving that pain point introduces new complexity: prefix caching, an optimization that works well in pure-attention models, is no longer free to use under a hybrid architecture.
2. Two Fundamentally Different Kinds of Cache
Understanding the optimization space requires clarifying how the two state types differ.
The key-value cache is addressable. It consists of K/V tensors for every attention head in every layer, its size scales linearly with sequence length, and it splits naturally at token boundaries. The cache for any prefix can be independently extracted, concatenated, reused, and even shared across requests.
The state cache is not splittable. Linear layers maintain a set of fixed-size recurrent states (matrices or vectors) that compress the entire history into a constant-length representation. As a result:
- It cannot be trimmed at exact token granularity and can only be recomputed at block granularity;
- The state is path-dependent, so the same text produces different states under different preceding contexts;
- State updates are sequential recurrences, which is inherently unfriendly to parallel chunk processing.
When both are managed within the same request lifecycle, reusing a cache strategy designed for pure attention causes a severe semantic mismatch.
3. Why Prefix Reuse Frequently Fails in Hybrid Architectures
The failure sources fall into four categories.
First, granularity mismatch. The system hashes prefixes at token granularity for matching, but state can only be restored at block granularity. A match of 1,200 tokens may still have to fall back to the nearest block boundary, wasting part of the hit.
Second, failure propagation. If the state of any layer in the hybrid stack misses, the entire chain must be recomputed from that point, invalidating the caches of all subsequent layers—one mismatch triggers a full recompute.
Third, inaccurate eviction. Traditional approximate LRU policies consider only access frequency and recency, ignoring the fact that rebuilding a state cache is far more expensive than rebuilding a key-value cache. The result is that the “expensive” entries are evicted while the “cheap” ones are kept.
Fourth, concurrency interference. When multiple requests share the same prefix, read-modify-write access to the state cache races. Without reference counting and copy-on-write, this leads to duplicated computation or state corruption.
4. A General Optimization Design: Block Alignment and Tiered Caching
The reusable design principles for these problems form a combined approach.
4.1 Use Blocks as the Unified Addressing Unit
Split the prefix into fixed-length blocks (for example, 256 or 512 tokens) and build fingerprints for both key-value and state data at block boundaries. No partial reuse happens within a block, and blocks are guaranteed to be concatenable. Illustrative logic:
BLOCK = 512def match_prefix(blocks, incoming):hit = 0for i, blk in enumerate(incoming):key = fingerprint(blk) # content fingerprint, not positionalif key not in blocks:breakhit = i + 1return hit * BLOCK # hit length is always block-aligned
The key point is that the fingerprint depends only on content, not absolute position, so shared segments such as system prompts and tool definitions can genuinely be reused across requests.
4.2 Hot/Cold Tiering and Cost-Aware Eviction
Treat GPU memory as the most expensive tier, host memory as the second, and distributed or local storage as the third. The eviction score should explicitly include rebuild cost:
score = access_weight / (rebuild_cost * size)
The rebuild cost of a state cache is significantly higher than that of a key-value cache, so at equal access热度 state entries are retained preferentially.
4.3 Joint Consistency of State and Key-Value Data
When reusing a prefix, the corresponding key-value shards and state snapshot must be restored together, bound to the same block version number, to avoid the hidden error of “key-value from a new block, state from an old block.” In practice, a versioned metadata table can manage this uniformly.
4.4 Parallel Prefill and Chunk Scheduling
Feed the long prefix into computation in parallel by block, and handle the state portion with a two-phase strategy: first compute each block’s local contribution in parallel, then perform a lightweight sequential state merge. This preserves recurrence semantics while compressing the serial length from the full sequence to the number of blocks.
4.5 Time-to-First-Token Optimization at the Scheduling Layer
Introduce a “cache hit rate first” scheduling weight during request queuing so high-hit requests are prioritized, which can significantly reduce p90 time to first token. At the same time, throttle low-hit long requests to prevent them from monopolizing compute and creating a long tail.
5. Where the Gains Come From
The several-fold improvement in prefill throughput comes from three effects stacking together. First, hit blocks skip repeated computation, saving attention work at the level of quadratic complexity. Second, chunk-level parallelism breaks up the serial recurrence and raises compute utilization. Third, tiered caching lets large, low-cost media absorb reuse requests, reducing round trips to expensive GPU memory. The improvement in the time-to-first-token tail mainly comes from consistency between scheduling and hit determination, which reduces extremely slow requests caused by fallback recomputation.
It is worth stressing that the exact multiplier depends heavily on workload characteristics: the higher the share of common prefixes, the more turns per session, and the better the block size matches the structure of the business text, the more pronounced the benefit. In short-context, low-repetition scenarios, the gain is limited.
6. Implementation Advice and Validation
A gradual path is recommended: first unify block granularity and fix the fingerprint definition, then add cost-aware eviction, and finally introduce cross-node sharing and distributed caching. Validation should cover three categories of metrics: prefill throughput, time-to-first-token percentiles (especially p90/p99), and cache hit rate along with peak GPU memory.
If general cloud capabilities are used, the pattern is to deploy inference services on a container platform, use object storage or a distributed cache to hold state snapshots, and use monitoring and alerting services to track percentile latency and hit rate, forming a closed tuning loop. Baidu AI Cloud typically supports such scenarios with a combination of general containers, storage, and observability capabilities rather than a single proprietary component.
7. Summary
Hybrid architectures do not make caching simpler; they make cache semantics more complex. Only by getting block alignment, joint consistency, cost-aware eviction, and parallel scheduling right can the decoding advantage at the architecture level be turned into end-to-end service efficiency.
