KV cache offloading

On this page 7

KV cache offloading is the technique of moving the key and value tensors that a transformer stores during inference out of scarce GPU high-bandwidth memory into a cheaper, larger tier such as CPU DRAM, local NVMe drives or networked storage, and bringing them back when they are needed. In AI infrastructure, it is one of the main tools for serving long contexts, many concurrent users and reusable prompts without buying more accelerators.

The technique trades memory capacity for transfer latency. Every token kept in a slower tier frees GPU memory for larger batches, but it must cross a PCIe, NVLink or network link before the attention layers can use it again, so the design goal is to hide that transfer behind useful computation.

Why the KV cache outgrows GPU memory

The KV cache grows linearly with sequence length, batch size and model depth, because every layer stores one key vector and one value vector per token and per attention head group. For a 70-billion-parameter model with grouped-query attention, a single 128,000-token context can need tens of gigabytes in 16-bit precision, which rivals the weights themselves.

When the cache cannot fit, a server must reject or preempt requests, shrink batches, or recompute evicted tokens through a fresh prefill phase. Offloading adds a fourth option: keep the data, but park it in a slower tier until the next decoding step or the next conversation turn requires it.

The economics follow from the price gap between tiers. A gigabyte of host DRAM costs far less than a gigabyte of HBM, and NVMe capacity is cheaper still. Reloading a cached prefix also costs much less energy than running the attention and feed-forward computation again, so offloading often lowers cost per token as well as latency for repeated prompts.

Offloading also changes capacity planning. Instead of sizing a GPU cluster for peak cache demand, operators can size GPU memory for active decoding only and let the slower tiers absorb idle sessions, which makes utilization more predictable during traffic spikes.

How KV cache offloading works

An offloading system manages the cache in fixed-size blocks, similar to pages in operating system memory, which is why it pairs naturally with PagedAttention. A cache manager tracks where each block lives, decides which blocks to evict, and schedules asynchronous copies so that transfers overlap with computation on other layers or other requests.

The following table lists the storage tiers commonly used and their rough characteristics; exact figures depend on hardware generation and topology.

TierTypical capacity per nodeApproximate bandwidth to GPUBest use
GPU HBM80-192 GB per GPUSeveral TB/s (local)Active decoding tokens
CPU DRAM0.5-2 TBAbout 50-450 GB/s via PCIe or chip linksPaused sessions, recent prefixes
Local NVMe SSDSeveral to tens of TBAbout 5-25 GB/s across drivesLong-lived prompt prefixes
Networked storageEffectively unboundedDepends on RDMA fabricCluster-wide shared prefixes
Memory tiers used for KV cache offloading

Systems usually apply an eviction policy such as least recently used, sometimes weighted by prefix popularity. Some designs prefetch layer by layer: while layer n runs attention, the blocks for layer n+1 are already in flight, which hides much of the copy time during long prefill computations.

Main offloading strategies

KV cache offloading is not one algorithm but a family of strategies that differ in what they move and when. Choosing among them depends on whether the bottleneck is concurrency, context length or repeated prompts.

StrategyWhat is offloadedPrimary benefit
Session offloadingWhole caches of idle conversationsFast resume of multi-turn chats
Prefix offloadingShared system prompts and documentsAvoids repeated prefill work
Layer-wise offloadingOlder layers during long prefillFits very long single contexts
Sparse retrieval offloadingLow-attention tokens of long contextsLoads only relevant blocks back
Disaggregated transferCaches from prefill to decode nodesSeparates compute-heavy phases
Common KV cache offloading strategies

Open-source serving stacks such as vLLM and companion libraries provide CPU and disk connectors, while some deployments add a distributed cache layer so that any node in a GPU cluster can fetch a prefix computed elsewhere.

Offloading is often combined with other techniques that reduce memory pressure, and each addresses a different part of the problem. The table below separates their effects.

TechniqueHow it saves GPU memoryMain cost
KV cache offloadingMoves blocks to slower tiersTransfer latency and bandwidth
KV cache quantizationStores values in 8 or 4 bitsPossible accuracy loss
PagedAttentionRemoves fragmentation wasteIndexing overhead, little else
Prompt cachingReuses computed prefixesNeeds storage, often offloading
RecomputationDiscards cache and rebuilds itExtra GPU compute per request
KV cache offloading compared with neighboring techniques

In practice, quantization and offloading stack well: a 4-bit cache moves four times fewer bytes than a 16-bit one, which directly shortens transfer time. Offloading becomes attractive whenever reloading is cheaper than recomputing, which is typically true for prefixes of several thousand tokens.

Configuration choices and performance trade-offs

Tuning an offloading setup means balancing time to first token, time per output token and overall goodput. The decision table below summarizes common situations.

SituationRecommended approachReason
Many idle multi-turn chatsOffload sessions to CPU DRAMResume is faster than full prefill
Shared long system promptPersist prefix on NVMe or cluster storeReuse across users and nodes
Strict per-token latency goalsKeep decoding cache on GPUTransfers would stall decoding
Short prompts, short answersSkip offloadingOverhead exceeds benefit
When to apply KV cache offloading

The most common failure is bandwidth saturation: if many requests reload large caches at once, the PCIe link becomes the bottleneck and latency spikes. Other pitfalls include pinned-memory exhaustion on the host, cache invalidation errors after model updates, and storing tenant data without proper isolation and encryption.

A plain-language summary

When a language model reads your text, it keeps working notes about every word. These notes sit in the graphics card’s memory, which is very fast but small.

With long documents or many users, the notes no longer fit. KV cache offloading moves some notes to a bigger, slower shelf, such as the computer’s main memory or a fast disk. When they are needed again, they come back. It is like moving older files from your desk to a nearby cabinet. The desk stays clear, and nothing has to be rewritten.

An example from practice

A company runs an internal assistant on a server with eight GPUs. Every request starts with the same 20,000-token policy manual as context, and employees often pause conversations for several minutes before asking follow-up questions.

Without offloading, the server recomputes the manual for each new session and evicts paused chats under load, so first responses take several seconds. The operations team enables prefix caching with CPU and NVMe offloading, assigning about 1 TB of host memory and a local SSD array to cached blocks.

After the change, most requests load the manual from host memory instead of recomputing it, paused chats resume without a fresh prefill, and the cluster serves noticeably more concurrent users. The team keeps active decoding entirely on the GPUs, so per-token latency stays unchanged.