PagedAttention Explained: How vLLM Ends KV Cache Waste

PagedAttention Explained: How vLLM Ends KV Cache Waste

What if the biggest bottleneck in running a large language model had almost nothing to do with the math and almost everything to do with bookkeeping? That is the surprising lesson behind PagedAttention, an attention algorithm that borrows one of the oldest tricks in operating systems and applies it to the GPU memory that holds a model’s conversation state. The idea landed in the 2023 paper “Efficient Memory Management for Large Language Model Serving with PagedAttention” and became the engine inside vLLM, one of the most widely used open-source serving systems. This article breaks down what PagedAttention actually does, why the memory problem it solves was so expensive, and where the technique stops helping.

The memory problem hiding behind every chatbot

When a transformer model generates text, it does not recompute everything from scratch for each new token. Instead it stores the key-value (KV) cache: the intermediate attention keys and values for every token it has already processed. That cache is what makes streaming responses feel fast, because the model only needs to compute attention for the newest token. (Attention (machine learning)))

The catch is size. The KV cache grows with every token in the conversation, and it lives in precious GPU memory. Older serving systems handled this by reserving a contiguous chunk of memory per request, sized for the maximum sequence length the model might produce. Most requests never reach that maximum. The result was memory set aside and never used, plus gaps left behind as requests of different lengths came and went.

The authors of the PagedAttention paper put the waste at roughly 60 to 80 percent of KV cache memory in existing systems, split between fragmentation and over-reservation. In practice, that means a GPU could be technically “full” while most of its cache memory sat idle. Fewer requests fit at once, throughput drops, and operators end up buying more hardware to serve the same traffic.

What PagedAttention actually changes

What PagedAttention actually changes

Here is the key thing to understand: PagedAttention does not change the attention math itself. The queries, keys, and values are still multiplied and softmaxed the same way. What changes is where the KV cache lives and how the system finds it.

Instead of one contiguous block per sequence, PagedAttention splits the cache into fixed-size blocks, each holding the keys and values for a small number of tokens. Blocks can sit anywhere in GPU memory, in any order. A per-sequence block table maps logical block numbers (block 0, block 1, block 2 of this conversation) to physical block locations in memory. When the model needs to attend to a token, it looks up the block through the table rather than assuming the data is laid out sequentially.

If that sounds familiar, it should. This is essentially the same design as virtual memory in an operating system, where a page table translates the addresses a program uses into physical locations in RAM. The analogy is deliberate and it is the reason for the “Paged” in the name. Readers who want the fuller picture of the operating-system concept can explore how virtual memory and page tables work, since the paper leans on that model directly.

Two practical consequences follow from this design:

  • On-demand allocation. Memory is handed out block by block as tokens are generated, rather than reserved up front for a worst-case length. A short request only ever occupies a few blocks.

  • Near-zero fragmentation. Because blocks are fixed size and non-contiguous, the memory manager never has to find a large contiguous gap. It just needs free blocks, wherever they are.

The result is that KV cache memory usage tracks actual sequence length closely, which is exactly what you want when request lengths vary wildly, as they do in real chat and API traffic.

Memory sharing and copy-on-write

The block table provides a second benefit that is easy to miss: memory sharing. Because sequences reference blocks indirectly, multiple requests can point at the same physical block.

Consider a system prompt or a long shared instruction that dozens of users send at once. Under the old model, each request would store its own copy of those keys and values. With PagedAttention, the shared prefix can occupy one set of physical blocks that many block tables reference. When a sequence needs to write a new token into a block that is shared, the system copies that block first, so the other sequences are untouched. This is copy-on-write, again mirroring how operating systems handle forked processes.

The paper reports that this sharing produces the largest gains in workloads with heavy prefix reuse, such as few-shot prompting, multi-turn chat with a fixed system message, and parallel sampling where several outputs branch from one prompt. The more sharing a workload has, the more the technique pays off.

How much faster does it actually get?

The headline claim from the paper is that vLLM, the serving system built around PagedAttention, reaches roughly 2 to 4 times higher throughput than prior state-of-the-art systems such as FasterTransformer and Orca, at comparable latency. The gains grow with longer sequences and with more prefix sharing, because those are the conditions where KV cache waste was worst.

Aspect Traditional contiguous KV cache PagedAttention
Allocation Reserved for max length up front Allocated block by block on demand
Memory layout One contiguous region per sequence Fixed-size blocks anywhere in GPU memory
Fragmentation High, from gaps and over-reservation Low, because blocks are uniform
Prefix sharing Each request keeps its own copy Shared blocks via block table and copy-on-write
Typical waste reported 60 to 80 percent of cache memory Small, tied mostly to the last partial block

It is worth being precise about what these numbers mean. A 2 to 4 times throughput figure is a serving-system result, measured under specific hardware and workload conditions in the paper’s evaluation. It is not a claim that the model itself computes faster, and it is not a universal multiplier you should expect on every deployment. Treat it as evidence that memory management, not raw FLOPs, was the binding constraint.

The block size trade-off

Nothing here is free, and block size is the clearest example. Smaller blocks mean less internal waste in the final partial block of a sequence, but they also mean more entries in the block table and more work for the attention kernel that has to gather data from scattered locations. Larger blocks reduce table overhead but waste more space at the tail of each sequence. The paper evaluates this trade-off directly, and vLLM has historically defaulted to a modest block size measured in tokens. Because defaults can change between versions, anyone tuning a deployment should check the current vLLM documentation rather than assuming a value from an older blog post. (Blog)

