Inference Cache Slowdown: The Hidden Cost of Caching in LLM Serving

    This article examines a counterintuitive production incident where adding a cross-request cache to a large MoE model's inference pipeline left median latency unchanged but pushed tail latency from roughly 3.96 seconds to 17.3 seconds. It breaks down why cache reads are not free, how serialized writes, synchronization barriers, and hit-checking overhead compound across parallel ranks, and when caching stops being an accelerator. It then presents practical fixes: asynchronous writes, batching and shard spreading, admission control, tiered locality-first reads, and observability, along with a simple cost criterion and methodology for validating optimizations using tail metrics.

    1. A Counterintuitive Production Incident

    A team was serving inference for a very large Mixture-of-Experts (MoE) model on a single node with 8 high-end accelerators. To reduce time-to-first-token on long prompts, they placed a cross-request shared cache in front of the inference path to store previously computed attention intermediate results, hoping that similar prefixes would hit the cache and skip redundant computation.

    The post-launch numbers were surprising: median latency barely moved, while tail latency for the slowest 1% of requests degraded from about 3.96 seconds to 17.3 seconds. The team’s first instinct was a full rollback.

    The value of this case is not any specific number. It exposes a long-overlooked rule: reading from a cache is not a zero-cost operation, and its cost is routinely underestimated. When the read cost exceeds the computation it saves, a cache degrades from an accelerator into a speed bump.

    2. Understanding the Workload: How the Model Consumes Accelerators

    Diagnosing the problem requires seeing the shape of the system first.

    Attention compression. This class of models typically uses a low-rank latent attention scheme that compresses each token’s key-value information into a shared low-rank vector. The key property is that the intermediate data stored per token is constant regardless of the number of attention heads. In this case, under FP8 precision, every 256 tokens are packed into one block of roughly 12.4MB, or about 48.4KB per token. That looks small, but long-context workloads multiply it quickly.

    Prefill context parallelism. The prefill stage for long prompts is extremely compute-intensive, so the system splits a text into several parts, processes them in parallel across multiple cards, and exchanges intermediate results over intra-node communication so every card holds the full context. The cost: 8 ranks mesh like gears, and if one slows down, the other seven must wait.

    Two-tier cache structure. The L1 cache sits in local memory: fast but small. The L2 cache lives in a centralized key-value store: large, and shareable across multiple inference instances, much like a public library.

    The bottleneck was exactly at L2. Think of a chain of bubble tea shops: each store records its own sales quickly, but headquarters requires all transactions to be written into a single master ledger kept by one accountant. At peak hours, eight stores write simultaneously and all pile up at that one desk—the more stores, the worse the jam.

    3. Root Cause: Three Layers of Cost Stacking Up

    3.1 Queueing Cost of Serialized Writes

    Centralized storage has a throughput ceiling. When 8 ranks finish their shards at the same moment and write back simultaneously, requests queue on the storage side. Queueing time scales nonlinearly with concurrency, which is the classic signature of unchanged median latency with a sharply worsening tail: most requests pass between peaks, while a few land at the back of the queue.

    3.2 Amplification from Synchronization Barriers

    Under parallel splitting, cache reads and writes were placed on the synchronization path. A 10-millisecond storage jitter, multiplied by 8-way waiting and multiple synchronization rounds, can amplify into a multi-second stall end to end. In distributed systems, tail latency is never an average problem; it is the problem of the slowest shard.

    3.3 Hidden Overhead of Hit Checking and Transfer

    Hits are not free: you must build the query key, compare prefixes, fetch the data, deserialize it, and move it into device memory. If the hit benefit is “skip one computation” while the hit cost is “one cross-network read plus one large-object deserialization,” the trade is already a loss under low compression ratios or short prefixes. Writing to the cache also consumes bandwidth and memory that could have gone to computation—an opportunity cost.

    This can be abstracted into a criterion: a cache pays off only when hit benefit × hit rate > read cost + amortized write cost + synchronization amplification. The original system’s mistake was estimating only the left-hand side.

    4. The Fix: Making the Cache an Accelerator Again

    4.1 Make the Read/Write Path Asynchronous

    Move cache writes off the critical path with a “return first, persist in the background” strategy, using a bounded queue for backpressure:

    1. # Illustrative: cache writes do not block the main inference path
    2. if write_queue.qsize() < MAX_PENDING:
    3. write_queue.put_nowait(block) # drop when full; prioritize latency
    4. # A background thread batches and merges writes to remote storage

    4.2 Batching and Shard Spreading

    Merge 8-way concurrent writes into a few large sequential writes, and spread them across different storage shards by rank—turning “eight stores fighting over one accountant” into “parallel service windows.”

    4.3 Admission Control and a Hit Threshold

    Not every request is worth a cache lookup. Set thresholds based on prefix length, block count, and estimated compute savings; short requests skip the cache entirely. Also give cache queries a timeout and fast failure, falling back to the pure compute path on timeout.

    4.4 Tiering and Locality First

    Prefer the local memory tier; use the remote tier only as a fallback for cross-instance sharing. Reserve a fixed cache pool on the device side to reduce jitter from dynamic allocation and data movement.

    4.5 Observability First

    Instrument these separately: hit rate, read latency distribution (P50/P95/P99), write queue backlog, synchronization wait time, and the number of dropped write requests. Without these metrics, teams can only judge “whether the cache helps” by intuition.

    5. Methodology Takeaways

    The general lesson is that any optimization must be evaluated in the end-to-end path, not rationalized on a local metric. A cache is fundamentally a trade of space and bandwidth for time; once bandwidth becomes the scarce resource, the equation flips. The safer engineering approach is to first establish a latency budget and a cost criterion, then run a canary with a rollback switch, and finally validate with tail metrics rather than averages. In inference serving practice on platforms such as Baidu AI Cloud, combining monitoring and alerting, logging services, and elastic container resources to build capacity and latency baselines is likewise an effective way to avoid these “hidden costs.”