When Cache Reads Are Slower Than Compute: The Hidden Cost of Inference Acceleration

    This article examines why adding a shared cache to an LLM inference path can degrade tail latency instead of improving throughput. Using a low-rank compressed attention example, it breaks down the underestimated costs of cache reads—fragmented random access, bandwidth contention with compute, tail amplification, and hit-detection overhead—and shows why an ideal sub-millisecond copy can still trigger multi-second p99 spikes. It then outlines practical design and scheduling countermeasures, including hit thresholds, memory layout and alignment, bandwidth isolation, cost-aware eviction, observability, and a deployment checklist centered on proving net benefit before enabling reuse.

    1. A Counterintuitive Production Incident

    A team built an inference service for a very large Mixture-of-Experts (MoE) model on a single node with 8 accelerator cards. To speed up long-context requests, they added a shared cache layer in the request path that stored previously computed attention intermediate results, hoping similar requests could reuse them directly. After deployment, median latency barely moved, but the slowest 1% of requests degraded from about 3.96 seconds to 17.3 seconds. The team had to roll the whole thing back.

    The conclusion is a single sentence: reading from a cache has a cost, and that cost has long been underestimated. A cache is not a “free second tier of compute.” It is an extra memory, bandwidth, and scheduling operation, and it only pays off when the recompute it saves is significantly larger than the data fetch and wait it adds.

    2. How the Model Actually Consumes VRAM and Bandwidth

    Take a low-rank compressed attention scheme as an example (compressing each token’s keys and values into a shared low-rank vector). Its benefit is that the intermediate data stored per token is decoupled from the number of attention heads. Assume that under FP8 precision, every 256 tokens are packed into one block of about 12.4MB, which works out to roughly 48.4KB per token.

    This allows a rough capacity and traffic estimate (illustrative):

    1. KV bytes per token ≈ 48.4 KB
    2. 32K context, single request ≈ 32768 * 48.4KB ≈ 1.55 GB
    3. One cache hit requires moving 1.55 GB
    4. At 2 TB/s effective bandwidth, the copy alone ≈ 0.78 ms (ideal)

    The ideal value is under 1 millisecond, so why does tail latency multiply several times over? Because the real path is not a single large block copy, but fragmentation, contention, and queuing.

    3. Where the Cache Gets Slow: Four Underestimated Costs

    1. Fragmented random reads: Hit detection happens per block, and hits are often scattered, forming many small transfers with very low bandwidth utilization. Latency is dominated by “number of operations × per-operation overhead.”
    2. Bandwidth contention with compute: Copies and operators compete for VRAM bandwidth at the same time, slowing compute, which in turn lengthens cache residency time, creating a feedback loop.
    3. Tail amplification: p99 is determined by the slowest batch of requests. As long as the cache path introduces jitter (locks, allocation, swap-in/swap-out), tail latency gets amplified while the median looks fine.
    4. Hit-detection and scheduling overhead: Similarity/prefix matching, hashing, metadata lookups, and batch reassembly are all pure overhead; when the hit rate is low, it is all net loss.

    4. Treat “Reading the Cache” as a First-Class Design Concern

    • Hit threshold: Only use the cache when the number of reused blocks is at least a threshold (e.g., ≥ 4 blocks) and the estimated recompute time is greater than the fetch time multiplied by a safety factor.
    • Layout and alignment: Store large blocks contiguously, align to pages, and use a pre-allocated pool to avoid runtime allocation; make access as sequential as possible.
    • Bandwidth isolation: Give copies a dedicated queue and priority, let low-priority tasks yield, and avoid competing with critical operators for bandwidth.
    • Residency and eviction: Score eviction by “recompute cost × hit probability” rather than simple LRU; give long-tail contexts a separate quota.
    • Observability first: Instrument hit block count, copied bytes, queue time, copy duration, operator duration, and swap-in/swap-out counts separately.

    Illustrative logic:

    1. def should_use_cache(blocks, recompute_ms, copy_ms_est):
    2. if blocks < 4: return False
    3. if copy_ms_est * 1.5 >= recompute_ms: return False
    4. return queue_depth(copy_lane) < LOW_WATERMARK

    5. Scheduling Layer: How Batching and Caching Shape Each Other

    The cache changes the shape of batches: hit requests become “lighter” and easily aggregate into large batches, while miss requests become “heavier” and get pushed to the back of a long queue. The countermeasure is to make scheduling cache-aware: spread heavy requests across different steps, set timeouts and degradation for copies (fall back to recompute on timeout), and apply affinity scheduling for requests with the same prefix to keep reuse locality within the same execution domain.

    6. Deployment Checklist

    1. Replay offline first to quantify hit rate and block distribution before deciding whether to go live.
    2. Set a lower bound on “per-request cache benefit”; bypass directly if it is not met.
    3. Separate copy and compute into different queues with different priorities, and configure a hard timeout.
    4. Plan capacity by “total cacheable context” rather than by request count.
    5. Build a tail-latency dashboard that shows p99 and copy queue time side by side.
    6. Roll out gradually by traffic percentage, and keep a one-click bypass switch.

    When deploying on Baidu AI Cloud, you can combine the container platform to host the inference runtime, object storage to accumulate replay samples, and logging and monitoring/alerting services to track tail latency and copy queuing, forming a closed loop of “observable—replayable—degradable.” The core principle: calculate the read cost first, then talk about reuse benefit; a cache that cannot prove net benefit is worse than no cache.