LLM Inference Performance Tuning: When Cache Reads Cost More Than Recompute
This article reviews a performance tuning effort for an ultra-large mixture-of-experts inference service in which enabling a shared attention cache left median latency unchanged but raised p99 latency from about 4 seconds to more than 17 seconds. It explains why cache hits can be slower than recomputation because of HBM bandwidth pressure, fragmented physical addresses, decode overhead, and kernel launch costs, then presents four mitigation approaches and lessons about tail latency, cache cost, and data movement in GPU inference.
In high-performance computing and large-scale machine learning inference, the intuition that “one more cache layer means one more layer of performance” is almost universal. After all, a cache exists to avoid repeated computation by trading space for time. But when a cache mechanism in an inference system instead makes the slowest requests more than four times slower, that common sense becomes questionable. A performance tuning effort for an ultra-large mixture-of-experts inference service revealed the truth behind this counterintuitive outcome: reading from a cache has a cost, and under specific hardware and data-scale conditions, that cost can be far higher than recomputing.
The Symptom: Cache Made Performance Worse, Not Better
In an inference service built for an ultra-large mixture-of-experts model, the service nodes consisted of multiple flagship GPUs and supported a sparse-activation model with an extremely large parameter count. The model used multi-head latent attention, an efficient attention implementation that compresses key-value states into low-rank vectors. The core advantage of this design is that regardless of how many attention heads the model has, the amount of intermediate data that must be persisted per token is significantly compressed, making long-context inference feasible.
In actual deployment, the engineering team introduced a shared cache to make long-text requests respond faster. The cache was designed to store previously computed attention intermediate results. The expectation was that when a new request overlapped with historical requests—for example, by sharing a system prompt or document prefix—the system could directly reuse cached data and avoid recomputing the expensive attention scores from scratch.
The results of a canary load test were striking: with the cache enabled, median latency barely changed, but the latency of the slowest 1% of requests deteriorated sharply from about 4 seconds to more than 17 seconds. That number was completely unacceptable, and the feature was ultimately rolled back entirely.
The team initially suspected that a low cache hit rate made the cache useless, but monitoring data ruled that out—the cache hit rate was normal. The real cause lay in the details of hardware behavior and kernel execution.
Root Cause Analysis: Why the Cache Was Slower
To understand why even cache hits could cripple performance, it is necessary to reexamine how GPUs process data. In modern GPU architectures, there is a huge asymmetry between the speed of compute units and the speed of data movement between compute units and VRAM. What is often called a “compute bottleneck” can be replaced by a “memory bandwidth bottleneck” in specific scenarios, and cache reads are exactly the kind of operation that consumes high bandwidth.
1. Spatial Characteristics and Bandwidth Pressure of Attention Cache
In this scenario, each attention block occupied about 12.4 MB of VRAM at FP8 precision, averaging about 48.4 KB per token. When the inference service processed many requests concurrently, the number of token blocks resident in the shared cache became extremely large. GPU L2 cache is usually only tens of megabytes, so it cannot possibly hold intermediate results at this scale. This meant that every attempt to read data from the shared cache was in practice a physical transfer from HBM to the compute cores.
HBM bandwidth is wide, but it still cannot sustain frequent large-block reads that span the entire VRAM address space. More importantly, computing an attention block only requires executing a number of matrix multiplications and normalization operations, which take very little time on a GPU. In contrast, moving 12.4 MB of data back from VRAM can take longer than recomputing those matrix operations. The original goal of “avoiding computation” was therefore completely offset by the time cost of “reading data.”
2. Physical Cost of VRAM Reads and Discontiguous Addresses
Another easily overlooked factor is the continuity of physical addresses. Shared caches are often implemented with hash tables or index structures to manage token blocks. When a historical token block needs to be read, the system must first query the index structure, locate the physical address, and then initiate the read. The latency introduced by this indirect jump is negligible for a single request, but under high concurrency, when many threads perform queries and address jumps simultaneously, it significantly increases scheduling pressure on the memory controller.
In addition, if the shared cache is stored in compressed or encoded form to save VRAM, reading it requires extra decoding overhead. Decompression is inherently compute-intensive work, yet it is placed on a latency-sensitive path. The result is that a cache hit does not save time; instead, it moves computation cost onto the critical path of the request.
3. Kernel Launch and Context-Switching Overhead
On a GPU, any cache read passes through at least two kernel stages: the first kernel extracts data from the cache and writes it to shared memory or registers; the second kernel performs the actual fused attention computation. Synchronization and dependencies between these two kernels introduce considerable kernel launch latency and thread-block scheduling overhead. When the request batch size is small, the relative cost of this kernel switching is even higher. The sharp rise in p99 latency—the slowest 1% of requests—occurred precisely because many fragmented cache-read operations were inserted into the scheduling sequence of those requests, lengthening the overall execution pipeline.
Optimization Paths: Putting the Cache Back in the Right Place
Once the problem was clear, the direction for solutions became clear as well. The core logic is simple: a cache must be fast enough not to become a bottleneck; if it cannot be fast enough, then reduce the frequency of cache reads or inline the cache into the computation process.
Option 1: Two-Level Cache and Tiered Storage
Instead of a single large shared cache, introduce a two-level structure. The first level is a relatively small but physically contiguous hot-data region in high-bandwidth memory, dedicated to recently and frequently accessed token blocks. The second level is a larger cold-data store for low-frequency blocks. Reads first access the hot region; when the hit rate is insufficient, the system batch-prefetches from cold data. This structure makes full use of GPU locality and reduces random reads across address spaces.
Option 2: Fused Kernel for Computation and Cache
A further optimization is to design a cache-aware fused kernel. This kernel does not treat “reading the cache” and “executing attention” as two independent stages. Instead, it embeds cache-read instructions directly into the attention computation instruction stream, letting some threads handle data movement while other threads execute matrix operations, using GPU stream-processor parallelism to overlap movement time with computation time. In this way, cache-read overhead is effectively hidden, and p99 latency is no longer stretched by cache operations.
Option 3: Replacement-Style Cache Prefetching
In actual deployment, cache-prefetch instructions can also be introduced at the scheduling layer. When the system identifies that a request will reuse historical context, it does not wait until the execution stage to read the cache. Instead, at the tail end of the previous request, it moves the required data into on-chip cache in advance. This requires the scheduler to have some predictive capability, but compared with simply reading during the execution stage, the latency reduction can be very significant.
Option 4: Fine-Grained Caching and Attention Scaling
Given the mathematical characteristics of MLA, the storage granularity of the cache can also be changed. If attention data originally stored by “block” is further subdivided, and only the parts related to static prompts are cached while dynamically generated parts are always computed in real time, the amount of cached data can be greatly reduced, lowering the movement cost of each read. Although some hit rate is lost, end-to-end latency improves overall.
Engineering Lessons: The “Cost Conservation” of Performance Design
The most important lesson from this tuning effort is not to unconditionally believe the assumption that “reading a cache is faster than computing.” In hardware architectures where compute units are extremely efficient while the storage hierarchy lags relatively behind, computation is sometimes the cheapest operation. What truly determines system performance is always fine-grained control over data-movement paths.
When designing any cache system, three questions should be answered first: How long does one cache read take? How long does it take to recompute the same data? Can the cache hit rate cover the extra overhead introduced by the read cost? A cache is meaningful only when read time is significantly less than compute time and the benefit from the hit rate is enough to offset the latency jitter caused by reads.
In addition, tail-latency control cannot be ignored. In distributed inference systems, p99 latency often determines the upper bound of user experience. Many optimizations that look excellent from an “average performance” perspective may be full of holes from a tail-latency perspective. This is especially true for cache mechanisms, which naturally introduce uncertainty—requests that hit the cache and requests that miss it follow vastly different execution paths. If the difference is too large, the scheduler will struggle to maintain a stable response rhythm.
Conclusion
From “add a cache to speed things up” to “remove the cache” and then to “redesign the cache,” this winding tuning path was essentially a game against the laws of hardware physics. VRAM bandwidth is limited, data movement has a cost, and compute can actually be cheap at specific precisions. A truly high-performance inference system is not a simple stack of cache and computation, but a carefully designed coupling between storage hierarchy and compute pipeline based on data-flow characteristics.
The next time you encounter the difficult problem of “the cache hit but performance got slower,” it may help to step outside the cache-is-always-beneficial assumption and reexamine every detail of the memory-access path. After all, the most basic rule of system optimization has always been this: there is no absolute optimal strategy, only the deepest understanding of and adaptation to the behavior of the current hardware.
