When Caching Becomes the Bottleneck: Rethinking Cache Overhead in LLM Inference

    This article examines a production case in which a two-tier cache for large language model inference left median latency unchanged but drove p99 latency from 3.96 seconds to 17.3 seconds. It analyzes the causes—network amplification, synchronization stalls, and an unfavorable hit-rate/compute tradeoff—and describes four optimizations that reduced p99 latency to 3.1 seconds, better than the no-cache baseline.

    Introduction: Caching Does Not Always Accelerate

    In large-scale distributed system design, caching is often the first choice for performance optimization. However, in AI inference services that pursue extremely low latency, the extra overhead introduced by caching can far exceed expectations and even overturn the common assumption that “caching necessarily accelerates.” Starting from a real production case, this article explores how a system can move from “acceleration” to “drag” when cache reads cost more than recomputation, and it presents reusable optimization ideas.

    Background: Storage and Parallel Structure in LLM Inference

    Modern LLM inference often uses a mixture-of-experts architecture, splitting model parameters by task type so that multiple expert subnetworks work together. To support very long contexts, inference engines commonly use a multi-head latent attention mechanism. This mechanism compresses the key-value information of each token into a shared low-rank vector, significantly reducing the storage of intermediate states. For example, at FP8 precision, every 256 tokens are packed into a “block,” and a single block requires only about 12.4 MB, or 48.4 KB per token on average. This compact representation makes it possible to cache long-text history.

    At the same time, to process long sequences, the system uses prefill context parallelism: a long text is split into multiple shards, distributed across multiple GPUs for parallel prefill, and then the results of all shards are synchronized over a high-speed network so that every GPU has the complete preceding context. Within an 8-GPU node, the eight GPUs are tightly coupled like gears, and any communication delay in one step causes the whole group to wait.

    Problem Reproduction: Latency Spikes After Caching Is Enabled

    To accelerate repeated computation for similar requests, an inference service team designed a two-level cache system. The first level is stored in local GPU memory (called L1), with extremely fast reads but limited capacity. The second level is stored in a distributed key-value database (called L2), with large capacity and shared by all inference engines. In theory, for repeated long-text prefixes, the system can directly reuse attention computation results from L2 and avoid recomputation time.

    The measured data, however, was surprising: after caching was enabled, the overall median latency of the system was almost unchanged, but the slowest 1% of requests (p99 latency) jumped from 3.96 seconds to 17.3 seconds, an increase of more than four times. The team even rolled back the entire caching feature at one point. The root cause was not missing data, but that cache reads themselves became a non-negligible bottleneck under the specific architecture.

    Root Cause Analysis: The Underestimated Cost of Cache Reads

    1. Network Amplification from Multi-Level Caching

    The L2 cache resides in a distributed database and is shared by all inference engines. When a request needs to hit L2, it must first query the database over the network and then wait for the data to return. In an 8-GPU node, each request may need to pull multiple blocks from L2 (each block is 12.4 MB). For long text, a single inference may pull hundreds of blocks, and the accumulated data volume can reach several GB.

    More critically, because all GPUs work at the same time, they issue query requests to L2 simultaneously. This instantly congests the database’s IO queue, and each request must wait for other requests to complete. This many-to-one traffic aggregation effect multiplies the latency of an individual request. It is like eight shops submitting ledgers to a single head accountant at the same time; the accountant can process them only sequentially.

    2. Network Synchronization and Lock Waiting

    Under prefill context parallelism, after each GPU finishes processing its own shard, it must exchange data through collective communication operations such as AllReduce. If one GPU blocks while waiting for L2 cache data to return, the entire communication group is held up. Worse, the cache database itself may also have lock contention or transaction consistency checks. These additional costs occur beyond network transmission and further lengthen a single read.

    3. The Tradeoff Between Cache Hit Rate and Computation Cost

    Caching does not benefit all requests. When the shared prefix between a new request and a historical request is short, computation is still required for the remaining part after a hit, so the time saved is limited. The overhead of cache querying and transfer is fixed, which can make the net benefit negative. After rolling back, the team performed a quantitative analysis: the average time to hit a block was about 120 ms, while recomputing that block took only about 40 ms—cache reads were three times slower than computation. This overturns the traditional belief that “reading cache is always faster than computing.”

    Optimization Plan: Making Caching a True Accelerator

    To address these problems, the team rebuilt the cache design across four dimensions and ultimately reduced p99 latency to 3.1 seconds, which was even better than when caching was disabled.

    1. Capacity-Aware Prefetch Queue

    Instead of letting every GPU access L2 directly and in parallel, the team introduced an asynchronous prefetch module. Based on the context prefix of the current request, the module predicts which blocks may be needed in advance and pulls them in batches into a shared L1 region. Although L1 capacity is limited, an intelligent eviction algorithm (such as an LRU variant) keeps hot data resident. At the same time, prefetch requests are merged into larger block groups to reduce network round trips, increasing the throughput of each read batch by an order of magnitude.

    2. Tiered Load Balancing and Local Caching

    The originally monolithic L2 database was split into multiple shards, each bound to a specific GPU group. In this way, different GPU groups mainly access their own “local cache” and turn to the global cache only on a local miss. This design reduces the number of contenders for parallel access from eight to one or two, greatly lowering the probability of queue congestion. Measurements showed that the local hit rate reached 70%, so most requests did not need to touch the global database.

    3. Adaptive Decision Between Computation and Reading

    A lightweight “cost estimator” was added to the inference engine. It estimates the time to read the current block from cache (based on network bandwidth and queue length) and the time to recompute it (based on GPU utilization). When read cost is higher than computation cost, the system actively chooses recomputation. The decision granularity is at the “block” level, which precisely filters out low-value cache requests. The implementation requires only a simple probabilistic model but brings a significant throughput improvement.

    4. Asynchronous Writes and Version Validation

    To address possible write contention, the system switched to an asynchronous “write cache first, confirm later” mode. Inference results are returned to the client first, and shared blocks are written to L2 asynchronously in the background. At the same time, version-number validation ensures that read data is not incorrect due to staleness. This removes blocking of the read path by the write path and lets the database always prioritize read requests.

    Conclusion and Takeaways

    The core lesson of this case is that the performance model of a cache system must include all overheads—network transmission, queue waiting, lock contention, consistency validation, and so on. In distributed, multi-GPU parallel AI inference scenarios, these overheads can far exceed the computation saved. When designing a cache, focus not only on hit rate but also on the ratio between “computation time saved per cache hit” and “extra latency introduced per cache access.” Only when the cache access path is sufficiently light and fast can the original goal of “trading space for time” be truly achieved. For developers, the recommendation is to benchmark on the target hardware before introducing any cache layer, simulate the latency distribution of cache IO under concurrent access, and design an adaptive fallback strategy. After all, when caching is slower than computation, removing the cache can itself be an optimization.