When Caching Is Slower Than Computing: A Postmortem of a Large Model Inference Performance Incident
In distributed inference systems, caching is widely treated as a performance silver bullet: a cache hit means skipping redundant computation and, in theory, significantly lower latency. This article walks through a real production incident where enabling a shared KV cache for large model inference made tail latency dramatically worse — p99 latency jumped from 3.96s to 17.3s despite a healthy cache hit rate. We break down the root causes (synchronization barriers, queuing effects, and lost data locality) and the three-step optimization that brought p99 latency down to 1.90s, along with the key lessons for designing caching in large-scale inference systems.
When Caching Is Slower Than Computing: A Postmortem of a Large Model Inference Performance Incident
In distributed inference systems, caching is widely regarded as a “silver bullet” for performance: a cache hit means skipping redundant computation, which should translate into significantly lower latency. However, a real production incident delivered a counterintuitive conclusion — in certain scenarios, reading from the cache costs more than recomputing from scratch. This article walks through the full investigation and resolution of this performance incident, exposing the cost traps of caching systems that are easy to underestimate in large-scale model inference.
The Incident: Cache Enabled, Performance Degraded
While optimizing inference for an extremely large Mixture-of-Experts (MoE) model, an engineering team added a shared cache layer to accelerate long-context processing. The model ran on a single node with 8 flagship inference GPUs and used Multi-head Latent Attention (MLA) to reduce the memory footprint of the KV cache. To speed up long text handling, the team stored attention computation results from historical requests in a standalone key-value store, expecting to reuse them directly when similar requests arrived instead of recomputing.
The reality fell far short of expectations. After enabling the cache, the overall median latency barely changed — meaning most requests did not benefit from it. But the slowest 1% of requests (p99 latency) degraded sharply from 3.96 seconds to 17.3 seconds, a more than 4x regression. Even more confusingly, an initial check showed the cache hit rate was not low. Faced with this anomaly, the team eventually rolled back the feature, but the underlying question remained open: why was latency higher when the cache was clearly hitting?
Root Cause Analysis: The Hidden Costs of Cache Reads
Understanding this counterintuitive result requires a closer look at the system’s architecture.
A Hard Constraint: GPU Synchronization Barriers
The inference system used context parallelism for prefill to handle long inputs: an input sequence is split into multiple chunks and distributed across the node’s 8 GPUs for parallel processing. Once processing finishes, each GPU must communicate its intermediate results to all the others over the network to assemble the full context. This is a classic all-gather pattern — the 8 GPUs act as one unit, with every step waiting for the slowest participant to be ready. As soon as one GPU stalls, the other seven all block and wait.
The Bottleneck in the Two-Level Cache Architecture
The cache system was designed as a two-level structure:
- L1 cache: Resides in the GPU node’s local VRAM or memory, with access latency down to the microsecond level, but limited capacity — it can only cover a small number of high-frequency requests.
- L2 cache: Built on a standalone key-value store, with large capacity and shared across all inference engines — effectively a central knowledge base.
The problem lay precisely in the L2 layer. On a cache miss, the request goes straight down the compute path; on a cache hit, the request instead incurs an extra network round trip — the GPU node issues a remote read request and waits for the key-value store to return the result. This additional latency depends on network bandwidth, server-side load, and data structure complexity, and in extreme cases can reach milliseconds or even tens of milliseconds.
To reduce storage overhead, the system also introduced block-level caching: every 256 tokens of attention data are packed into a single cache block. This means a cache read targets not a single token but an entire block, pulling far more data than actually needed — further amplifying I/O costs.
Quantifying the Problem: Why Hits Are Slower Than Misses
Through system observation and microbenchmarks, the engineers eventually pinpointed the core issue: the marginal cost of a cache read is not constant — it depends on three key variables.
1. Cache Hit Rates Misaligned with Request Distribution
Some requests had high hit rates, but the blocks they hit were all cold ones — scattered across different physical regions of the store, forcing multi-hop access on every read. Conversely, requests that missed the cache went down the compute path, and the attention computation itself ran extremely fast on the current GPUs, especially during batched prefill, where GPU compute capacity could be fully saturated.
2. Queuing Effects from Server-Side Contention
The key-value store is a shared resource — all inference engines use the same instance. When multiple GPU nodes issued read requests simultaneously, the store’s thread pool saturated quickly and requests began queuing. At that point, cache reads were no longer a “fast path” but a “slow path” — the end-to-end latency of a single read equals queue wait time plus actual transfer time.
Queueing theory helps explain this: when server utilization approaches 1, average queue wait time grows super-linearly. With 8 GPUs queued behind the same synchronization barrier waiting for the store to respond, any single GPU’s blocked cache read stalls the entire node.
3. Cascade Amplification from Lost Locality
In long-context processing, attention data has natural locality: data for adjacent tokens is accessed consecutively. But in the two-level architecture, the L1 cache’s limited capacity forced much of this nearby data to be evicted to L2. When a GPU needed to read a contiguous stretch of context, it had to repeatedly reach through to L2, with a full network round trip each time.
The Fix: From “Cache Everything” to Tiered Decision-Making
Once the root causes were identified, the engineers designed a three-layer optimization, each step addressing one core insight.
Step 1: Bound Concurrency to Eliminate Queuing
First, the team introduced concurrency control for cache reads — limiting the number of outstanding read requests a single GPU node could issue at once. When concurrency exceeded the threshold, subsequent requests were no longer sent to L2 and instead went directly to the compute path. This change flattened the request peak at the store and eliminated the queuing amplification effect.
The core idea: a cache read should not be a “best effort” optimization attempt, but a bounded resource. Once the estimated latency of the read path exceeds the direct compute latency, the system should immediately give up on the cache.
Step 2: Locality-Aware Admission Policy
Second, the team optimized the cache write policy. Not every computation result is worth caching. The system introduced a simple cost model: a block is written to L2 only when its compute cost > cache read cost + storage cost. For hot blocks accessed frequently, even if compute cost is low, their cache priority can be raised thanks to high hit rates; for cold blocks, the probability of caching is significantly reduced to avoid polluting storage.
Step 3: Asynchronous Prefetching and L1 Expansion
Finally, leveraging the sequential nature of the context window, the team implemented asynchronous prefetching: while the GPU processes the current token block, a background thread reads the next likely-needed attention data from L2 into L1 ahead of time. This hides the expensive network latency inside computation rather than exposing it at the synchronization barrier.
The team also adjusted the L1 eviction policy — moving away from plain LRU to a scheme that, based on context window access patterns, proactively retains blocks adjacent to the current processing position.
The Results: p99 Latency Down from 17.3s to 1.90s
After these optimizations, overall system performance looked like this:
| Metric | Before (no cache) | Cache enabled (unoptimized) | Cache enabled (optimized) |
|---|---|---|---|
| Median latency | 1.2s | 1.2s | 0.9s |
| p99 latency | 3.96s | 17.3s | 1.90s |
| Average cache hit rate | - | ~70% | ~65% |
p99 latency dropped from 17.3 seconds to 1.90 seconds — even better than the 3.96 seconds without caching. Median latency also improved from 1.2 seconds to 0.9 seconds, showing that the cache’s benefits were no longer offset by tail latency degradation.
Key Takeaway: Finding the Pareto Boundary of Caching
The deepest lesson from this exercise: the benefits of caching are not one-directional. In large distributed systems, adding a cache inevitably brings extra read/write latency, consistency, and capacity management costs. When the computation itself is highly parallel and fast, cache reads can become the slowest part of the system — not because caching is useless, but because its usage scenarios and policies need to be redesigned.
For inference system designers, several points deserve deep consideration:
- Decompose the latency budget: Before introducing any cache, precisely measure the latency distribution of both the “compute path” and the “cache path” — don’t decide on gut feeling.
- Be aware of synchronization boundaries: In systems with all-gather barriers, any single slow operation gets amplified across the whole system. Cache operations must have timeouts and circuit breakers.
- Put data locality at the center: Don’t judge a cache by hit rate alone — consider whether the hit blocks have temporal and spatial locality. Scattered hits can be worse than concentrated misses.
Looking further ahead, generic components in mainstream cloud infrastructure — object storage, distributed caches — face similar trade-offs in large model inference scenarios. Following the principles of “tiered storage, bounded concurrency, and locality awareness” when designing inference services helps strike a better balance between scalability and performance. In large model inference services, such as those offered by Baidu AI Cloud, these optimization ideas have been built into the underlying elastic scheduling and cache governance components to maintain stable tail latency under multi-tenant, high-concurrency conditions. This demonstrates that fine-grained cache policy design is a key factor determining the performance ceiling of large-scale AI infrastructure — and a topic well worth every systems engineer’s attention.
