Explain PagedAttention and how vLLM eliminates KV cache memory fragmentation
Written by the mlprep team · Updated
What problem does PagedAttention solve in LLM serving? Walk me through how KV cache memory was wasted before vLLM, how virtual-memory paging fixes it, and what new optimizations paging unlocks.
Why naive KV cache management wastes 60%–80% of GPU memory#
Standard attention implementations expect a sequence's KV cache to sit in a contiguous tensor in GPU HBM. Because you don't know how long an LLM response will be ahead of time, pre-vLLM serving engines had to pre-allocate a contiguous slice sized for max_sequence_length (e.g., 4,096 tokens) for every request.
This causes three kinds of severe memory waste:
- Internal fragmentation: A request allocated 4,096 slots finishes at 180 tokens — 95% of its reserved KV memory sat empty for its entire lifetime.
- Reservation waste: Even for a request that eventually reaches 4,096 tokens, slots 500–4,096 sit unused while it is still generating token 100.
- External fragmentation: Different requests with different max lengths leave unusable gaps between contiguous allocations, preventing new requests from fitting even when total free memory is sufficient.
Because batch size is bounded by available KV cache memory, wasting 70% of KV memory directly cuts maximum batch size and throughput by 3× or more.
How PagedAttention works#
PagedAttention borrows the exact idea of virtual memory and paging from operating systems:
- Logical vs. physical KV blocks: Instead of one giant contiguous tensor per request, divide each sequence's KV cache into small, fixed-size blocks (e.g., 16 tokens per block).
- Block table: Each request maintains a lightweight
block_tablemapping logical block index0, 1, 2, ...to arbitrary non-contiguous physical blocks in a pre-allocated GPU memory pool. - On-demand allocation: A request only allocates a new physical block when its current block fills up. When the request finishes, its physical blocks immediately return to the global free list.
- Custom paged attention kernel: During attention, the GPU kernel reads the
block_table, gathers K and V tiles from non-contiguous physical blocks on the fly, and computes attention without ever copying them into a contiguous buffer.
Internal fragmentation drops to at most one partially filled block per sequence (e.g., fewer than 16 tokens — typically under 4% waste), and external fragmentation drops to zero because all physical blocks are identical in size.
Bonus capability: zero-copy memory sharing (Copy-on-Write)#
Because multiple logical block tables can point to the same physical KV blocks with reference counts:
- Beam search & parallel sampling (
n > 1): All candidate branches share the prompt's KV blocks (and shared prefix blocks) with zero duplication. When branches diverge on the final block, a Copy-on-Write copies only that single 16-token block. - Shared system prompts / prefix caching: Requests sharing the same system prompt point to the same physical blocks.
What is the trade-off in choosing the block size (e.g., 16 vs. 64 vs. 256 tokens), and does non-contiguous memory access hurt GPU attention kernel speed?
Block size trade-offs#
- Too small (e.g., 1–4 tokens): Minimal internal fragmentation, but huge block tables, poor GPU memory coalescing, and lower kernel efficiency because each memory fetch loads tiny scattered chunks.
- Too large (e.g., 128–256 tokens): Great contiguous reads within a block, but worse internal fragmentation on short outputs and coarser prefix sharing (two prompts must match on full 256-token boundaries to share a block).
- Sweet spot (typically 16 or 32 tokens): Small enough to keep fragmentation under a few percent and allow fine-grained prefix sharing, while large enough for vectorized HBM memory loads and efficient warp-level tiling in CUDA/Triton kernels.
Does non-contiguous lookup slow down attention?#
There is a modest overhead compared to fully contiguous FlashAttention (extra indirection through the block table and slightly more complex tiling), typically a few percent on a single sequence. However, at the system level, eliminating 60%–80% memory waste allows 2×–4× larger batch sizes, which overwhelmingly dominates throughput and goodput. Modern FlashAttention-2/3 and FlashInfer also natively support paged KV layouts, closing nearly all of the kernel gap.
Senior/staff considerations#
- PagedAttention does not shrink the theoretical KV cache size of a single token — you still combine it with GQA/MLA and FP8 KV cache quantization to reduce bytes per token.
- When GPU KV blocks run out mid-generation (because all running requests grew simultaneously), vLLM must preempt a sequence — choosing between recomputation (better for low-cost prefills) and CPU swap (better when PCIe bandwidth beats recomputing huge contexts).
Interview red flags#
- Confusing PagedAttention with FlashAttention: FlashAttention optimizes SRAM tiling during attention computation (avoiding materializing the N×N attention matrix in HBM), while PagedAttention optimizes HBM memory management for the KV cache across requests. Production engines use both together.
- Claiming PagedAttention compresses or approximates the KV cache — it is 100% lossless and mathematically identical to standard attention.
Go deeper: How KV caching works covers what is stored, and how much memory it costs.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
PagedAttention applies OS virtual-memory paging to LLM KV caches: splitting each request's KV cache into fixed-size blocks stored non-contiguously in GPU HBM via a block table. It eliminates external fragmentation, cuts internal waste to under one block per request, enables copy-on-write sharing for parallel sampling and shared prefixes, and dramatically increases achievable batch size.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How do FlashAttention and PagedAttention complement each other inside a modern serving engine?
- In beam search with width 4, how much KV memory does copy-on-write paging save compared to naive duplication?
- What happens to scheduling latency if block size is too small under 100k-token contexts?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
How does prefix caching (RadixAttention) speed up multi-turn and agentic LLM workloads?
How would you design an LLM serving system for millions of users?
How do you serve hundreds of fine-tuned LoRA adapters on one base model efficiently?
How does continuous batching work and why does static batching waste GPU utilization?