When Caching Is Slower Than Computing: The Hidden Cost of Inference Acceleration
This article examines a counterintuitive production incident where enabling a shared cache for a large MoE model's long-context inference caused P99 latency to jump from about 3.96 seconds to 17.3 seconds while median latency barely moved. It breaks down the four overlooked costs of cache reads—addressing and metadata, serialization and layout conversion, bandwidth contention, and tail-latency amplification—and offers a practical framework for deciding when caching is actually worthwhile. Readers will find concrete remediation strategies such as zero-copy layout alignment, batched access, resource isolation, admission control, and observability to turn caching back into a genuine accelerator.
1. A Counterintuitive Incident: P99 Latency Quadrupled After Enabling the Cache
A team serving online inference for a very large Mixture of Experts (MoE) model ran into something that defied common sense. The architecture splits different capabilities across multiple expert subnetworks, and a routing mechanism selects a few experts per token, keeping the parameter count very large without much increase in per-step computation. The service ran on a single node with 8 high-performance accelerator cards, with ample memory capacity and inter-card bandwidth.
To speed up long-text scenarios, the team added a shared cache layer to store previously computed attention intermediate results. The idea was simple: reuse them for requests with identical or highly similar prefixes to avoid redundant computation. After rollout, median latency barely changed, but the slowest 1% of requests (P99) jumped from about 3.96 seconds to 17.3 seconds. The first instinct was to roll back.
The core conclusion of this postmortem is a single sentence: reading from a cache has a cost, and that cost has long been severely underestimated. When the cache read path shares the same hardware resources as the compute path, the cache is no longer just an accelerator that “saves time”—it becomes a new resource competitor and a new failure amplifier.
2. Understanding the Workload First: Attention Compression Changed the Cost Structure
To understand the cause, you first need to see how the model uses memory and bandwidth. This class of model uses a low-rank compressed attention mechanism: each token’s key-value information is compressed into a shared low-dimensional latent vector, then restored on demand during attention computation. The direct benefit is that, regardless of the number of attention heads, the intermediate data each token needs to persist is fixed and very small.
In the incident scenario, FP8 precision was used, with every 256 tokens packed into a “block” of about 12.4MB, which works out to only about 48.4KB per token. This number is critical: it makes the cache look “cheap,” and it also changes the access pattern from “large sequential transfers” to “small, high-frequency random addressing.”
Per-token intermediate data ≈ 48.4 KBPer block (256 tokens) ≈ 12.4 MBCache size for 128K-token long text ≈ 6.2 GB
A 6.2GB footprint is not dramatic compared to accelerator memory, but the problem is the access pattern: during decoding, every generated token requires locating and fetching the corresponding historical block from the cache—a classic case of fine-grained random reads.
3. Where the Cost Actually Hides: Four Overlooked Overheads
3.1 Addressing and Metadata Overhead
The cache is not a contiguous block of memory but a sparse structure made of many small blocks. Every read goes through key construction, hashing, bucket lookup, length validation, reference counting, and lifecycle management. A single operation is only microseconds, but it happens at every decoding step and sits on the critical path, so the accumulation is enough to eat up the computation time it was supposed to save.
3.2 Serialization and Layout Conversion
The compute side requires a specific memory layout (alignment, blocking, channel-first), while the cache side often uses a different layout for generality and sharing. After a hit, a “pack/unpack” step is mandatory, and this overhead is proportional to the number of hit blocks. The more hits, the more conversion—producing the paradox of “high hit rate but slower.”
3.3 Bandwidth and Cache Contention
Cache reads share the same memory bandwidth and on-chip cache with attention computation and expert weight loading. MoE expert routing itself causes irregular weight access, already putting bandwidth under strain; adding fine-grained cache reads on top lowers the on-chip cache hit rate and forces the compute side to wait.
3.4 Tail-Latency Amplification
This is the main cause of the P99 spike. The cache path introduces extra queuing points: lock contention, allocator jitter, and cross-stream synchronization. Median requests take the “clean path” and are almost unaffected; but once a request queues on the cache side, its wait is amplified by synchronization barriers and ultimately shows up as a long tail. In other words, the cache moved the system’s jitter source from the compute side to the storage side without eliminating the jitter.
4. A Decision Framework: When Is Caching Actually Worth It
Don’t ask “should we add a cache,” but rather “is the cache’s read cost lower than the compute cost it replaces.” A simple criterion:
Benefit = hit rate × per-computation timeCost = per-read time + layout conversion time + expected queue waitIf Benefit - Cost <= 0, the cache is a negative optimization
In scenarios with long text, highly overlapping prefixes, and homogeneous in-batch requests, the benefit is usually positive; in high-concurrency, heterogeneous-request, scattered-prefix scenarios, the cost often overtakes it. As an engineering practice, first use small-scale shadow traffic to measure the two quantities “per-read time” and “expected queue wait,” then decide whether to enable the cache at full scale.
5. The Remediation Path: Making the Cache a Real Accelerator
5.1 Zero-Copy and Layout Alignment
Make the cache layout identical to the compute layout, so that after a hit it can be referenced directly as a view, avoiding pack/unpack. When necessary, do the conversion once on the write side, moving the cost to a stage that does not block requests.
5.2 Batched and Coalesced Access
Merge fine-grained random reads into block-ranged reads, align accesses to the 256-token block granularity, reduce the number of addressing operations, and improve bandwidth utilization.
5.3 Resource Isolation and Independent Streams
Put cache reads on a separate execution stream and a separate memory pool to avoid contending with compute for on-chip cache; use bounded queues and backpressure instead of unbounded queuing to cage the long tail.
5.4 Admission Control and Degradation
When cache-side queuing exceeds a threshold, automatically bypass and go straight to the compute path. Better to save a little less than to let tail latency spiral out of control. This is the most direct way to pull P99 back from 17 seconds to under 4 seconds.
5.5 Observability
Instrument hit rate, read-time distribution, conversion time, queue duration, and bypass ratio separately, and build percentile dashboards with logging services and monitoring alerts. Looking only at average hit rate hides the problem; you must look at P99 read time.
6. Conclusion
A cache is not a free lunch; it bets a certain read cost against an uncertain compute saving. As models move toward ultra-long context and hardware moves toward high bandwidth and low latency, the compute side gets faster and faster, and the fixed overhead on the cache side becomes more and more conspicuous. A truly mature inference system is not one with as many caches as possible, but one that can account for every read path. In engineering practice on platforms such as Baidu AI Cloud, the sequence of “quantify the cost first, then apply admission control and degradation, and only then optimize hit rate” is becoming the standard playbook for long-text inference services.
