When Caching Is Slower Than Computing: The Hidden Cost of Inference Acceleration
This article examines why adding a shared cache to a large-scale MoE inference service made p99 latency worse instead of better, increasing it from 3.96 seconds to 17.3 seconds while leaving the median unchanged. It breaks down the model structure, context parallelism, and two-tier cache design behind the bottleneck, identifies four hidden costs of cache reads, and outlines a practical redesign based on cost modeling, selective caching, batched I/O, asynchronous write-back, and tail-latency-based validation.
1. A Counterintuitive Result: Enabling the Cache Quadrupled Tail Latency
A team building an inference service for a very large Mixture of Experts (MoE) model ran into a result nobody expected. The system ran on a single node with 8 high-end accelerator cards. To speed up long-context processing, the engineers added a shared cache layer that stored previously computed attention intermediate results, hoping to reuse them when similar requests arrived and skip redundant computation.
The post-launch numbers were not encouraging. Median request latency barely improved, but the slowest 1% of requests (p99 latency) jumped from 3.96 seconds to 17.3 seconds. The team briefly rolled back the entire caching feature.
This technical report answers two questions: why reusing computed results slowed the system down, and how to restructure the cache so it actually delivers a benefit. The core conclusion can be stated up front: reading from a cache is not a zero-cost operation, and that cost is usually severely underestimated during capacity planning.
2. Understanding the Workload: Model Structure and Parallelism Determine the Bottleneck
To judge whether caching is worthwhile, you first need to understand three things: how large the intermediate results are, how they are distributed, and who is responsible for moving them.
2.1 Attention Compression Shrinks Each Data Item, but the Total Does Not Disappear
The system uses a low-rank compressed attention mechanism: the key-value information for each token is compressed into a shared low-rank vector, so the amount of intermediate data stored per token is fixed and small regardless of the number of attention heads. At FP8 precision, every 256 tokens are packed into one block of roughly 12.4MB, which works out to about 48.4KB per token.
That number is misleading. It looks small in isolation, but long-context scenarios routinely involve hundreds of thousands of tokens. Cache capacity requirements grow linearly with context length and quickly exceed what local memory can hold.
2.2 Context Parallelism Turns One Cache Read into Eight Synchronized Ones
Prefilling a long context is extremely compute-intensive, so the system uses context parallelism: a long input is split into several parts and assigned to multiple accelerator cards that process them simultaneously. Once processing finishes, the cards must exchange results through intra-node communication so that every card has the full preceding context.
The critical detail is that these 8 execution units are tightly coupled at every step, like meshing gears: if any single rank slows down, the other seven can only wait. This means an extra few tens of milliseconds on one card gets amplified into a global stall for the whole group.
2.3 Two Cache Tiers: L1 Is Fast and Small, L2 Is Large and Shared
The cache was designed as two tiers. The tier close to the compute units sits in local memory (L1) with low latency but limited capacity. The remote tier sits in a shared key-value store (L2) with large capacity that every inference instance can read, effectively a public library.
The problem is precisely in L2. Think of a chain of bubble tea shops: each store keeps its own books (L1), but so that headquarters can audit everything centrally, all transactions are also written into a single master ledger (L2), and only one accountant maintains that ledger. At peak time, eight stores write simultaneously and all end up queuing at the accountant’s desk.
3. Root Cause Breakdown: Four Hidden Costs of Reading the Cache
3.1 The Serialized Shared Store Becomes a Global Bottleneck
The shared key-value store typically has a single point of serialization on the write path: network round trips, serialization, and index updates happen in sequence. When all 8 ranks issue reads or write-backs at the same time, requests queue up and wait times degrade from milliseconds to seconds, landing exactly in p99.
3.2 Deciding Whether a Hit Is Possible Also Costs Money
Determining whether a request can be reused requires computing prefix hashes, comparing block boundaries, and querying the index. The longer the context, the higher the decision cost. When the hit rate is insufficient, the system pays for a full extra round of overhead with nothing to show for it.
3.3 Moving Data Can Cost More Than Recomputing It
A block is 12.4MB. Transferring it across the network and deserializing it into GPU memory consumes bandwidth and memory-copy time. If the amount of computation being reused was small to begin with, then “reading it out” is more expensive than “computing it again.”
3.4 Synchronization Barriers Turn Local Jitter into Global Jitter
Under context parallelism, the slowest rank sets the pace for everything. Any tail jitter introduced by the cache is amplified by the synchronization barrier, ultimately showing up as worse tail latency across the whole batch while the median barely moves. This is exactly the explanation for the case at hand: an unchanged median with exploding p99.
4. The Fix: Turning the Cache from a Burden into a Benefit
4.1 Build a Cost Model First, Then Decide Whether to Cache
Before launch, use a simple inequality as an admission check:
Benefit ≈ hit rate × compute time savedCost ≈ hit rate × (lookup + transfer + deserialization) + (1 - hit rate) × lookup overheadEnable only when Benefit > Cost × safety factor (1.5~2 recommended)
4.2 Raise the Bar: Cache Only What Is Worth Caching
Prioritize caching long prefixes and stable, frequently reused system prompts and fixed context. Disable caching outright for short requests and low-reuse content. Replace “cache everything” with tiered admission.
4.3 Turn Random Small I/O into Batched, Coalesced I/O
Aggregate the read and write requests from multiple ranks locally, then submit them in batches on a fixed time window or size threshold, compressing 8 random accesses into 1 sequential access. Also set a concurrency limit and timeout fallback for the shared store: on timeout, fall back to local computation.
4.4 Make Write-Back Asynchronous and Reads Local-First
Run cache write-back asynchronously, off the critical path. Fix the read order as L1 → L2 → recompute, and let L1 absorb the vast majority of hits, with L2 serving only as a cross-instance fallback.
4.5 Validate on Tail Latency, Not Averages
Monitoring should include p95/p99, cache hit rate, average queue wait length, per-block transfer time, and fallback rate, plus an automatic degradation switch: when tail latency exceeds a threshold, bypass the cache automatically.
5. Conclusion
A cache is not a free accelerator; it is a conditional bet. It only holds up when the reuse benefit clearly exceeds lookup and transfer costs and when it does not create tail latency inside a synchronized parallel structure. In very large model inference, the correct order is always to quantify the cost first, then talk about hit rate. If needed, Baidu AI Cloud’s related cloud services generally offer common building blocks such as caching, object storage, and monitoring and alerting, which can serve as reference base modules for building this kind of tiered system.
