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

    Caching is often the first optimization teams reach for when serving large-scale Mixture-of-Experts (MoE) models, yet reading cached data is not free. This article examines a real case where a shared attention cache left p50 latency unchanged but pushed p99 from roughly 3.96 seconds to 17.3 seconds. It breaks down the four overlooked cost paths—bandwidth contention, tail-latency amplification, scheduling interference, and hit-rate illusion—and offers a reusable framework for measuring hit savings, transfer cost, and interference before enabling a cache. Readers will find practical guidance on tiered caching, asynchronous writes, cache-aware scheduling, admission control, and canary rollout with p99 as the primary constraint.

    1. A Common Assumption That No Longer Holds: Cache Is Not Always an Accelerator

    When building inference services for very large Mixture-of-Experts (MoE) models, teams instinctively add caching. The idea is to store intermediate attention results that have already been computed, so similar requests can hit the cache and skip redundant work. Intuitively, reading data from GPU memory should be cheaper than running the matrix multiplications and additions again.

    A real deployment proved otherwise. A team deployed a very large MoE model on a node with 8 high-performance accelerators. To speed up long-context processing, they added a shared cache that stored previously computed compressed attention key-value data. After enabling it:

    • Median latency (p50) barely changed.
    • The slowest 1% of requests (p99) jumped from about 3.96 seconds to about 17.3 seconds.
    • The team eventually rolled the feature back entirely.

    The core conclusion is a single sentence: reading a cache has a cost, and that cost has long been severely underestimated. This article abstracts the phenomenon into a general engineering problem and provides a reusable framework for analysis and optimization.

    2. Understand the Workload First: How Large Is the Attention Intermediate Data?

    To decide whether caching pays off, calculate the unit cost of “read” versus “compute.”

    Consider an attention mechanism that compresses each token’s key-value information into a shared low-rank vector (often referred to in the industry as low-rank compressed attention). Its advantage is that the intermediate data stored per token is fixed and small, regardless of the number of attention heads. Assume that at FP8 precision, every 256 tokens are packed into one block of about 12.4MB, or roughly 48.4KB per token on average.

    This allows a simple order-of-magnitude estimate:

    1. One request reusing 32K tokens:
    2. 32768 / 256 * 12.4MB ≈ 1.55GB
    3. With 64 concurrent hits:
    4. 64 * 1.55GB ≈ 99GB of read volume

    No matter how strong the compute is, moving nearly 100GB of data into the compute units within a millisecond-scale window will saturate both GPU memory bandwidth and interconnect links. At that point, the “saved compute” is far smaller than the “extra data movement.”

    3. Where the Slowdown Comes From: Four Overlooked Cost Paths

    3.1 Bandwidth Contention: Cache Reads Compete With the Main Compute Path

    Inference performance depends heavily on GPU memory bandwidth. The extra reads from cache hits compete for the same bandwidth pool as model weight loading, KV access, and expert routing, directly lengthening execution time for all requests. Tail requests suffer the most.

    3.2 Tail-Latency Amplification: A Queuing Theory Perspective

    Treat the system as a queuing system: p50 is determined by average service time, while p99 is determined by queuing and interference. Caching increases the variance of service time (hits are fast, misses are slower, and data movement adds jitter). Higher variance amplifies tail latency non-linearly, which explains the typical pattern of “flat median, exploding p99.”

    3.3 Scheduling Interference: Long Tasks Block Short Tasks

    If cache filling, eviction, and prefetching run synchronously on the critical path, one long request can block subsequent short requests. Under continuous batching, a single slow request holds up the completion time of the entire batch.

    3.4 Hit-Rate Illusion: Similarity Does Not Mean Reusability

    “Similar requests” may be close in vector space, but that does not mean their prefixes are identical. With a low hit rate, the cache contributes only write overhead and metadata management overhead, with almost no benefit.

    4. Diagnosis: Measure First, Then Refactor

    Do not enable caching based on intuition. Use data to answer three questions first:

    • Hit savings: How much compute time is actually saved on a hit (not theoretical FLOPs)?
    • Transfer cost: How many bytes does an average hit move, and how much bandwidth-time does it consume?
    • Interference cost: After enabling the cache, how much do p99, timeout rate, and batch throughput change?

    It is recommended to consistently collect the following metrics in monitoring, alerting, and logging services:

    1. cache_hit_ratio, cache_read_bytes, cache_write_bytes,
    2. prefill_time_ms, decode_time_ms, batch_size,
    3. gpu_mem_used, gpu_bw_util, queue_wait_ms, p99_latency

    The decision rule can be written as a minimal inequality:

    1. Compute time saved by a hit > read transfer time + management overhead + interference loss

    Only when the left side is significantly larger than the right side is the cache a net gain.

    5. Refactoring: Turning the Cache From a Drag Into a Real Accelerator

    5.1 Make “Reads” Cheaper

    • Increase the block-level compression ratio to reduce bytes per token.
    • Use tiered caching: hot data in high-speed GPU memory, warm data in host memory, cold data in object storage.
    • Apply double buffering and prefetching for data movement to avoid synchronous blocking.

    5.2 Move “Writes” Off the Critical Path

    Make writes, evictions, and index updates asynchronous, decoupled with a message queue. Keep the critical path read-only and reorganize in the background.

    5.3 Make Scheduling Cache-Aware

    • Prioritize cache-hit requests for batching and throttle misses to prevent long tasks from blocking others.
    • Set concurrency and bandwidth quotas to prevent cache reads from saturating bandwidth.
    • Use container platforms for resource isolation so cache tasks and inference tasks do not interfere with each other.

    5.4 Replace “Cache Everything” With Admission Control

    Cache only requests whose prefixes match exactly, whose length exceeds a threshold, and whose historical hit rate is stable; compute everything else directly.

    Illustrative policy:

    1. def should_cache(req, stats):
    2. if req.prefix_exact_match is False: return False
    3. if req.tokens < 8192: return False
    4. if stats.hit_ratio(req.bucket) < 0.35: return False
    5. return stats.estimated_read_ms() < stats.estimated_compute_ms() * 0.5

    5.5 Canary Rollout With Tail Latency as the First Priority

    Roll out gradually at 1% → 5% → 20% of traffic, watching p99 and timeout rate. If p99 rises beyond budget, roll back automatically.

    6. Implementation Notes on Baidu AI Cloud

    When building this kind of inference service on Baidu AI Cloud, you can assemble a closed loop from general-purpose capabilities: object storage for cold cache, a message queue for asynchronous writes, container platforms for resource isolation and elastic scaling, and monitoring, alerting, and logging services for metric collection and automatic rollback. The key is to turn “whether the cache is a net gain” into an observable, canary-ready, and rollback-capable engineering decision rather than a one-time switch.

    7. Conclusion

    There is no free lunch with caching. Its benefit lies in compute, while its cost lies in data movement, scheduling, and tail interference. Calculate bytes per token and the bandwidth budget first, then use p99 as the constraint for admission control and canary rollout. Only then can a cache become a true accelerator instead of the hidden burden that turns 4 seconds into 17.