Cache Reads Are Not Free: P99 Latency Optimization for LLM Inference
A shared prefix cache was added to a large MoE model serving stack to accelerate long-context inference. Median latency barely moved, but p99 jumped from 3.96s to 17.3s, and the team rolled the whole thing back. This article breaks down where cache-read cost actually comes from — queuing, synchronization barriers, write amplification, fixed per-request overhead, hit validation, and miss penalties — then shows how to measure it stage by stage and how to fix it with asynchronous write-back, L2 sharding, admission control, hotness tiering, prefetching, and cost-aware scheduling. The takeaway: evaluate any shared component as a queuing system, not as storage.
A counterintuitive incident: enabling the cache made tail latency 4x worse
A team was serving inference for a very large mixture-of-experts (MoE) model on a single node with 8 accelerator cards. To speed up long contexts, they added a shared prefix cache layer that reused previously computed attention intermediate results. After rollout, median latency was essentially unchanged, but the slowest 1% of requests (p99) went from 3.96 seconds to 17.3 seconds. The team ended up rolling the entire change back.
The core conclusion of this postmortem is one sentence: reading from a cache is not a zero-cost operation, and beyond capacity and bandwidth, the cost that gets ignored most often is queuing and synchronization.
1. First, understand the shape of the system: why it is extremely sensitive to being “a little slow”
1.1 Low-rank compressed attention makes each block very small
The system uses low-rank compressed multi-head latent attention (each token’s key/value information is compressed into a shared low-rank vector). Its key property: no matter how many attention heads there are, the amount of intermediate data each token must write to storage or transfer is fixed and very small. At FP8 precision, every 256 tokens are packed into one block, a block is about 12.4MB, which works out to only about 48.4KB per token.
This leads to a conclusion that is easy to misjudge: small blocks → cheap cache reads. In reality, small blocks mean metadata and per-request overhead make up a larger share, and every read pays a fixed cost.
1.2 Prefill context parallelism meshes 8 cards together like gears
Long-context prefill is compute-heavy, so the text is split and distributed across multiple cards for parallel processing (prefill context parallelism). Once processing finishes, the cards must synchronize through inter-card communication before each card has the complete context. The 8 ranks behave like meshed gears: if one is slow, seven wait.
1.3 A two-tier cache structure
- L1: local memory, fast and small in capacity, visible only to the local engine;
- L2: shared key-value store, large in capacity and shared across engines, effectively a public library.
The bottleneck is at L2. Think of a chain of bubble tea shops: each shop keeps its own books (L1), but there is only one accountant for the headquarters ledger (L2), so when all eight shops write at the same time, they all queue.
2. Cost breakdown: what actually makes cache reads expensive
- Queueing delay: the shared store has limited concurrency, and wait time grows nonlinearly as request arrival rate rises.
- Synchronization barrier amplification: if the cache read of any single rank is blocked, the whole group of 8 cards idles, so one jitter event is amplified into 8x wasted resources.
- Write amplification and write-back contention: hit reads and miss write-backs share the same channel and crowd each other out.
- Fixed overhead: serialization, deserialization, metadata lookup, and network round trips make up a very high share in small-block scenarios.
- Hit validation cost: prefix matching, block hashing, and version consistency checks all happen on the critical path.
- Miss penalty: looking and finding nothing is slower than not looking at all.
From a queueing theory perspective, with service rate μ and arrival rate ρ = λ/μ, wait time scales roughly as 1/(1−ρ). When ρ rises from 0.6 to 0.9, wait time grows about 4x — consistent in magnitude with the observed p99 degradation.
3. Measure the cost first, then optimize
- Instrument each stage separately: queueing, lookup, transfer, deserialization, and validation each get their own timer;
- Look at p50 and p99 separately; the median hides the long tail;
- Watch the share of end-to-end latency spent waiting on the cache; alert once it exceeds 5%;
- Use flame graphs to locate the inter-card synchronization idle windows.
# Observability skeleton on the critical path (illustrative)t0 = now()entry = cache_get(block_ids) # includes queueing + lookup + transfermetrics.observe("cache_wait", now() - t0)if entry is None:kv = prefill(block_ids) # miss: extra penaltycache_put_async(block_ids, kv) # key: move it off the critical pathelse:kv = entry
4. The redesign: turn synchronous reads into asynchronous, sharded, admission-controlled ones
- Asynchronous and batched write-back: the hit path is read-only; miss write-backs go into a local queue and are submitted in batches aggregated by block, eliminating read/write mutual blocking.
- Shard L2: shard by block hash across multiple storage instances to remove the single accountant and turn concurrency from 1 into N.
- Admission control: set a concurrency limit and timeout for cache access; on timeout, degrade immediately to direct computation, and never wait indefinitely.
- Hotness tiering: promote only high-reuse blocks to L2, keep cold blocks in L1 or drop them, reducing total traffic.
- Prefetching: based on prefix matching results, issue reads early and hide the wait before computation begins.
- Cost-aware scheduling: use the cache only when the compute time saved exceeds the total cost of reading.
expected_gain = hit_prob * recompute_costtotal_cost = queue + transfer + deserialize + verifyuse_cache = expected_gain > total_cost * safety_factor
5. Results and a transferable checklist
After the redesign, median latency was essentially unchanged, p99 returned close to its original level, and the cache started producing a net gain. Transferable lessons:
- Evaluate any shared component as a queuing system, not as storage;
- Synchronization barriers amplify local jitter, so prioritize asynchrony over scaling out;
- For small-object scenarios, account for fixed overhead rather than only byte counts;
- Tail latency is a design constraint, not a post-launch observation;
- A cache must ship with a degradation path and an admission policy; only a rollback-safe design is safe to launch.
When deploying on the cloud, you can combine a container platform to host the inference engines, a monitoring and alerting system for percentile latency observation, and a logging service for full-chain tracing, turning cache cost into a continuously measurable engineering metric instead of one-off tuning luck.
