When Caching Is Slower Than Computing: The Hidden Cost of Inference Acceleration

    This article examines a counterintuitive production incident where adding a shared cache to a Mixture-of-Experts (MoE) inference service left median latency unchanged but pushed p99 latency from 3.96 seconds to 17.3 seconds. It explains why reading a cache is not free, how low-rank compressed attention limits caching gains, and how to build an explicit cost model that accounts for transfer and contention overhead. Readers will learn why tail latency explodes and how to fix it through asynchronous non-blocking lookups, capacity and concurrency governance, locality-aware storage, and p99-based acceptance criteria.

    1. A Counterintuitive Production Incident

    A team was serving inference for a very large Mixture-of-Experts (MoE) model—an architecture that assigns different sub-networks to different tokens or tasks, expanding parameter count without a proportional increase in computation. The hardware was a single node with 8 high-bandwidth accelerator cards, and the workload was dominated by long-text requests. To avoid redundant computation, the team added a shared cache in front of the attention layers to store previously computed attention intermediate results, expecting similar requests to hit the cache and skip recomputation.

    The post-launch numbers were awkward: median latency barely moved, but the slowest 1% of requests (p99) jumped from 3.96 seconds to 17.3 seconds. The team’s first instinct was a full rollback.

    The core takeaway is a single sentence: reading a cache has a cost, and that cost has long been severely underestimated. A cache is not a free accelerator; it is a trade of storage and data movement for computation. When the two sides of the trade are not equivalent, the cache degrades from an accelerator into an amplifier—amplifying tail latency.

    2. Understand the Workload First: Attention Determines Cache Value

    To judge whether caching is worthwhile, you must first understand how the model consumes GPU memory and bandwidth. The model in this case uses a low-rank compressed attention mechanism (which compresses each token’s key-value information into a shared low-dimensional vector). Its advantage is that the intermediate data stored per token is fixed and small, regardless of how many attention heads there are.

    This leads to an easily overlooked corollary: when the per-token cache footprint is tiny, recomputing it is also cheap. The upper bound on caching gains is therefore very thin, while the fixed overhead of the read path (lookup, validation, transfer, synchronization) remains fully intact. The benefit is small and variable; the cost is constant and rigid. By design, this equation is hard to balance.

    3. Cost Model: Break “Reading a Cache” into Measurable Terms

    Stop making decisions based on the intuition that “a cache is always faster.” Instead, build an explicit cost model for the cache:

    1. Benefit = P(hit) × T_recompute_saved
    2. Cost = P(hit) × (T_lookup + T_verify + T_transfer + T_sync)
    3. + P(miss) × (T_lookup + T_miss_penalty)
    4. + T_memory_pressure + T_contention

    The key is that T_transfer and T_contention are often left out of the calculation:

    • Transfer cost: The attention intermediate results are already in GPU memory, but a cache hit often still requires a cross-layer or cross-device copy. Bandwidth gets squeezed, and the compute stream is actually slowed down.
    • Contention cost: A shared cache is typically a global structure. Under high concurrency, locks, reference counting, and eviction policies serialize access and create queuing. This is the classic signature of worsening p99 with an unchanged median—the cache does not slow everyone down; it just makes things worse for the requests already stuck at the tail.

    4. Why p99 Explodes: The Tail Latency Amplification Mechanism

    An unchanged median means the average path is not broken; the problem is concentrated in the tail. There are usually three reasons:

    1. Head-of-line blocking: A cache operation performing a large copy or eviction blocks all subsequent requests that need the same resource.
    2. Memory pressure feedback: The cache occupies GPU memory that could otherwise be used for computation, triggering more frequent swapping, with long requests hit first.
    3. Hit validation amplification: Similar requests are not necessarily reusable. Incorrect similarity judgments cause invalid hits—data is moved but no computation is saved.

    5. Remediation: Making the Cache Actually Faster

    1. Do the Math Before Launch

    Use the cost model above to estimate the break-even point offline. Enable the cache only when Benefit > Cost and tail latency is under control. Low-rank compressed workloads often fail this condition, so shut the cache off or narrow its scope decisively.

    2. Make It Asynchronous and Non-Blocking

    Move lookup, validation, and transfer out of the critical path. Use asynchronous prefetching with a policy of “use on hit, fall back to recomputation on miss,” so no cache operation blocks the compute stream.

    1. # Example: non-blocking hit check, fall back immediately on miss, never wait
    2. def try_cached(key):
    3. entry = cache.peek(key) # non-blocking read
    4. if entry and entry.valid(): # lightweight validation
    5. return entry.value
    6. return None # immediate fallback to recomputation
    7. value = try_cached(k) or recompute(k)

    3. Capacity and Concurrency Governance

    Set a strict GPU memory limit and per-request quota for the cache. Use sharding to reduce lock contention, combined with admission control, to prevent a few long requests from consuming all resources.

    4. Tiering and Locality

    Prefer on-chip or near-core storage close to the compute units to reduce cross-device copies. Reserve the remote shared cache for scenarios with genuinely high reuse and large payloads.

    5. Use Tail Latency as the Acceptance Metric

    The launch gate should not look at averages, but at p99/p999 and timeout rates. Pair this with monitoring, alerting, and logging to continuously track hit rate, transfer time, and queue depth.

    6. Conclusion

    More caching is not always better, and the cost of reading a cache must be explicitly modeled and included in acceptance criteria. When optimizing inference on platforms such as Baidu AI Cloud, remember: when per-token intermediate results are tiny and reuse gains are limited, the safest “acceleration” is sometimes to turn the cache off. Decide with a cost model, govern tail latency with asynchronous and sharded designs, and use p99 as the yardstick—only then can a “cache that drags you down” become a component that genuinely speeds things up.