Breaking the Memory Bottleneck: Cache Offloading and Prefetching for Long-Context LLM Inference

    This article explains why GPU memory capacity, rather than compute, becomes the first constraint in long-context LLM inference, and how offloading context cache to host memory with layered prefetching can raise decode batch size and throughput. It covers the feasibility boundary of offloading, a three-tier architecture that separates storage from compute, paged block management, a layer-and-step prefetch pipeline, double buffering, admission and fallback control, a high-fidelity simulator used to estimate the upper bound of gains before implementation, simulation results at 32K and 128K context, and a staged rollout plan with observability guidance for reproducing the gains in your own inference stack.

    1. The Problem: Why Memory Becomes the First Constraint in Long-Context Inference

    In disaggregated inference architectures that separate prefill from decode, the performance bottleneck for long contexts is shifting from compute to memory capacity. Take a very large model that uses a sparse attention mechanism: sparse attention sharply reduces the computational complexity of the prefill phase, keeping time to first token (TTFT) manageable even for long sequences. But as the sequence keeps growing, decode throughput does not scale with it.

    The root cause is that the size of the context cache in the decode phase (here referring to the compressed key-value state, i.e., latent cache) grows roughly in proportion to sequence length. Memory capacity is a hard constraint: the more space the cache occupies, the fewer concurrent requests fit, and the batch size is forced down. Batch size is exactly what determines whether matrix multiplication operators can reach the compute-bound region. If the batch is too small, weight loading and memory access overhead cannot be amortized, arithmetic intensity stays low, and decode becomes memory-bound. Only when the batch is large enough do compute utilization and memory bandwidth utilization rise together.

    This produces a clear causal chain:

    • Longer context → higher cache footprint per request;
    • Higher cache footprint → fewer concurrent requests that fit in memory;
    • Fewer concurrent requests → limited batch size and insufficient arithmetic intensity;
    • Insufficient arithmetic intensity → stalled decode throughput and persistently high cost per token.

    Note that this chain has nothing to do with whether compute is plentiful. Even if the GPU still has compute headroom, throughput cannot grow as long as memory cannot hold more requests. So the optimization direction is not to keep piling on compute, but to turn the cache from a “capacity-bottleneck resource” into a “tierable, movable resource.”

    2. Feasibility: Where the Gains from Offloading Actually Come From

    Moving the context cache down to host (CPU) memory is a natural idea: host memory capacity is typically one to two orders of magnitude larger, enough to support a much larger batch size. But feasibility hinges on one inequality — whether the transfer overhead is smaller than the waiting benefit.

    Let S be the amount of data that must be brought in for one layer in one step, and B the effective bandwidth between host and device. A single transfer then takes about S/B, while the per-step decode time saved by a larger batch is ΔT. Offloading pays off only when S/B is clearly smaller than ΔT and the transfer can be hidden behind computation. This leads to three key conclusions:

    1. Gains scale with context length. The longer the sequence, the larger the cache, and the more time a larger batch saves, so the relative benefit of offloading grows. For short contexts, transfer accounts for too large a share and the benefit may be negative.
    2. Gains are bounded by transfer bandwidth and interconnect topology. Inter-device interconnect bandwidth, the number of host memory channels, and support for pinned memory and multi-channel concurrent transfers all directly change the upper bound on the benefit.
    3. Gains depend on prefetching, not on-demand loading. If decode waits synchronously for data to arrive at every step, latency gets worse immediately. The transfer must be moved ahead of the moment the data is needed, so computation time covers transfer time.

    The engineering goal should therefore be stated precisely: maximize decode batch size and overall throughput under a per-request latency constraint (such as a cap on per-token output latency), rather than pursuing maximum concurrency without limits.

    3. Design: Decoupling the Storage Path from the Compute Path

    We abstract this into a general architecture in the form of an Expanded Sparse Server (ESS). The core idea is to fully separate where the cache is stored from where it is used, forming three tiers:

    • Compute tier: device memory holds only the cache shards and temporary tensors that actually participate in the current step;
    • Staging tier: host memory acts as a large cache pool, managed in pages per request;
    • Scheduling tier: an independent transfer and scheduling path handles swap-in/swap-out, prioritization, and concurrency control.

    Key design points:

    Paging and block granularity. Split each request’s cache into fixed-size blocks and use the block as the minimum unit of swap-in/swap-out. Blocks that are too small make transfer startup overhead dominate; blocks that are too large waste bandwidth on data that is moved but not fully used. The right size must be determined empirically against the number of layers and the attention sparsity pattern.

    Prefetch pipeline. Build a multi-level pipeline over two dimensions, layer and step: while layer i is still computing, asynchronously prefetch the blocks needed by layer i+1; while step t is still decoding, prepare data for steps t+1 and t+2 in advance. The scheduling logic can be described with the following pseudocode:

    1. # Pseudocode: layer-wise, two-step-ahead prefetch scheduling
    2. for step in range(max_steps):
    3. layer = 0
    4. while layer < num_layers:
    5. # Overlap current-layer compute with next-layer transfer
    6. compute(layer, resident_blocks[step][layer])
    7. async_prefetch(
    8. src=host_pool,
    9. dst=staging[step][layer + 1],
    10. stream=copy_stream
    11. )
    12. layer += 1
    13. # Step-level prefetch: prepare the farthest-ahead block for later steps
    14. async_prefetch(
    15. src=host_pool,
    16. dst=staging[step + 1][0],
    17. stream=copy_stream
    18. )
    19. wait_all_copy_streams()

    Double buffering and stream concurrency. Maintain double buffers for each staging slot to avoid the conflict of “compute reads a block while transfer writes the same block.” Use independent transfer streams and multiple concurrent channels, splitting one large copy into several medium copies to improve effective bandwidth utilization.

    Compatibility with incremental decode optimizations. This offloading mechanism is orthogonal to common optimizations such as multi-token prediction (MTP) and two-batch overlap: MTP increases the number of tokens produced per step, two-batch overlap uses one batch’s computation to hide another batch’s communication, and offloading addresses the question of how large the batch can be. The three stack rather than exclude each other.

    Admission and fallback control. When latency metrics approach the threshold, the scheduler should reduce prefetch depth, shrink the batch size, or fall back some requests to a memory-resident mode to keep tail latency under control. This step is a prerequisite for production deployment; a design without a fallback path is prone to latency jitter under real traffic.

    4. High-Fidelity Simulation: Getting Trustworthy Answers Before Writing Code

    The parameter space of an offloading design is large (block size, prefetch depth, channel count, batch size cap, context length, MTP order), so relying entirely on real-hardware experiments makes iteration extremely expensive. We therefore built a simulator that models three kinds of overhead:

    • Compute time: distinguishes compute-bound from memory-bound regions by operator type, varying nonlinearly with batch size;
    • Communication time: distinguishes communication patterns in prefill and decode, factoring in topology and concurrency;
    • Transfer time: models effective host-device bandwidth, startup overhead, and the overlap ratio.

    The value of the simulator is that it produces an “upper-bound curve” of the benefit: for a given hardware configuration and context length, it first computes the theoretical maximum throughput gain from offloading, then tells you whether the engineering effort is worth it. If the upper bound itself is insufficient, there is no need to enter the development phase.

    The simulation results show two patterns. First, at a 32K context, raising MTP from 2 to 4 improves overall throughput by about 123%, of which roughly 53 percentage points come from MTP itself and the remaining ~70 percentage points come from the offloading and prefetching mechanism — showing that once per-step output increases, cache transfer is more easily hidden behind computation, and the two are positively synergistic. Second, at an ultra-long 128K context with MTP set to 2, the offloading and prefetching mechanism alone delivers about a 123% throughput improvement, confirming that the benefit grows with context length.

    5. Implementation Advice and Validation

    To reproduce similar gains in your own inference system, proceed in this order:

    1. Measure first, optimize second. Collect cache footprint, batch size ceiling, and per-step decode latency across different context lengths to locate whether the real bottleneck is capacity or bandwidth.
    2. Use the simulator to set parameters. With the latency constraint as a hard boundary, sweep block size and prefetch depth to find the point of diminishing returns instead of tuning blindly.
    3. Roll out in stages. First enable “swap-out only, no prefetch” to validate correctness, then turn on layered prefetching, and finally raise the batch size cap and wire in the fallback strategy.
    4. Build observability. Set up monitoring and alerts for transfer wait time, prefetch hit rate, actual batch size, and tail latency percentiles so you can roll back quickly when something goes wrong.

    In short, the throughput ceiling of long-context inference is not determined by compute alone, but jointly by where the cache lives and when it is moved. Decoupling the context cache from device memory, combined with layered prefetching and latency-aware scheduling control, can substantially enlarge the batch size without sacrificing per-request latency, yielding both higher throughput and lower unit cost.