AI Infra Interviews logo
LLM Inference & Serving / 04
medium★ EssentialNewvLLMTogether AIBaseten

How does PagedAttention work, and what problem was it solving?

Before vLLM, engines reserved the maximum context for every request up front and wasted most of it. The fix borrowed virtual memory from the operating system, and the numbers show how much capacity it returned.

Updated Sep 2026 · Grounded in real AI infrastructure interview loops and written to a senior-engineer editorial bar, with every number worked and every diagram hand-built.

TL;DR: PagedAttention stores each sequence's KV cache in fixed-size blocks (16 tokens each in vLLM) that need not be contiguous, with a per-sequence block table mapping logical positions to physical blocks, exactly as an OS maps virtual pages to frames. That removes the reservation waste of contiguous allocation, which can idle half of GPU memory, and it makes prefix sharing and copy-on-write a table edit instead of a memcpy.

How to approach it

State the problem before the mechanism: KV cache size is unknown at admission because output length is unknown, so a contiguous allocator must reserve for the worst case. Ask what the engine's max sequence length and typical output are, since those two numbers set the waste. Then describe the block table, do the fragmentation arithmetic for a concrete model, and finish with what non-contiguous storage buys beyond packing: shared prefixes, beam forks and cheap swap.

A strong answer

A typical situation: an engine reports its KV pool 95% allocated and serves 12 concurrent users on hardware that should hold 40. The memory is reserved for tokens that were never generated, and the fix came from operating systems rather than from machine learning.

A sequence's cache grows by one token per decode step, and nobody knows at admission how many steps it will take. Pre-vLLM engines handled that by allocating max_seq_len × per-token bytes as one contiguous slab per request. The waste has three parts: internal fragmentation (space reserved past where the sequence stops), external fragmentation (gaps between slabs of different sizes that no new request fits into), and reservation (memory held for a request that will never use it). A typical measurement in the vLLM paper (Kwon et al., SOSP 2023) found 60 to 80 percent of KV memory wasted this way.

inputs: Llama 3.1 70B bf16, KV per token = 328 KB, engine max_seq_len = 8,192
  a chat request: prompt 600 tokens, output 250 tokens, total used = 850 tokens

contiguous reservation = 8,192 × 328 KB ≈ 2.69 GB per request
actually used          =   850 × 328 KB ≈ 0.28 GB
waste per request      ≈ 2.41 GB, about 90% of the reservation

KV budget on 8 × H100 after weights ≈ 435 GB
  contiguous: 435 ÷ 2.69 ≈ 161 concurrent requests, most of the pool idle
  paged (16-token blocks, 5.2 MB each): 850 tokens → 54 blocks ≈ 0.28 GB
             435 ÷ 0.28 ≈ 1,550 concurrent requests of this shape
sanity: the usable concurrency rose close to 10x, which matches the throughput
        gains the paper reports on short-output workloads

The mechanism is the OS page table. Physical GPU memory is carved into blocks of 16 tokens' worth of K and V for one layer (vLLM keeps one block pool per layer, or a layered layout, depending on version). Each sequence owns a block table: logical block 0 maps to physical block 731, logical block 1 to 12, and so on. The attention kernel takes the table, gathers the right physical blocks for each query, and computes scores across them. Only the last block is partially filled, so internal waste is at most 15 tokens per sequence, and there is no external fragmentation because every block is the same size.

PAGED ATTENTION (toggle allocation)
37 real tokens, 35 wasted
The KV cache must hold every sequence's keys and values. Contiguous allocation reserves a full max-length block per sequence, so unused slots (faded) are wasted and fragmentation caps how many fit. PagedAttention stores the cache in small blocks allocated on demand, packing memory and serving 4 sequences where contiguous fits 4.

The block table is what makes three other features cheap:

FeatureWithout pagingWith paging
Shared system prompt across N requestsN copies of the prefix KVone set of blocks, N tables point to it, refcount per block
Beam search or parallel sampling forkcopy the whole cache per branchshare blocks; copy-on-write only the block that diverges
Preemption under memory pressureevict whole slab, recomputeswap blocks to host memory or drop and recompute; both at block granularity

The prefix-sharing case matters most in production. A 2,000-token system prompt at 328 KB per token is 656 MB. Shared across 200 concurrent conversations that is 131 GB of duplicate cache under contiguous allocation and 656 MB under paging with refcounts. That is the memory side of Prefix Caching and KV Reuse; the compute side (skipping the prefill) comes for free once the blocks are addressable by content hash.

