FDEInterviews logo
★ Read firstSOSP 2023

Efficient Memory Management for Large Language Model Serving with PagedAttention

The Berkeley paper behind vLLM: manage the KV cache like operating-system virtual memory, in small pages allocated on demand, and serve 2 to 4 times more requests on the same GPU.

Breakdown by FDE Interviews · 13 min read · Updated

About the original · 2023
AUTHORS
Kwon, Li, Zhuang, Sheng, Zheng, Yu, Gonzalez, Zhang, Stoica
FROM
UC Berkeley, Stanford University, UC San Diego
PUBLISHED
Sep 2023 · SOSP 2023
CITED BY
9,202Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 23% of this breakdown.

01

The paper in one minute

When a language model generates text, it keeps the keys and values of every previous token for every layer: the KV cache. For a 13-billion-parameter model it takes about 800 KB per token, so a single 2,048-token request can need 1.6 GB. Serving throughput depends on how many requests you can batch together, and on a 40 GB A100 serving a 13B model, about 65% of memory holds the weights and about 30% is left for KV caches. How that 30% is managed decides how many requests run at once.

Woosuk Kwon, Zhuohan Li and colleagues at Berkeley (with Ion Stoica and Joseph Gonzalez) found that existing systems wasted most of it. Deep learning frameworks want tensors in contiguous memory, so servers reserved a contiguous chunk for each request sized to its maximum possible length, since output length is unknown in advance. Most of that space sat unused: reserved for tokens not yet generated, never used because the answer was short, or lost to fragmentation from the memory allocator. Only 20.4% to 38.2% of KV memory actually held token states.

PagedAttention borrows the operating system's answer to the same problem: virtual memory with paging. Each request's KV cache is split into blocks of a fixed number of tokens (16 by default). Blocks are allocated only when needed and can sit anywhere in GPU memory; a block table maps a request's logical blocks to physical ones, and the attention kernel reads keys and values through that table. Waste is limited to the unfilled part of each request's last block. Because blocks are referenced through tables, requests can share them: parallel samples from the same prompt share the prompt's blocks, beam-search candidates share common history, and requests with a common system prompt share its cache, with copy-on-write when one needs to diverge.

vLLM, the serving engine built around it, put 96.3% of KV memory to use, sustained 1.7 to 2.7 times the request rate of an idealized version of Orca, then the leading research system, and up to 22 times FasterTransformer at the same latency.

THE IDEA, VISUALLY
KV CACHE AS VIRTUAL MEMORY request A: logical blocks request B, same prompt: logical blocks p1 p2 a3 p1 p2 b3 → block table → → block table → physical blocks p2 a3 b3 p1 16 tokens per block; a new block only when the last one fills. Shared prompt blocks are stored once (reference count 2) and copied only if one sample must change them. No pre-reserved maximum length, so no internal fragmentation beyond the last block; equal-size blocks, so no external fragmentation. The attention kernel reads keys and values block by block via the table. OPT-13B: 800 KB of KV cache per token, up to 1.6 GB for one 2,048-token request MORE OF THE MEMORY HOLDS TOKENS share of KV cache memory storing actual token states (Fig. 2) existing systems, best case 38.2% existing systems, worst case 20.4% vLLM 96.3% SERVING THROUGHPUT, SAME LATENCY vs Orca (oracle lengths) 1.7–2.7× vs Orca (max length) 2.7–8× vs FasterTransformer up to 22× ShareGPT trace; beam search shares 44–66% of KV blocks. The paged attention kernel itself was 20–26% slower.
PagedAttention stores the KV cache in small blocks mapped through a table, like virtual memory. Almost all of the reserved memory then holds real tokens, so many more requests fit in a batch.
Research notes: the problem, approach and results
96.3%

of KV cache memory holding real token states in vLLM, against 20.4–38.2% in prior systems

2–4×

higher serving throughput than state-of-the-art systems at the same latency

800 KB

of KV cache per token for OPT-13B, up to 1.6 GB for one 2,048-token request

THE PROBLEM

LLM serving throughput depends on batch size, and batch size is limited by KV cache memory. Existing systems reserved contiguous space for each request's maximum length and used only 20% to 38% of it for real tokens.

THE IDEA

Split each request's KV cache into fixed-size blocks allocated on demand and mapped through a block table, like pages in an operating system, and let requests share blocks when their prefixes match.

THE RESULT

vLLM put 96.3% of KV memory to use, served 1.7–2.7× the request rate of an idealized Orca and up to 22× FasterTransformer at the same latency, with bigger gains for beam search and shared prefixes.

02

Why it matters to an FDE

vLLM became one of the default open-source serving engines, and the ideas here, paged KV cache, continuous batching and prefix sharing, are now standard across serving stacks. When a customer self-hosts a model, this paper explains most of what determines cost per request.

KV cache memory sets your capacity. Concurrency, and therefore throughput, is limited by how many requests' KV caches fit in GPU memory after the weights. Long prompts and long outputs consume it quickly. Capacity planning starts with KV bytes per token for the model and the customer's real length distribution.

Long contexts cost more than they look. Every token in context stays in the cache for the whole request. A customer who sends 30,000-token prompts can fit far fewer concurrent users than one who sends 2,000-token prompts.

Shared prefixes are free capacity. System prompts, few-shot examples and retrieved context reused across requests can share KV blocks. In the paper, sharing a five-example prefix gave 3.58× the throughput of the baseline. Keep shared instructions identical and at the start of the prompt so engines can reuse them.

Sampling several answers is cheaper than it seems. Parallel samples and beam search share the prompt's KV cache, saving up to two-thirds of memory in the paper's beam-search runs.

Know the knobs. vLLM exposes the share of GPU memory for KV cache, block size, maximum sequence length and preemption behavior. Setting the maximum model length to what the customer actually needs, rather than the model's maximum, can matter.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 49 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦3 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The memory problem, the paging design, scheduling and sharing, and the misreadings to avoid

    Free account
  2. 04How it works

    The block table, the scheduler, preemption, and how sharing and copy-on-write work

    Premium
  3. 05The numbers that matter

    Throughput results by workload, the setup, and the caveats in the comparisons

    Premium
  4. 06Try it yourself

    Simulate contiguous and paged KV allocation on a ShareGPT-like trace and count how many requests fit

    Premium
  5. 07Where it breaks

    The costs and limits of paging, and what later systems added

    Premium
  6. 08Using it on a deployment

    Sizing and tuning a self-hosted vLLM-style deployment for a customer

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →