When Caching Is Slower Than Computing: The Hidden Cost of Inference Acceleration
This article examines a counterintuitive failure mode in large language model inference: enabling a shared cache for attention intermediate results left median latency unchanged while pushing P99 tail latency from roughly 3.96 seconds to 17.3 seconds, eventually leading to a full rollback. Using a Mixture of Experts (MoE) model with Multi-head Latent Attention (MLA) as the example, it explains why block-level cache I/O becomes expensive under Prefill Context Parallelism (PCP), how a shared L2 key-value store turns into a single point of contention, and what practical changes make caching a real accelerator: asynchronous batched writes, isolated read and write paths, calibrated capacity and eviction, local-first lookups, timeout-based degradation inside parallel groups, and observability built on percentile and end-to-end metrics rather than hit rate alone.
Introduction: A Common Assumption Turned Upside Down
When building inference services for very large Mixture of Experts (MoE) models, latency optimization for long-context workloads is almost unavoidable. A widely adopted approach is to persist the attention intermediate results that have already been computed, forming a cache shared across requests, so that similar requests can reuse them and skip redundant computation.
In one real-world deployment, however, a team observed the opposite of what intuition predicted. After enabling the shared cache, median request latency barely improved, while the slowest 1% of requests (P99 tail latency) degraded from about 3.96 seconds to 17.3 seconds. The feature was ultimately rolled back entirely.
The core claim of this article fits in one sentence: reading from a cache is not a zero-cost operation. When the write, lookup, transfer, and synchronization overhead of a cache exceeds the computation it saves, the cache stops being an accelerator and becomes a bottleneck amplifier. Below, we start from the system architecture, unpack the root causes layer by layer, and outline practical changes that can be implemented.
1. Understanding the Workload: Why Attention Intermediate Results Are Worth Caching
Take a model that uses a low-rank compressed attention mechanism (commonly known in the industry as MLA, Multi-head Latent Attention). Its core idea is to compress the key and value information of each token into a shared low-dimensional latent vector. The direct benefit: regardless of how many attention heads the model has, the amount of intermediate data that must be persisted per token is fixed and small.
At FP8 precision, every 256 tokens are typically packed into one “block,” with a single block occupying roughly 12.4MB, which works out to only about 48.4KB per token. This order of magnitude tells us two things:
- A single block is not large, so in theory a higher cache hit rate could meaningfully reduce repeated attention computation.
- But “not large per block” does not mean “cheap to read.” Once concurrency rises, block-level I/O is amplified into system-level congestion.
2. Understanding the Hardware: Parallel Inference Turns Local Slowdowns into Global Ones
The prefill stage for long contexts is extremely compute-intensive. In engineering practice, Prefill Context Parallelism (PCP) is often used to split a long sequence and hand the pieces to multiple accelerators for simultaneous processing. Once processing finishes, the cards must exchange and synchronize intermediate results over a high-speed network so that every card holds the complete context.
Under this architecture, the 8 cards within a node (that is, 8 ranks) behave like meshed gears: if any one card waits, the other seven must idle. This means a single point of I/O jitter is amplified by the synchronization barrier into a full-node stall, which sharply stretches tail latency.
3. Root Cause: L2 in the Two-Tier Cache Becomes the Single Point
A typical implementation splits the cache into two tiers:
- L1: close to the compute units, residing in local memory, fast but small.
- L2: a larger-capacity key-value store that can be shared by multiple inference instances, similar to a public library.
The problem lies precisely in L2. Consider a chain of bubble tea shops: each store keeps its own books (L1), but headquarters requires all transactions to be consolidated into a single master ledger (L2), and only one accountant maintains that ledger. At peak hours, eight stores write simultaneously and all queue up at the accountant. Mapped onto the system, concurrent block-level writes contend for the same backend resource, read requests are forced to queue, and upstream synchronization waits are triggered.
4. Optimization Path: Turning the Cache into a Real Accelerator
- Asynchronous writes with batch coalescing: remove cache writes from the critical path, using a bounded queue and batched flushes so that synchronous writes never block the main inference flow.
- Isolated read and write channels: assign separate connection pools or separate instances for writes and reads so they cannot starve each other.
- Calibrated capacity and eviction policy: size capacity based on block size, concurrency, and the reuse window; prioritize reusing high-hit content and avoid frequent thrashing eviction.
- Local-first and proximity hits: serve hits from L1 whenever possible, with L2 only as a cross-instance fallback, reducing cross-network round trips.
- Fault-tolerant degradation within the parallel group: set timeouts and degradation paths on the synchronization barrier so that a single waiting card can fail fast instead of blocking the whole group.
- Observability first: instrument hit rate, lookup latency, write queueing time, network transfer time, and synchronization wait time separately. Only then does “the cache is slow” turn from a feeling into a metric.
Illustrative strategy pseudocode:
on request(block_keys):hits = L1.lookup(block_keys)if hits.complete:return compute_with(hits)remote = async_pool.submit(L2.batch_get, block_keys)hits = await with_timeout(remote, budget=5ms)if hits.timeout:return recompute(block_keys) # Degrade: recompute rather than stall the whole groupL1.insert(hits)write_queue.offer(L2.batch_put, hits) # Async write, does not block the main pathreturn compute_with(hits)
5. Conclusion
A cache is not an inherent performance asset; it is a cost that must be repaid: memory footprint, network round trips, backend queueing, and synchronization waits. In a highly parallel inference architecture, that cost is multiplied by the synchronization mechanism and ultimately shows up as degraded P99 rather than a degraded median. Whether a cache is worth enabling should be judged on percentile latency and end-to-end metrics, not on a single-point hit rate. To put this kind of inference optimization into practice on Baidu AI Cloud, you can combine general-purpose capabilities such as container platforms, object storage, log services, and monitoring and alerting to build an inference pipeline that is observable, degradable, and elastically scalable.
