KV Cache Layered Storage: Breaking the GPU Memory Bottleneck in Long-Context Inference
This article explains how layered KV Cache storage (LayerSplit) addresses the GPU memory bottleneck in long-context inference for coding agents. It covers why context parallelism forces every rank to hold a full KV Cache replica, how splitting the cache by layer reduces memory usage roughly in proportion to the number of ranks, and how per-layer broadcasting trades memory for communication. It also details overlapping broadcasts with Indexer computation, reports throughput gains at high prefix-cache hit rates, outlines the approach's applicability boundaries, and offers practical deployment recommendations.
1. Background: Long Contexts and Coding Agent Workloads Push the Bottleneck to GPU Memory
In coding agent workloads, request patterns differ significantly from traditional chat. A single request often carries a very long context, and multiple turns share large identical prefixes (system prompts, repository structure, snippets of prior patches). This leads to two direct consequences.
First, the prefix cache hit rate is high, so the prefill phase does not need to recompute cached tokens. But it also means the cache itself must stay resident, and capacity becomes a hard constraint. Second, prefill is compute-intensive and is the main contributor to end-to-end latency, so online prefill nodes commonly adopt context parallelism (CP): a long sequence is split across multiple ranks for parallel processing.
The problem is that CP strategies in mainstream open-source implementations typically require every rank to hold a complete KV Cache replica so it can perform attention locally. The sequence is split, but the cache is not, so each GPU’s memory is occupied by large redundant replicas. Once memory is consumed, the number of cache tokens that can stay resident drops, batch size is forced to shrink, and GPU compute utilization ends up constrained by memory capacity. This is a typical resource mismatch: expensive compute units waiting on cheap storage.
2. Core Idea: Each GPU Holds Only a Subset of Layers
Layered storage (LayerSplit) starts from a simple observation: since attention is computed layer by layer, the cache can also be split by layer, with each CP rank responsible for storing only a subset of layers rather than all of them.
The benefit is that memory usage drops roughly in proportion to the number of ranks. Memory that originally held N cached tokens can support close to k×N effective cache capacity after layer-based ownership (k being the number of ranks participating in the split), so more long-context requests can stay resident and both batching and concurrency increase.
The cost is equally clear: when computing layer l, the rank holding that layer’s cache is not every rank, so the data must be delivered where it is needed. The approach is to have the rank that owns a layer’s cache broadcast that layer’s data to the other ranks before the layer’s attention computation begins, after which all ranks collaborate on that layer’s prefill. In other words, static redundant storage is replaced with dynamic on-demand transfer, trading communication for memory.
One easily overlooked engineering detail: the broadcast granularity is a layer, not the entire cache. Layer-by-layer pipelined broadcasting splits communication into many small tasks, which naturally suits overlap with computation and leaves room for the scheduling optimizations described later.
A sketch of the execution skeleton (pseudocode, expressing scheduling semantics only):
# Each rank stores only the KV Cache portion for layer_owner[layer]for layer in range(num_layers):owner = layer_owner[layer]# Non-owners post the receive early; the owner issues the broadcasthandle = broadcast_async(kv_cache[layer], src=owner)# Computation overlapped with this layer's broadcast: Indexer-related operatorsindexer_prefetch(layer)handle.wait()attn_out = attention_forward(layer, kv_cache[layer])
3. Hiding Communication Overhead: Overlapping with Indexer Computation
With a naive implementation, per-layer broadcasting adds noticeable communication time, especially when cross-node bandwidth is limited. The key optimization is to identify a段 of work in the computation graph that has no strong dependency on the cache data—the Indexer computation—and mask the broadcast against it on the timeline.
The Indexer supplies index/selection information for sparse attention. Its input size depends only on sequence structure and index dimensions and is far smaller than the full cache. In engineering terms, this cache is usually on the order of one eighth of the KV Cache. So the extra communication introduced by the whole flow is roughly 1/8 of the cache volume, with near-negligible impact on end-to-end performance.
The overlap implementation boils down to three points:
- Dependency decoupling: run the index operators that do not depend on the current layer’s cache first, filling the bandwidth idle window;
- Asynchronous communication: broadcasts use async handles, and receivers mount buffers ahead of time to avoid pipeline bubbles caused by synchronous waiting;
- Per-layer pipelining: layers form a software pipeline, with communication, indexing, and attention staggered across different layers.
4. Results and Applicability Boundaries
With a cache hit rate of 90% and request lengths between 40k and 120k, system throughput improves by roughly 10% to 132%, with longer contexts yielding larger gains. This is unsurprising: the longer the context, the larger the absolute volume of cache redundancy and the greater the memory dividend from layering; meanwhile, a larger single-layer cache is more easily absorbed by computation, so overlap efficiency also rises.
The boundaries should be viewed objectively: when contexts are short, cache hit rates are low, CP scale is small, or inter-GPU bandwidth is ample, the memory benefit from layering is limited while the scheduling complexity of broadcasting remains. So in architecture selection, layered storage is better suited as a targeted optimization for long-context prefill rather than a global default. This approach has been adopted by the open-source inference community and is listed as one of the core technologies in the inference stacks of some vendors for very large-scale coding agent scenarios.
5. Deployment Recommendations
- Measure before changing: analyze the length distribution and prefix cache hit rate of online requests, and confirm they fall in the high-benefit range before investing.
- Balance the layer mapping: split by per-layer cache volume rather than layer index, so no single rank becomes a memory hotspot.
- Align communication with topology: map layers to fit the high-bandwidth inter-GPU domain as closely as possible; fewer cross-node hops means better overlap.
- Keep a fallback path: automatically fall back to standard CP for short requests or low-hit scenarios to control tail latency.
- Prioritize observability: combine monitoring/alerting and logging to separately track resident memory, broadcast wait time, and pipeline bubble ratio, and tune parameters with data; on the cloud, rely on the container platform for elastic scaling and canary validation.
