Inference Cache Is Slower Than Compute: The Hidden Cost of Inference Acceleration
Caches are widely assumed to speed up LLM inference, but shared caching can quietly make latency worse. This article explains why, using a real-world case where a shared cache left median latency flat while pushing p99 from about 3.96 seconds to 17.3 seconds on a large MoE model. It breaks down where cache access time actually goes, why synchronization barriers amplify tail latency, and how to move caching off the critical path with asynchronous reads, batched writes, admission control, locality, communication isolation, and observability. It also includes a practical rollout checklist for measuring baselines, hit rates, and automatic fallback thresholds.
1. The Assumption That Fails: Cache Does Not Automatically Mean Speed
A typical inference deployment looks like this: a team serves a very large Mixture-of-Experts (MoE) model online—an architecture that lets different parts of the model handle different types of tasks, expanding parameter count without a proportional increase in computation—on a single node with 8 high-end accelerators. To reduce time-to-first-token for long prompts, the team adds a shared cache layer that stores previously computed attention intermediates, hoping similar requests can reuse them.
The production data contradicted expectations. Median latency barely moved, while the slowest 1% of requests (p99) degraded from roughly 3.96 seconds to 17.3 seconds. The team ended up rolling the feature back entirely.
The conclusion is simple: reading from a cache has a cost, and that cost is routinely underestimated. Cache benefits are probabilistic—compute is saved only on a hit—while cache costs are deterministic: every request must look up, transfer, and synchronize. When that cost sits on the critical path and there is no queue isolation, the benefit gets averaged away and the cost gets amplified in the tail. The result is the classic pattern: no change at the median, an exploding long tail.
2. Understand the Workload First: How the Model Consumes Accelerators
Diagnosing the problem requires clarity on the data shapes and parallelism involved.
Compressed attention intermediates. Models in this class typically use low-rank latent attention (each token’s key/value information is compressed into a shared low-rank vector). The key property: regardless of how many attention heads exist, the intermediate data per token is fixed and small. Under FP8, 256 tokens are usually packed into a “block” of about 12.4MB, or roughly 48.4KB per token. This makes the cache look cheap—while making each block transfer non-trivial.
Context parallelism during prefill. Long-prompt prefill is compute-heavy, so the text is split and processed across multiple accelerators, which then exchange and synchronize intermediates over intra-node networking so every device holds the full context. The 8 ranks behave like meshed gears: if one slows down, the other seven must wait. A synchronization barrier turns a single-point hiccup into a global one.
Two-tier cache structure. The L1 cache lives in local memory—fast but capacity-limited. The L2 cache is a shared key-value store with large capacity, usable by multiple inference engine instances, much like a public library. The problem is precisely at L2: reads and writes from all instances converge on one place, and its backend typically has limited concurrency.
A fitting analogy: chain stores each keep their own books (L1), which is fast, but headquarters requires every transaction to be consolidated into a single master ledger (L2) maintained by one accountant. At peak, eight stores write simultaneously and all queue at the accountant’s desk. The master ledger is not slow at writing—it is slow at queuing.
3. Breaking Down the Bottleneck: Where the Latency Goes
Expanding a single cache access reveals at least six cost components:
- Key computation and lookup (long prefixes require per-block key derivation, consuming CPU and memory bandwidth);
- Queueing (limited backend concurrency causes requests to pile up);
- Serialization/deserialization and memory copies;
- Network round trips and bandwidth contention (competing with context-parallel synchronization traffic on the same link);
- Reordering and reassembly on a hit (non-contiguous blocks must be reorganized before downstream operators can consume them);
- Fallback on failure (a miss or timeout still requires full computation—the cost is paid for nothing).
The first five stack up on the critical path; the sixth amplifies the tail. With a synchronization barrier in place, the slowest rank’s cache access determines the group’s timing, pushing p99 sharply higher.
The coupling of “synchronization + cache” can be expressed with illustrative pseudocode:
# Anti-pattern: cache access serialized with collective communication on the critical pathfor chunk in prefill_chunks:kv = cache.get(chunk.key) # may queue, may time outif kv is None:kv = compute_attention(chunk) # miss: cost paid for nothingcache.set(chunk.key, kv) # write contends for the shared backend againall_gather(kv) # synchronization barrier: wait for the slowest rank
4. The Fix: Move the Cache Off the Critical Path
1) Asynchronous I/O and batch coalescing. Change hit-path reads to “check local first, prefetch remote asynchronously”; on a miss, proceed to compute immediately without blocking. Coalesce writes into batched submissions to reduce queueing events per unit of data.
# Fixed: prefetch runs in parallel with compute; write-back is batched and asyncprefetch = cache.async_get(chunk.key) # non-blockingkv = prefetch.result(timeout=0) or compute_attention(chunk)cache.async_put_batch(pending_writes) # coalesced writes, async flushall_gather(kv)
2) Admission control and timeout budgets. Set a strict time budget for cache access (single-digit milliseconds); on budget overrun, fall through to the compute path. Do not cache very large blocks or keys with very low hit rates—avoid paying the cost without getting the benefit.
3) Tiering and locality first. Keep reuse within the same rank or node whenever possible; cross-instance sharing should be a fallback only. Shard and replicate the shared backend to turn “one accountant” into “multiple service windows.”
4) Communication isolation. Separate cache traffic from context-parallel synchronization traffic at both the link and thread-resource level so they do not contend.
5) Observability first. At minimum, collect hit rate, lookup queue time, transfer time, fallback rate, and per-rank p99/p999, with an attribution dashboard. The rule of thumb: if the hit rate is not enough to cover the average access cost, turn the cache off—a threshold that can be decided directly from data.
5. Rollout Checklist
- Measure the baseline first: p50/p99/p999 and throughput without cache;
- Then measure pure access cost with “lookup only, no use” to establish a cost baseline;
- Use shadow traffic to evaluate real hit rates and block reuse distribution;
- Gradually enable async prefetch and batched write-back, watching the tail rather than the mean;
- Define an automatic fallback switch: degrade if p99 worsens beyond a threshold relative to baseline;
- Include the cache in capacity planning, with explicit concurrency limits and sharding strategy for the shared backend.
6. Conclusion
A cache is not a free lunch; it is a deterministic investment with probabilistic returns. For very large MoE and long-context inference, the correct optimization order is: quantify access cost first, then improve hit rate, and only then discuss capacity and sharing. Move the cache off the critical path and treat tail latency as the primary metric, and “shared caching” will finally live up to its name. Teams looking to build a similar architecture on Baidu AI Cloud can combine general capabilities such as container platforms, object storage, message queues, and monitoring/alerting to build an inference pipeline that is isolated, observable, and gradually releasable.
