Paged Attention and Prefix Caching

Topics Covered

The Fragmentation Problem

Contiguous allocation

Three kinds of waste

Why waste limits throughput

Why not just reserve less

The operating system analogy

Fragmentation in numbers

A day in the life of a contiguous cache

Why the problem grew with context length

Fragmentation in other systems

Measuring waste in your own system

The lesson for system design

Where the idea came from

The KV cache is the largest dynamic consumer of GPU memory in LLM serving, and how it is allocated decides how many sequences can run at once. Early serving systems allocated it the simple way, one contiguous region per request, and wasted most of it. Paged attention, introduced with vLLM in 2023, borrowed the idea of virtual memory paging from operating systems and changed what a GPU could serve. Prefix caching built on the same block structure to reuse work across requests. This lesson covers why contiguous allocation fails, how paged attention works, how prefix caching and sharing build on it, and how to reason about block sizes and cache hit rates. This first section explains the fragmentation problem.

Contiguous allocation

A simple engine allocates the KV cache for a request as one contiguous tensor sized for the maximum length the request might reach: prompt length plus maximum output tokens, or even the model maximum context. The attention kernel can then read the cache with simple indexing. The problem is that the engine does not know in advance how long the output will be. A request with a 500 token prompt and a 4,000 token maximum output reserves space for 4,500 tokens, but most chat answers end after a few hundred tokens. The rest of the reservation sits empty for the life of the request, unavailable to anyone else.

Three kinds of waste

The vLLM paper identified three sources of waste in contiguous allocation. Reservation waste is space held for tokens that have not been generated yet but will be. Internal fragmentation is space reserved for tokens that will never be generated, because the request ends before its maximum. External fragmentation is free memory split into gaps too small to hold a new request contiguous region, even though their total would be enough. The paper measured that existing systems used only twenty to forty percent of their KV cache memory for actual token states; the rest was waste of these three kinds.

Contiguous allocation reserves room for the longest possible answer. Most answers end early, and the rest of the reservation sits empty.

Why waste limits throughput

Every gigabyte of wasted cache is a gigabyte that could hold the context of more running sequences. Since decode throughput grows almost in proportion to batch size, a system that uses only a third of its cache for real tokens runs batches roughly a third the size it could, and produces roughly a third of the throughput. Fragmentation was therefore not a small inefficiency but one of the main reasons early LLM serving was expensive. Fixing it raised throughput by factors of two to four on the same hardware in the original measurements, before counting any other optimization.

Why not just reserve less

An engine could reserve only the prompt length plus a small margin and grow the region when needed. But growing a contiguous region means finding a larger free region and copying the whole cache into it, which is expensive and may fail if memory is fragmented. It could also guess output lengths, but guesses are often wrong, and a wrong guess either wastes memory or forces a costly reallocation. The fundamental problem is that contiguous allocation requires knowing the final size up front, and generation does not provide it.

The operating system analogy

Operating systems faced the same problem decades ago with process memory. Programs need memory that grows unpredictably, and requiring contiguous physical memory led to fragmentation. The solution was virtual memory with paging: memory is divided into fixed size pages, each process sees a contiguous virtual address space, and a page table maps virtual pages to whichever physical pages happen to be free. Pages are allocated only when touched. Paged attention applies exactly this idea to the KV cache, with blocks of tokens playing the role of pages.

Fragmentation in numbers

Imagine a cache of 40 gigabytes, enough for 320,000 tokens of a mid sized model, serving requests that reserve 8,000 tokens each but use 1,500 on average. Contiguous allocation fits 40 requests, holding 60,000 real tokens, under a fifth of capacity. Allocating only what is used fits over 200 requests of 1,500 tokens. The difference, a batch of 40 versus 200, translates almost directly into throughput and cost per token, which is why paged attention is considered one of the most important serving advances.

A day in the life of a contiguous cache

Picture a contiguous cache at the start of a busy hour. The first few requests each carve out large regions sized for their maximum output. As some finish, they leave holes of different sizes between still running requests. A new request arrives needing a region of 6,000 tokens. The total free memory is 20,000 tokens, but the largest single hole is 4,500, so the request cannot be admitted. It waits in the queue while the GPU runs a half empty batch. Meanwhile, most of the regions already handed out are themselves mostly empty, waiting for tokens that will never arrive. The operator sees high memory use and low throughput at the same time, a combination that looks paradoxical until the allocator is understood.

Some early systems tried to defragment by periodically compacting the cache, copying running sequences to close the gaps. Compaction helps external fragmentation but costs bandwidth and pauses the batch while copies run, and it does nothing about reservation waste or internal fragmentation. Others reserved smaller regions and preempted sequences that outgrew them, which traded memory waste for recomputation. None of these patches addressed the root cause, which was the requirement for contiguity itself.

Why the problem grew with context length

The waste from fragmentation grows with the gap between the maximum context and typical usage. When models supported 2,048 tokens, a worst case reservation was not much larger than a typical request. As context windows grew to 32,000, 128,000 and beyond, reserving for the maximum became absurd: a single 128,000 token reservation for a 70 billion parameter model would consume tens of gigabytes for a request that usually uses a few thousand tokens. Long context models made a paged design not just helpful but necessary.

Fragmentation in other systems

The same pattern appears wherever memory needs grow unpredictably. Databases allocate pages for tables and indexes, file systems allocate blocks for files, and memory allocators in language runtimes use size classes and pages. In each case, fixed size units with an indirection table won over contiguous allocation because they decouple logical layout from physical placement. Recognizing this pattern helps engineers reason about new allocation problems in serving, such as managing adapter weights or multimodal embeddings, where the same solution tends to apply.

Measuring waste in your own system

Waste is straightforward to measure even without changing the allocator. Log, for each request, the tokens it reserved and the tokens it actually used, and sum both over a busy period. The ratio of used to reserved tokens is the effective cache efficiency. Teams running older serving stacks who do this exercise often discover efficiencies of thirty percent or less, which tells them exactly how much batch size, and therefore throughput, a paged engine could recover. The same logs also reveal the output length distribution, which is useful later for capacity planning and for setting sensible maximum output limits. A short report of reserved versus used tokens per product surface is often enough to justify migrating to a modern engine.

The lesson for system design

The fragmentation story is a reminder that throughput problems are not always compute problems. Teams sometimes reach for faster GPUs or custom kernels when the real limit is how memory is handed out. Before optimizing arithmetic, it is worth asking how many sequences fit and why, since the answer often points to an allocation fix with a much larger payoff.

Where the idea came from

The vLLM authors at UC Berkeley noticed that serving throughput was bounded by memory long before compute, and that most of that memory was empty. Their insight was that the operating system community had solved this decades earlier, and that the attention kernel could be adapted to work through a page table with little cost. The paper, published at the SOSP conference in 2023, is short and readable, and worth studying for anyone who builds serving systems.

The batch size hides in the allocator

The memory allocator decides how many sequences fit, and the number of sequences decides throughput. A better allocator can be worth more than a faster kernel.