Real architecture · Staff
vLLM / PagedAttention (SOSP 2023)
Real production architecture. Key points: paged KV cache, memory fragmentation, continuous batching, and prefix sharing.
Short answer
Real production architecture. Key points: paged KV cache, memory fragmentation, continuous batching, and prefix sharing. Serving a large language model is expensive: according to recent estimates cited in the paper, processing an LLM request can cost ten times more than a traditional keyword query.
01 · THE PROBLEM
The KV cache dominates memory in LLM serving
Serving a large language model is expensive: according to recent estimates cited in the paper, processing an LLM request can cost ten times more than a traditional keyword query. For this reason, increasing throughput, and with it reducing the custo per request, is becoming more important.
Throughput can be raised by grouping several requests into a batch. However, the number of requests that fit together in the batch is still limited by GPU memory, above all by the space for the KV cache, and so the system's throughput is memory-bound.
In the paper's example, a 13B-parameter LLM on an NVIDIA A100 GPU with 40GB reserves about 65% of memory for the weights, which stay static during serving, and close to 30% for the requests' dynamic states. For a Transformer, these states are the attention keys and values, the KV cache.
The KV cache per token is large. In OPT-13B it takes 800 KB per token, and since the model generates sequences of up to 2048 tokens, a single request can need up to 1.6 GB. Even dedicating all available memory to the KV cache, only a few dozen requests would fit.
Two characteristics make this cache hard to manage: it grows and shrinks dynamically as the model generates tokens, and its lifetime and size are not known in advance.
02 · HOW IT WORKED BEFORE
Contiguous reservation and iteration-level scheduling
Earlier systems store each request's KV cache in a contiguous tensor, because most deep learning frameworks require tensors in contiguous memory. Since the output is unpredictable, they statically allocate a chunk based on the maximum possible sequence length, whatever the actual size of the input or output.
This creates three sources of waste: slots reserved for future tokens, internal fragmentation from oversizing for the maximum length, and external fragmentation from the memory allocator, such as the buddy allocator. The whole chunk stays reserved for the life of the request, and shorter requests cannot use the unused part.
In the paper's measurements, only between 20.4% and 38.2% of KV cache memory held actual token states in the existing systems.
There are also fine-grained scheduling techniques, such as iteration-level scheduling, that operate per iteration: after each one, finished requests leave the batch and new ones enter. This way, a new request waits only one iteration, not the whole batch. The paper also describes that vLLM selects the candidate sequences for the batch at each decoding iteration.
The request arrives through the frontend at the scheduler. With iteration-level scheduling, finished requests leave the batch and new ones enter at each iteration.
For each request, the system reserves in GPU memory a contiguous chunk sized by the maximum possible sequence length, whatever the actual size.
While the request lives, the whole chunk stays reserved and shorter requests cannot use the part that has not been used yet.
03 · PAGEDATTENTION
Virtual memory for the KV cache
PagedAttention divides each request's KV cache into blocks, and each block stores the keys and values of a fixed number of tokens. These blocks can sit in non-contiguous physical memory. The paper's analogy is operating-system virtual memory: blocks play the role of pages, tokens that of bytes, and requests that of processes.
The KV cache manager maintains block tables, which link each request's logical blocks to physical blocks. Logical blocks are filled from left to right, and separating logical from physical allows the cache to grow on demand, without reserving all positions up front. According to the paper, this eliminates most of the waste in existing systems.
vLLM is a complete serving system, with a FastAPI frontend and a GPU inference engine. Its centralized scheduler coordinates the execution of the distributed GPU workers, and the KV cache manager takes care of physical memory through the instructions it receives from the scheduler.
In prefill, the manager reserves only the blocks the prompt needs, not the maximum length. In the paper's example, a seven-token prompt uses two logical blocks, mapped to two physical blocks.
In the first decoding step, the cache of the new token fits in the last logical block, which still had a free position.
In the next step the last block is full: the manager allocates a new physical block and records the mapping in the block table.
At each step, the scheduler prepares a message with the token IDs and the block table of each request and sends it to the workers, which read the KV cache according to that table.
Because a new physical block is allocated only when all the previous ones are full, vLLM limits each request's waste to one block. When a request finishes, its blocks are released for other requests. The paper adds that relatively small blocks allocated on demand alleviate internal fragmentation, and that equal-sized blocks eliminate external fragmentation.
| Criterion | Contiguous reservation (earlier systems) | Paged blocks (vLLM) |
|---|---|---|
| Reservation per request | Chunk sized by the maximum possible sequence length | Only the needed blocks, allocated on demand |
| Internal fragmentation | Comes from oversizing for the maximum length | Alleviated by relatively small blocks allocated on demand |
| External fragmentation | Comes from the memory allocator, such as the buddy allocator | Eliminated, since all blocks are the same size |
| Compartilhamento | Not possible, since each sequence's cache sits in separate contiguous spaces | Possible at block granularity, across sequences of the same request or of different requests |
Block size is a design choice. If it is too small, vLLM may not exploit all the GPU's parallelism when reading and processing the KV cache; if it is too large, internal fragmentation increases and the chance of sharing decreases. The paper adopts 16 as the default, finding that value large enough to use the GPU well and small enough to avoid significant internal fragmentation in most workloads.
05 · SCHEDULING AND SCALE
Preemption, swapping to CPU, and multiple GPUs
When traffic exceeds the system's capacity, vLLM needs to prioritize requests and adopts the FCFS (first-come, first-served) policy for all requests, which guarantees fairness and avoids starvation. When preempting, the oldest requests are served first and the most recent ones are evicted first. As requests and outputs grow, vLLM may run out of physical GPU blocks for the new KV cache.
When blocks run out, it is necessary to decide which blocks to evict and how to recover them. Because all blocks of a sequence are accessed together, vLLM uses all-or-nothing eviction: either all blocks of a sequence are evicted, or none. Sequences of the same request, such as the candidates of a beam search, are scheduled as a group and preempted or rescheduled together.
| Criterion | Swapping | Recomputation |
|---|---|---|
| What happens to the evicted cache | The evicted blocks are copied to CPU memory | The KV cache is recomputed when the preempted sequence is rescheduled |
| How the sequence comes back | The blocks come back from CPU memory when a request finishes and releases its own | The generated tokens are concatenated to the original prompt and the cache for all positions is produced in a single iteration of the prompt phase |
The paper states that the performance of each technique depends on the bandwidth between CPU RAM and GPU memory and on the GPU's compute power. In swapping, the swap space in CPU RAM is limited by the GPU memory allocated to the KV cache.
For models too large for one GPU, vLLM supports Megatron-LM-style tensor parallelism. There is a single KV cache manager inside the centralized scheduler, and the workers share that manager and the logical-to-physical block mapping. Each worker has the same physical block IDs, but stores only the part of the KV cache for its own attention heads. This way, the workers do not need to synchronize memory management: they receive that information at the start of each decoding iteration, along with the step's inputs.
06 · RESULTS AND LIMITS
What the paper measured, and what it does not
The paper states that vLLM improves the throughput of popular LLMs by 2 to 4 times, at the same level of latency, compared with systems such as FasterTransformer and Orca. The improvement is larger with longer sequences, larger models, and more complex decoding algorithms.
On the ShareGPT dataset, vLLM sustained request rates 1.7× to 2.7× higher than Orca (Oracle) and 2.7× to 8× higher than Orca (Max), with similar latencies. On OPT-13B, it processed 2.2× more requests at the same time than Orca (Oracle) and 4.3× more than Orca (Max).
Orca (Oracle) assumes it knows the actual output length, an upper bound that is infeasible in practice; Orca (Max) always reserves space up to the model's maximum sequence length.
| Scenario | Result in the paper |
|---|---|
| Memory savings from sharing (OPT-13B, Alpaca trace) | 6.1% to 9.8% in parallel sampling and 37.6% to 55.2% in beam search |
| Shared prefix with one example (LLaMA-13B, translation) | 1.67× the throughput of Orca (Oracle) |
| Shared prefix with 5 examples (LLaMA-13B, translation) | 3.58× the throughput of Orca (Oracle) |
There are limits to consider. First, Orca was not publicly available, so the authors implemented their own version, and the variants differ in how much extra they reserve for the output. Second, on OPT-175B with the Alpaca dataset, which has short sequences, the advantage over Orca (Oracle) and Orca (Pow2) was less pronounced: there was memory left over for the KV cache, and performance became compute-bound rather than memory-bound. Third, in the chatbot test the KV cache is not kept between conversation turns.
Frequently asked questions
What is vLLM / PagedAttention (SOSP 2023)?
Real production architecture. Key points: paged KV cache, memory fragmentation, continuous batching, and prefix sharing.
What does the vLLM / PagedAttention (SOSP 2023) design cover?
It is organized in these sections: The KV cache dominates memory in LLM serving, Contiguous reservation and iteration-level scheduling, Virtual memory for the KV cache, Reference counting and copy-on-write, Preemption, swapping to CPU, and multiple GPUs, and What the paper measured, and what it does not.
How long is vLLM / PagedAttention (SOSP 2023) and what level is it?
It takes about 18 minutes to read and is rated Staff level.
Now design it yourself
Run this case as an interview in the System Design school: you make the decisions, the tutor challenges them and you see how your design scores.