When Caching is Slower Than Computation: Hidden Costs in LLM Inference
This article explores a counterintuitive incident where introducing a KV cache mechanism in a large-scale MoE model inference service unexpectedly caused a massive spike in p99 latency. It analyzes the root causes behind this degradation, including bandwidth contention from cache reads, insufficient hit rates, and cache-cost-oblivious schedulers. The article also provides actionable governance strategies, such as cost-based admission control, tiered storage, and decoupling data transfer from computation, emphasizing the necessity of evaluating cache optimizations using tail latency rather than average throughput.
When Caching is Slower Than Computation: Hidden Costs in LLM Inference
A Counterintuitive Failure Scene
A team providing online inference services for an ultra-large-scale Mixture of Experts (MoE) model encountered a textbook case of “optimization backfire.” The model was deployed on a single node with 8 high-end GPUs. To accelerate long-text scenarios, engineers introduced a shared cache layer to store attention intermediate results (i.e., KV data) computed by historical requests. The design intent was straightforward: when a new request shares a common prefix with a historical request, directly reuse the cached data to eliminate redundant attention computation.
However, the monitoring data post-launch left everyone dumbfounded:
- Median latency (p50) remained virtually unchanged;
- Tail latency (p99) skyrocketed from about 4 seconds to over 17 seconds, deteriorating by more than 4 times.
The engineers’ first reaction was to roll back. But rolling back only stops the bleeding; it is not the answer. The real question worth investigating is: Why would a theoretically foolproof caching mechanism drag the system into an abyss? This article takes this question as a starting point to systematically deconstruct the cost model of inference caching and provide actionable governance methods.
Establishing the Right Cost Intuition: Caching is Not Free
In traditional web services, “cache hit = saving money” is almost an iron law—reading from memory or Redis is far cheaper than querying a database. But the KV cache scenario in LLM inference has a fundamental difference: the cache does not store a few fields, but tensor data often measuring in tens of MBs or even GBs. Reading it itself consumes memory bandwidth, PCIe/NVLink bandwidth, and scheduling slots.
Take the scenario discussed in this article as an example. The model employs the MLA (Multi-head Latent Attention) mechanism, which compresses the key-value information of each token into a shared low-rank vector. This drastically reduces the amount of intermediate data that needs to be saved per token, independent of the number of attention heads. Under FP8 precision, every 256 tokens are packed into a data block of about 12MB, averaging roughly 48KB per token.
Sounds efficient? Let’s do the math: a common prefix of 16K tokens corresponds to about 64 blocks, nearly 800MB of cached data. If the cache is not in GPU memory but in host memory or even remote storage, reading this 800MB requires PCIe or network transfer. Estimating with the effective bandwidth of PCIe 4.0 x16 at about 25GB/s, the transfer alone takes over 30 milliseconds—and this does not account for queuing, DMA scheduling, memory allocation, and kernel launch overhead. Meanwhile, “recomputing the attention for this prefix” might only take tens of milliseconds when GPU compute resources are abundant.
Conclusion 1: Whether cache reuse is worthwhile depends on the dynamic price ratio between the “read cost” and the “recomputation cost,” rather than being a static architectural decision.
Three Root Causes of Tail Latency Explosion
Returning to the scene of the p99 deterioration. An unchanged median combined with a ruined tail is a classic symptom of “resource contention.” After a layer-by-layer investigation, the problem converged to three overlapping root causes:
1. Cache Reads Preempt Compute Resources
When a large number of requests hit the shared cache, the system needs to move massive amounts of KV blocks from the storage side to the GPU. This data movement contends with ongoing decoding computations for the same data pathways and memory bandwidth. Every step in the decode phase is memory-intensive; once the bandwidth is heavily occupied by cache loading, the decoding speed of online requests is dragged down. Requests that miss the cache should have been unaffected, but they slow down because their bandwidth is “stolen” by neighbors. This is the classic mechanism behind a soaring p99 while p50 remains static: most requests fall on the normal path, while a few land at the moments of the most severe contention.
2. Insufficient Hit Rate, Costs Still Paid
The upper limit of cache benefits is determined by the hit rate. Long-text scenarios might seem to offer many opportunities for prefix reuse, but in real traffic, user request prefixes often have subtle differences in conversation history, system prompt concatenation methods, and truncation strategies, leading to exact-match prefix lengths far shorter than expected. This leads to the worst-case scenario: spending bandwidth to query the cache and move data, only to reuse a very short segment, while the recomputation portion still exists—the cost of reading the cache is a pure loss.
3. Schedulers Are Oblivious to Cache Costs
Most inference schedulers make batching decisions based on “request length” or “queue order” and do not perceive “how much additional cache data this request needs to load.” When a large cache loading task and a long decoding task are scheduled into the same scheduling window, tail latency is further amplified.
Governance Approach: Make Caching Account for Its Costs
Once the problem is clearly identified, the optimization direction becomes clear. The core principle is: Treat cache reads as a resource operation with explicit overhead, managed uniformly within a cost model.
Admission Control: Calculate Before Reading Cache
Before deciding whether to reuse a cache, perform a lightweight estimation first:
recompute_cost ≈ prefix_len × per_token_compute_costfetch_cost ≈ kv_bytes / effective_bandwidth+ transfer_queue_delayuse_cache = fetch_cost < recompute_cost × margin
Here, margin is a safety coefficient (e.g., 0.8) used to absorb bandwidth fluctuations. The cache path is only taken if reading is significantly cheaper; otherwise, it recomputes directly. This judgment can be dynamically adjusted based on the bandwidth utilization observed in real-time by the scheduler—using the cache more when the system is idle, and decisively recomputing when it is busy.
Tiered Storage and Data Locality
Tier the KV cache by heat: the hottest common prefixes (like fixed system prompts) should reside permanently in GPU memory to eliminate transfer costs entirely; the next hottest go to host memory; and only cold data goes to remote storage. Simultaneously, give the scheduler “cache affinity”—routing requests that share the same prefix to nodes that already hold that cache, avoiding cross-node transfers.
Decoupling Transfer from Computation
Cache loading should use independent asynchronous data pathways and priority queues, isolating bandwidth quotas from decoding computations. For instance, limit cache prefetching to a maximum of 20% of the effective bandwidth to guarantee a minimum bandwidth floor for online decoding. This is essentially “rate-limiting” cache reads in exchange for controllable tail latency.
p99-Centric Acceptance Criteria
The deployment evaluation of any caching strategy should treat p99/p999 latency as a veto item, rather than just looking at average throughput. Caching is a classic optimization with “positive average returns but concentrated tail risks”; evaluating it solely by mean values will inevitably lead to misjudgments.
Conclusion
The greatest value of this case study is breaking a deeply rooted assumption: “hitting a cache is always faster than recomputing.” In LLM inference systems where data volumes are measured in hundreds of MBs and transfer pathways share resources with computation, reading from the cache is an I/O operation with explicit costs. Whether it is worthwhile must be adjudicated by a real-time cost model, not by arrows on an architecture diagram.
For teams building inference services—whether on self-built clusters or utilizing cloud providers’ container platforms, object storage, and monitoring/alerting systems to construct this architecture—the methodology is universal: first quantify the read cost of the cache, then establish admission and rate-limiting mechanisms, and finally verify with tail latency rather than averages. Caching can be fast, but the prerequisite is that you must first calculate exactly how slow it is.