This is one of the most common practical mistakes. Teams read that PagedAttention “eliminates waste” and then configure block sizes casually, or benchmark once and never revisit. In reality, block size interacts with sequence length distribution, batch size, and the GPU’s memory bandwidth. A workload of short chatbot turns and a workload of long document summaries do not want identical settings.

Where PagedAttention fits among serving optimizations

PagedAttention is a memory-management technique, and it is often confused with the other optimizations it gets deployed alongside. Keeping them straight helps:

  • Continuous batching schedules new requests into a running batch as slots free up, rather than waiting for a whole batch to finish. PagedAttention makes this far more practical because memory can be added and released per sequence.

  • Prefix caching persists shared prompt blocks across requests so they are computed once. It is a natural extension of the sharing mechanism PagedAttention introduced.

  • Quantization shrinks the precision of weights and cache entries to save memory. It is orthogonal: you can quantize a paged cache.

The pattern is that PagedAttention is an enabler. It removes a memory constraint that made other techniques awkward, which is why it spread quickly through the inference stack. For readers tracking how these serving layers evolve in production infrastructure, the cloud coverage at 9481.org follows the operational side of that shift, and the news section at 9481.org tracks related industry announcements.

Practical guidance for people deploying it

If you are running a model server and wondering whether any of this matters to you, a few habits separate smooth deployments from painful ones.

First, measure your actual waste before optimizing. Log KV cache utilization and the distribution of sequence lengths. If nearly all your requests are short and uniform, the fragmentation problem is smaller and the gains will be modest. If you have a long tail of very long conversations mixed with quick one-shot queries, the benefit is large.

Second, watch the scheduler as well as the kernel. PagedAttention changes memory behavior, and the throughput you observe results from memory management and batching policy together. Tuning one while ignoring the other leaves performance on the table. (Text generation strategies)

Third, be careful with benchmarks that report only tokens per second at maximum load. Latency at realistic concurrency matters more for user experience, and the paper’s own comparisons hold latency roughly constant precisely because raw throughput alone can mislead.

Fourth, do not conflate the algorithm with the implementation. PagedAttention is the idea; vLLM is the specific open-source system that popularized it. Other engines have adopted similar paged KV cache designs, and details like block tables, eviction policy, and swapping behavior differ between them. When you read a performance claim, check which system and which version produced it.

Finally, plan for the cache as a first-class resource. Many teams size GPU memory around model weights and treat the KV cache as whatever is left over. PagedAttention rewards the opposite mindset: the cache is where concurrency lives, so it deserves explicit budgeting.

Conclusion

PagedAttention is one of those ideas that looks obvious in hindsight. The KV cache was ballooning, systems were reserving memory they never used, and the fix was to stop demanding contiguity and start using a block table, exactly as operating systems have done for decades. By allocating cache blocks on demand and letting sequences share physical blocks through copy-on-write, the technique turned a memory-management problem into a throughput win of roughly 2 to 4 times in the paper’s evaluation, with the biggest gains in long-sequence and prefix-heavy workloads.

The nuance worth carrying away is that PagedAttention is not magic and not a model improvement. It is careful engineering applied to the least glamorous part of inference: where the bytes go. Teams that understand the block size trade-off, measure their real memory waste, and treat the KV cache as a resource to be budgeted will get the most from it. Those who assume the defaults are optimal, or who confuse the algorithm with the engine that implements it, will wonder why their numbers do not match the paper. The lesson generalizes well beyond this one technique: in large language model serving, the smartest optimization is often the one that stops wasting what you already paid for.

Frequently Asked Questions

What is PagedAttention in simple terms?

PagedAttention is a way to store a language model’s key-value cache in small fixed-size blocks scattered across GPU memory instead of one big contiguous chunk per request. A block table records where each block lives, so the system can allocate memory only as tokens are generated. It is the same idea as paging in an operating system.

Does PagedAttention make the attention calculation itself faster?

No. The attention math is unchanged. PagedAttention changes how the KV cache is stored and located, which reduces wasted memory and lets more requests run at once. The speedup comes from better memory utilization and higher concurrency, not from cheaper arithmetic per token.

How much throughput improvement does the paper report?

The original paper reports that vLLM, the system built on PagedAttention, achieves roughly 2 to 4 times higher throughput than earlier serving systems like FasterTransformer and Orca at similar latency. The gains are larger for longer sequences and for workloads with lots of shared prompt prefixes. Your own results will depend on hardware, model, and traffic shape.

What is the difference between PagedAttention and vLLM?

PagedAttention is the algorithm and memory-management design. vLLM is the open-source serving engine that implements it. People often use the two names interchangeably, but the distinction matters when you are comparing performance claims or reading about other engines that adopted similar paged cache designs.

Why does block size matter so much?

Block size controls a trade-off. Small blocks waste less space at the end of a sequence but require bigger block tables and more complex attention kernels. Large blocks simplify bookkeeping but waste more memory per sequence. The right value depends on your sequence length distribution, so check the current defaults rather than assuming an old figure.

Can PagedAttention be combined with other optimizations?

Yes, and it usually is. It pairs naturally with continuous batching, which slots new requests into a running batch, and with prefix caching, which reuses shared prompt blocks across requests. Quantization is also compatible because it shrinks the data stored in each block. These techniques address different constraints and stack well together.

Latest articles

Scroll to Top