Hands-On Engineering Playbook · vLLM & PagedAttention 0 of 6 labs explored

How PagedAttention Stops Wasting 70% of Your GPU Memory

Serving a large language model in production is rarely bottlenecked by math speed. It is bottlenecked by how you pack short-lived conversation memory into expensive GPU RAM. This guide breaks down the vLLM paper into five interactive labs—no advanced math required.

The Core Mental Model: The Banquet Table Problem Imagine a restaurant where customers never tell you how long they will stay. Traditional LLM servers reserve an entire 2,048-seat banquet table for every single diner, just in case. Most customers leave after 50 bites, locking up 97% of the seats. PagedAttention replaces banquet tables with small, flexible 16-seat tables linked by a clipboard—the exact same virtual memory trick operating systems have used since the 1960s.

0Why a 30% Slice of GPU RAM Decides Your Serving Throughput

Generating text happens in two phases. First, the prompt phase reads your entire input at once using fast parallel matrix math. Second, the generation phase predicts one new word (token) at a time. To avoid re-reading earlier words on every step, the GPU stores a scratchpad of previous key and value vectors called the KV cache.

Because model weights are locked in memory and never shrink, the remaining slice left for the KV cache strictly limits how many users you can batch together. Pick a real hardware setup from the vLLM benchmarks below to see how much room is left for user requests.

GPU Memory Layout — 13B Model on 1× A100 (40 GB) 15.7K max KV slots
Static Model Weights (locked) Dynamic KV Cache Pool (determines batch size) Activations & Overhead
Contiguous Serving (Orca Max)
7 reqs

Pre-allocates 2,048 tokens per request; only ~20.4% of KV space stores real tokens.

PagedAttention (vLLM)
30 reqs

96.3% effective KV cache utilization—fitting roughly 3×–4× more concurrent users.

Throughput Gain
4.3×

Same GPUs, same weights—just zero wasted memory between requests.

1Fragmentation vs. Paging: See Where the 70% Goes

Deep learning frameworks normally expect each tensor to sit in one unbroken, contiguous line of memory. Because a server cannot predict when a chatbot will emit an end-of-sequence token, older systems pre-allocate a chunk big enough for the worst case. That creates three distinct leaks:

Used Token State Reserved Future Slots Internal Frag (Wasted) External Frag (Unusable Gap) Free Pool Slot
Traditional Server (Contiguous Chunks) 0% effective

64 total GPU KV slots. Each request locks a contiguous slice up to its max limit (16 or 32 slots).

  • Active requests running0
  • Blocked in queue (no contiguous fit)0
  • Wasted / locked slots0 / 64
vLLM PagedAttention (4-Token Blocks) 0% effective

Same 64 GPU KV slots, split into 16 blocks of 4 tokens. New blocks are allocated only as tokens arrive.

  • Active requests running0
  • Blocked in queue0
  • Wasted tail slots (max 3 per req)0 / 64
Click + Add Incoming Request or Generate +1 Token above to watch contiguous chunks trap free memory while PagedAttention packs every block tightly.

2Logical-to-Physical Translation: Inside the Block Table

In 1970, computer scientist Peter Denning summarized how operating systems solve fragmentation: give every program a clean, continuous list of logical addresses, and let a lookup table—the page table—map each piece to wherever free space actually lives in physical RAM.

vLLM brings this exact pattern to attention. Your prompt and output fill Logical KV Blocks strictly from left to right. Behind the scenes, the Block Table maps each logical block to any free Physical KV Block on the GPU. Step through the paper's exact example below.

Prompt loaded (7 tokens)
Current Operation Prompt: “Four score and seven years ago our” (7 tokens) fills Logical Block 0 (4/4) and Logical Block 1 (3/4). Click Generate Next Token to predict token 8 (“fathers”).
1. Logical KV Blocks What the model sees

Tip: Click any filled token above to trace how the PagedAttention kernel reads its history across scattered blocks.

2. Block Table The translator
Logical Physical # Filled
Click a row in the Block Table or a token on the left to inspect its physical address translation.
3. Physical GPU DRAM Actual memory

Why Block Size = 16 Is the Sweet Spot

