When Caching Slows Down LLM Inference: Understanding P99 Latency Degradation

    This article examines why adding a shared cache to a large MoE model inference pipeline left median latency flat while pushing P99 latency from roughly 3.96 seconds to 17.3 seconds. It explains the four mechanisms behind the regression—queueing amplification near saturation, memory bandwidth contention, cache hit rate and pollution limits, and disrupted continuous batching—and closes with a practical optimization checklist covering cache admission checks, sharding and tiering, smarter eviction and prefetching, and tail-focused load testing.

    1. What Happened: Flat Median, Exploding Tail

    A team serving inference for a very large mixture-of-experts (MoE) model ran the model on a single node with 8 high-performance accelerator cards. To speed up long-context processing, they added a shared cache layer in front of the inference path to store previously computed attention-related tensors, hoping to reuse them when similar requests arrived and skip redundant computation.

    The observed result was the opposite of what they expected. With the cache enabled, median request latency stayed roughly flat, but the slowest 1% of requests (P99) climbed from about 3.96 seconds to 17.3 seconds—roughly a 4x increase in tail latency. The team initially rolled the feature back entirely.

    The key takeaway is not that “the cache was useless,” but that the cache changed the system’s queueing and resource-occupancy structure. The median is determined by the majority of requests that hit well and take a short path, while P99 is determined by the minority that encounter resource contention, cache misses, swap-in/swap-out, or batch reshuffling. The cache shifted part of the cost from “computation” to “movement and scheduling,” and movement plus scheduling produce long tails much more easily under high concurrency.

    2. Why a Cache Can Be Slower Than Computation: Four Mechanisms

    2.1 Queueing Theory: Latency Amplifies Nonlinearly as Utilization Approaches 1

    Inference serving is fundamentally a queueing system. Let system utilization be ρ; under a simplified M/M/1 approximation, average wait time is proportional to 1/(1-ρ). The extra copying, index lookups, and lock contention introduced by the cache raise per-request service time and push ρ closer to 1. At that point average latency may rise only slightly, while tail latency is amplified sharply. This explains the typical shape of “flat median, exploding P99.”

    2.2 Memory and Bandwidth: The Cache Trades Bandwidth for Compute

    Attention caches typically consume large amounts of GPU memory and perform large tensor copies on a hit. When memory approaches its limit, the cache squeezes the space that dynamically growing runtime structures such as KV need, triggering more frequent eviction and reallocation; the copies themselves consume memory bandwidth and compete with computation for the same physical resource. If the hit rate is insufficient, the system pays a high bandwidth cost for a small amount of saved computation, and the net benefit is negative.

    2.3 Hit Rate and Pollution: The Boundary Conditions of a Shared Cache

    The benefit of a shared cache depends heavily on request similarity and the reuse window. In long-context scenarios, similar prefixes with diverging middles cause the reusable fraction to drop sharply; in multi-tenant mixed workloads, caches from different businesses pollute each other and the hit rate falls further. Requests that miss not only get no speedup but also carry the extra cost of lookup and fallback.

    2.4 Batching and Scheduling: The Cache Disrupts Dynamic Batching

    High-performance inference relies on continuous batching to raise throughput. Cache hits change the execution-time distribution of individual requests, making batch composition less stable and producing head-of-line blocking where the batch waits for its slowest member; meanwhile cache swap-in/swap-out competes with batch assembly for the same execution queue, stretching the tail further.

    3. An Actionable Optimization Checklist

    3.1 First Decide Whether to Cache at All

    Use offline replay to estimate the potential hit rate and reusable fraction, and enable the cache only when the expected saved compute time clearly exceeds the copy and scheduling cost. The following pseudocode shows an admission check:

    1. # Illustrative: benefit check for cache admission
    2. def should_cache(req, cache_stats):
    3. hit_prob = cache_stats.estimated_hit_rate(req.prefix_signature)
    4. save_time = hit_prob * req.estimated_prefill_cost
    5. overhead = cache_stats.copy_cost + cache_stats.lookup_cost
    6. return save_time > overhead * SAFETY_FACTOR # safety factor avoids borderline flapping

    3.2 Sharding and Tiering: Reduce Contention on Any Single Cache Block

    Shard the cache by business or tenant to avoid cross-pollution, and use a tiered structure that keeps hot data in a low-latency tier while pushing cold data down. After sharding, both single-point lock contention and eviction jitter decrease, which helps converge P99.

    3.3 Smarter Eviction and Prefetching

    Eviction policy should consider both “most recently used” and “rebuild cost,” preferring to keep segments that are expensive to rebuild; for predictable long-context prefixes, prefetch to move the data movement off the critical path into idle windows, reducing bursty bandwidth usage at hit time.

    3.4 Load Testing Must Look at the Tail, Not the Average

    Load tests should cover high concurrency, mixed workloads, and cache cold/warm transitions, with a focus on P95/P99, cache hit rate, memory usage, and bandwidth utilization. Use gradual ramp-up to locate the inflection point:

    1. # Illustrative: ramp load to find the tail-latency inflection point (pseudo-command)
    2. loadtest --rps 50,100,200,400 --duration 10m \
    3. --metrics p50,p95,p99,cache_hit,mem_used,bw_used

    3.5 Combine With General Cloud Capabilities

    In a cloud environment, you can use container platforms for elastic scaling, monitoring and alerting to track tail latency and resource watermarks, and logging services to accumulate hit rates and eviction reasons, forming an “observe—locate—tune” loop. These are all general capability categories and do not involve any specific vendor interface.

    4. Conclusion

    “A cache slower than computation” is not a paradox but an inevitable result of a changed resource structure: the cache moves cost from compute to bandwidth, memory, and scheduling, amplifying the tail when utilization is high. Engineering practice should make hit-rate and benefit checks a prerequisite gate, then combine sharding, tiering, smart eviction, and tail-oriented load testing so the cache actually serves the latency target instead of pushing P99 into an unacceptable range.