When Caching Is Slower Than Computing: The Hidden Cost of Inference Acceleration
This article examines why adding a shared attention cache to a large MoE model's inference pipeline can make tail latency worse instead of better. It breaks down the real costs of cache lookup, serialization, write-back amplification, and synchronization, then presents three optimization paths: asynchronous batched writes, batched reads with partial-hit support, and tail-latency-aware admission and fallback. It also outlines when caching should be avoided altogether.
1. A Counterintuitive Result: Tail Latency Quadrupled After Enabling the Cache
A team was serving inference for a very large mixture-of-experts (MoE) model on a single node equipped with 8 high-bandwidth accelerator cards. To reduce time-to-first-token on long prompts, they added a shared attention cache in front of the existing pipeline: previously computed attention intermediates were stored and reused whenever an incoming request shared a similar prefix, avoiding redundant computation.
The production data contradicted expectations. Median latency barely improved, while tail latency for the slowest 1% of requests deteriorated from roughly 3.96 seconds to 17.3 seconds. The team ended up rolling the whole thing back.
The value of this incident is that it puts a long-ignored fact on the table: reading from a cache is not a zero-cost operation. A cache only pays off when the compute saved by a hit clearly exceeds the extra overhead introduced by lookup, transfer, and synchronization. Once that inequality flips, the cache stops being an accelerator and becomes an amplifier — amplifying queueing and jitter.
2. Before Judging Whether a Cache Pays Off, Understand How Workload and Hardware Couple
2.1 The Size of Attention Intermediates Determines Cache Economics
This model family uses a low-rank compressed attention variant that compresses each token’s key-value information into a shared low-rank vector. As a result, the intermediate data that must be stored per request stays constant and small regardless of the number of attention heads. At FP8 precision, 256 tokens are typically packed into one block of about 12.4MB, which works out to roughly 48.4KB per token.
That number is the starting point for any caching decision. A single block is small, so one transfer looks cheap. But long prompts easily reach hundreds of thousands of tokens, the block count grows linearly, and the cache’s total data movement quickly exceeds the cost of simply recomputing.
2.2 Context Parallelism Turns Waiting into a Global Cost
Long-prompt prefill commonly uses context parallelism: a sequence is split into shards assigned to multiple ranks that compute in parallel, then exchange intermediates through collective communication so every rank holds the full context.
Under this topology, the 8 ranks behave like meshed gears — a delay in any one of them stalls the entire group. Cache lookup time is no longer a local cost for a single request; it is amplified by the synchronization barrier into lost throughput across the whole group. Tail latency is fundamentally the slowest-member effect at a synchronization point.
2.3 The Structural Bottleneck of a Two-Level Cache
A typical implementation has two levels: a local memory tier near the compute (L1) with small capacity and low latency, and a remote key-value store (L2) with large capacity that can be shared across inference instances — effectively a public library.
The bottleneck is almost always L2. Think of a retail chain: each store keeps its own books quickly, but headquarters requires every transaction to be consolidated into a single master ledger maintained by exactly one accountant. At peak hours, eight stores write simultaneously and all of them queue at that one accountant. That is an accurate picture of a shared cache under high concurrency.
3. Cost Breakdown: Where Exactly the Cache Loses Time
- Lookup and addressing overhead: prefix matching must traverse the block index; with long contexts the index is large, so the search itself lands on the critical path.
- Serialization/deserialization and network round trips: the remote tier requires network access, and round-trip jitter there differs from local memory by orders of magnitude.
- Write-back amplification: every block of every request must be written to the master ledger, so write traffic scales with concurrency, and writes compete with reads for the same bandwidth and connection pool.
- Synchronization waits and head-of-line blocking: if any rank has not received its data, the whole group stalls, and time spent waiting in the queue is counted as latency.
Summing the four explains the opening paradox: median requests may gain slightly from a hit, but tail requests simultaneously encounter a miss, write-back congestion, and synchronization waits, so their latency is multiplied rather than added.
4. Optimization Path 1: Replace Synchronous Blocking Writes with Asynchronous Batched Writes
The core principle is to move cache writes off the critical path and reduce unit cost through batching.
# Illustration: asynchronous batched write-back to keep the inference hot path unblockedclass AsyncCacheWriter:def __init__(self, backend, batch_size=64, flush_ms=20):self.backend, self.batch_size, self.flush_ms = backend, batch_size, flush_msself.buf, self.last_flush = [], time.monotonic()def submit(self, block_key, block_bytes):self.buf.append((block_key, block_bytes))if len(self.buf) >= self.batch_size or \(time.monotonic() - self.last_flush) * 1000 >= self.flush_ms:self.flush()def flush(self):if not self.buf:returnbatch, self.buf = self.buf, []self.last_flush = time.monotonic()# Pipelined batch write to the remote tier; failures only emit metrics and never affect the responsethread_pool.submit(self.backend.put_many, batch)
Three points matter here: a failed write degrades gracefully instead of raising; batch-size and time-based triggers work together to balance throughput against freshness; and write concurrency is bounded by a semaphore so writes cannot starve read bandwidth.
5. Optimization Path 2: Batch Reads Too, and Allow Partial Hits
- Prefix block alignment: use a fixed block as the minimum reuse granularity to avoid fine-grained query storms.
- Batched read API: merge block lookups for multiple requests in the same batch into a single remote call.
- Partial hits are usable: start prefill as soon as the leading blocks hit, and recompute only the missing portion instead of making the whole request wait.
- Two-tier consistency policy: L1 uses versioned write-through or lazy backfill, and L2 sets a reasonable TTL to prevent dirty reads that trigger recomputation.
6. Optimization Path 3: Tail-Latency-Oriented Admission and Fallback
Governing tail latency requires explicit mechanisms rather than reliance on averages:
- Concurrency admission: when the remote tier’s queue depth exceeds a threshold, temporarily reduce cache usage and prioritize the compute path.
- Timeout and fall back: set a hard timeout on cache reads and switch to local recomputation immediately on timeout, converting uncontrollable waiting into controllable compute.
- Priority isolation: separate connection pools for reads and writes, and separate queues for long-prompt prefill and short requests so they do not trample each other.
- Observability: instrument P50/P95/P99 hit rate, remote round-trip time, write-back backlog, and synchronization barrier wait time separately, to pinpoint which stage is actually dragging things down.
7. When Not to Use a Cache
Caching is not a universal optimization. Be cautious or disable it in these cases:
- Low prefix overlap across requests, with hit rate persistently below the break-even threshold;
- Small blocks but an enormous block count, where total data movement exceeds recomputation cost;
- A cluster already bound by a synchronization bottleneck, where any additional network round trip gets amplified;
- A remote tier that is a shared single point, where write traffic grows linearly with the number of instances.
A practical rule of thumb: keep the cache only if (compute time saved by a hit − lookup and transfer time) is still positive at P99.
8. Conclusion
“Caching is always faster” is a piece of common sense that gets overturned. What truly determines inference performance is not the single attractive number of cache hit rate, but the comparison between hit benefit and read cost in the tail of the distribution. Make writes asynchronous, make reads batched, make timeouts and fallback default capabilities, and pair them with solid tiering and metric observability — only then does a cache turn back into an accelerator instead of a drag. In inference service practice on platforms such as Baidu AI Cloud, this “calculate the cost first, then talk about hit rate” methodology is likewise key to controlling tail latency in long-context inference.