If blocks are tiny (1–2 tokens), the GPU wastes time hopping through block table lookups instead of reading memory in big parallel bursts. If blocks are huge (128–256 tokens), short prompts leave most of the block empty—bringing back internal fragmentation. Slide the block size below to see how latency reacts across short and long workloads.

Normalized Request Latency by Block Size (Paper Figure 18b)
Block size 16 achieves optimal GPU parallelism while keeping tail waste under 15 tokens per request.

3Share Prompts, Save Memory: Copy-on-Write in Action

Because every request reads memory through a Block Table, two different requests can point to the exact same Physical KV Blocks without knowing it. Think of two developers branching off the same starter repository: neither duplicates the shared files unless they edit a line.

Each physical block tracks a reference count. When two samples share a block (ref_count = 2) and Sample A1 needs to append a different word, vLLM uses Copy-on-Write (CoW): it copies just that one block into a free physical slot, lowers the old reference count to 1, and lets Sample A2 write directly to the original block with zero extra copying.

Decoding Scenario
Step 0 of 3
Step-by-Step Guide Prompt “Four score and seven years ago our” is loaded once into Physical Blocks 7 and 1. Click the blue button above to generate two independent completions from this single prompt.
Logical Sequences (Branching Views) 0% memory saved
Physical GPU Blocks & Reference Counts 2 physical blocks used
  • Without sharing (naive copy)4 blocks
  • With PagedAttention + CoW2 blocks
  • Paper real-world savings16.2%–30.5% (Parallel Sampling)

4When GPU Memory Runs Out: Swapping vs. Recomputation

Even with zero fragmentation, a sudden traffic spike or runaway conversation can fill 100% of your physical KV blocks. When that happens, vLLM uses a First-Come, First-Served (FCFS) rule: it pauses the newest request so older requests can finish and free their blocks.

Because every token in a sequence needs all previous tokens to continue, vLLM evicts all or none of a request's blocks. Once space clears up, it can recover that paused request in two ways: Swapping the blocks to CPU RAM over PCIe, or Recomputing the KV cache in one fast parallel prompt pass. Test how block size changes the winner below.

Cluster State: Healthy Requests A (3 blocks) and B (3 blocks) are running comfortably on an 8-block GPU. Click 1. Trigger Traffic Spike when Request C arrives and needs 3 blocks!
GPU KV Pool (8 Blocks Max) 6 / 8 used
Recovery Channel
  • PathIdle
  • Overhead0 ms
  • CPU RAM Needed0 blocks

Waiting for preemption event.

CPU RAM Swap Pool 0 swapped
Recovery Latency: Swapping vs. Recomputation (Paper Figure 19a)
Notice how Swapping explodes at small block sizes due to tiny PCIe copies, while Recomputation stays flat (~20 ms) across every block size.

5The Bottom Line: 2×–4× Higher Throughput in Production

By packing 96.3% of KV memory with real tokens, sharing prompt blocks via Copy-on-Write, and recovering gracefully under spikes, vLLM fits dramatically larger batches on the same GPUs. Explore the paper's measured batch capacity and check your intuition below.

Benchmark Workload (OPT-13B on 1× A100)
Average Concurrent Batched Requests — ShareGPT Trace (Paper Figure 13)
Higher is better: fitting more requests into the same 12 GB KV cache budget directly multiplies token/s throughput.

Check Your Intuition: 4 Real-World Scenarios

Test what you have explored. Click the best engineering choice for each production scenario.

Plain-English Cheat Sheet

Paper Term Plain-English Meaning Why It Matters in Production
KV Cache Saved intermediate memory of earlier tokens in a conversation. Takes ~30%+ of GPU RAM and grows unpredictably with every word generated.
Logical vs. Physical Block Continuous sequence order vs. scattered fixed-size chunks in GPU RAM. Eliminates pre-allocation waste and external fragmentation.
Block Table A lightweight index mapping each sequence's logical blocks to GPU block IDs. Lets the attention kernel read scattered memory as if it were contiguous.
Copy-on-Write (CoW) Sharing physical blocks until a branch writes a different token, then copying one block. Cuts memory up to 55% in beam search and speeds up shared system prompts by 3.58×.
Recomputation vs. Swap Drop KV cache and re-run prompt math vs. offload blocks to CPU RAM over PCIe. Recomputation beats swapping at small block sizes and stays predictable (~20 ms).