The costs are real but small. The gather adds indirection to the attention kernel, which is why the first vLLM kernels were slower per token than a contiguous FlashAttention path until FlashAttention 2 and FlashInfer added native paged support. Block size trades internal waste (large blocks) against table size and gather overhead (small blocks); 16 tokens is the common compromise, with 32 and 64 used on some kernels.

Decision: any engine serving variable-length requests should page. The engine's gpu-memory-utilization and its reported KV block count are where you watch this working. The reversal condition: a single-tenant, fixed-length, batch-1 deployment where contiguous storage and a plain FlashAttention kernel are marginally faster and waste nothing.

What interviewers probe next

  • "What is the OS analogy exactly?" Virtual pages are logical blocks, physical frames are pool blocks, the page table is the block table, and copy-on-write works the same way when a fork writes into a shared block.
  • "How does the engine decide it can admit a request?" It needs free blocks for ceil(prompt tokens ÷ 16) plus at least one block to grow into; vLLM also watermarks a small reserve so decode steps do not immediately starve.
  • "What happens when the pool runs dry mid-decode?" The scheduler preempts the lowest-priority sequence, either swapping its blocks to pinned host memory over PCIe or freeing them and recomputing the prefix later; both show as an ITL spike and a preemption counter.

Common mistakes

  • Describing PagedAttention as a compression technique; it changes nothing about bytes per token.
  • Forgetting that the attention kernel had to change, which is the engineering half of the paper.
  • Claiming zero waste; the last block of every sequence is partial.
  • Not connecting the block table to prefix sharing, which is the feature interviewers at SGLang-style shops care about most.

Key takeaways

  • Contiguous KV allocation reserves max_seq_len per request; at 8k max and 850 used, about 90% is wasted.
  • Paging uses 16-token blocks and a per-sequence block table; internal waste falls to under one block per sequence.
  • The block table makes prefix sharing, copy-on-write forks and block-level swap cheap.
  • Admission needs ceil(prompt ÷ block size) + 1 free blocks; preemption counters reveal an undersized pool.
That one was free — and so are 10 answers per topic without an account. Signing in doubles that to 20, opens the Plus lessons in the courses, and remembers which topics you keep getting wrong.no card · Google sign-in · nothing to cancel
HOW DID IT GO?
0
READING SIGNED OUT

Signing in doubles your free answers, from 10 to 20 per topic, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free

The concepts behind this question

Ranked by how closely each one overlaps this question's topic, so the first card is the thing to read if the answer above moved too fast.

Advanced
🚀 Inference & Serving🔒 Premium
PagedAttentionPagedAttention stores the KV cache in fixed-size blocks scattered across HBM and maps each sequence's logical positions to physical blocks through a block table, the same trick an operating system uses for virtual memory. It removes the reservation and fragmentation waste of contiguous allocation, lets blocks be shared between sequences, and is why an engine can decide admission by counting free blocks.
Foundational
🚀 Inference & Serving
The KV CacheThe KV cache stores each token's attention keys and values so decode never recomputes them, turning a quadratic cost into a linear one at the price of memory that grows with every token in every concurrent sequence. Its size, 128 KB per token for Llama 3.1 8B and 320 KB for 70B in bf16, is what caps concurrency and context on a given GPU, so it decides batch size, replica count and whether a model fits at all.
Core
🚀 Inference & ServingSign in
Continuous BatchingContinuous batching schedules at the granularity of a single decode step instead of a whole request, so a finished sequence's slot is refilled on the next iteration rather than when the longest request in the batch ends. It is the scheduling idea that turned LLM serving from a padded, half-idle GPU into one that stays full, and it decides how the engine's scheduler, memory manager and latency SLOs interact.
Core
📐 AI Systems DesignSign in
GPU Job Scheduler DesignDesign a scheduler for a shared GPU cluster is the most common design prompt in AI infrastructure interviews, because it touches everything: queues and priorities, gang placement, topology, fairness across teams, preemption and the checkpoints that make it survivable, and the failure handling that keeps a 512-GPU job alive. This page builds the design in layers, states the data model and the scheduling loop, derives the numbers (how long a job waits, how much preemption costs, how much fragmentation wastes), and lists the trade-offs the interviewer will push on.
UP NEXT ON YOUR JOURNEY
FEDITOR'S NOTE

Scored on the fragmentation arithmetic and on knowing what the block table enables beyond packing: sharing, copy-on-write and cheap preemption.

DISCUSSION · 0

No comments yet — be the first to share your approach.