Article
Notes on Popular LLM Inference Engines
On this page 15
Intro
These are popular open-source frameworks I’ve been using for a while. Here are my notes on a few of their key design choices.
vllm
Architecture and Request Flow
The system has two familiar parts, a frontend and a backend.
- The frontend handles API requests, tokenizes their inputs, and passes the work to the backend in batches.
- The backend schedules the work in each batch and manages the runtime. Its components include:
- scheduler
- kv cache manager
- model runner
Client
│
▼
API Server
│ tokenize / request processing
▼
Engine Core
│
├── Scheduler
│
├── KV Cache Manager
│
└── Model Executor / GPU Worker
│
▼
GPU
The request flow looks roughly like this:
Frontend
API request
│
▼
tokenize / request processing
│
▼
Backend Engine
│
▼
Scheduler
│
├── consult KV Cache Manager
│ ├── Which KV entries already exist
│ ├── Which blocks can be reused
│ └── How much more KV space can be allocated
│
▼
Scheduled mixed batch
A: 128
B: 1
C: 1
│
▼
Model Runner
│
▼
Attention Backend
│
├── prefill workload
│ ↓
│ prefill kernel
│
└── decode workload
↓
decode kernel
│
▼
Write new KV entries into the cache
│
▼
logits / sampling
│
▼
Generate a new token
│
├── Request still running → Return to the scheduler for the next iteration
│
└── Output token → Frontend → Stream to the user
Scheduler
overview
The scheduler allocates compute at the granularity of one model forward pass.
A forward pass processes part of a sequence so the model can predict the next token. If part of the prompt is missing from the KV cache, the engine needs to prefill it, which takes more compute. During decode, it can reuse cached KV entries for the previous tokens.
On a full prefix cache hit, the engine still recomputes the last prompt position. It can reuse the first positions, but it needs the hidden state at position to predict the next token, and the KV cache does not store that hidden state.
The vLLM scheduler can make this decision based on the number of tokens that still need computation.
The scheduler works with the KV cache manager to determine how many tokens a request has already computed and how many it still needs to compute.
Two constraints govern scheduling:
KV cache capacityis limited by available GPU memory.Token budgetlimits how many tokens the engine can process per iteration. A larger user-configured budget can increase throughput at the cost of latency.
Within these constraints, vLLM maintains two queues:
- Waiting Queue holds requests awaiting their first execution or waiting to resume after preemption.
- Running Queue holds requests admitted for inference.
This resembles the queues that Linux maintains for process scheduling.
A request in the running queue may get no compute in a particular iteration, much like a runnable process that has been switched off the CPU.
Scheduling Mechanism
A scheduling mechanism defines how to organize and execute the work. As noted above, scheduling happens at the iteration level, with one batch computed each iteration.
The simplest approach is static batching.

As the figure shows, B and C finish early. After that, each iteration computes only one token, so utilization drops. Meanwhile, the long request A keeps its batch slot.
Continuous Batching
Continuous batching fills a slot as soon as a request finishes, improving utilization.

Chunked Prefill
If three very long prefills arrive and take up the available capacity, decode work can stall.
Splitting a long prefill into chunks has two benefits:
- Long prefills interfere less with decode latency.
- Decode is generally memory bound and prefill is generally compute bound. Combining the two can make better use of both resources.
The lower-level prefill and decode kernels are outside the scope of these notes.
Scheduling Policy
An inference engine has finite resources, so it needs a policy for deciding which requests to serve first.
The policy has to balance metrics such as time to first token (TTFT), time per output token (TPOT), throughput, and end-to-end latency.
A service level objective (SLO) specifies the service targets. The scheduler can then be tuned to meet them.
Scheduling Implementation
kv cache manager
Paged Cache
This is one of vLLM’s best-known designs. It borrows the idea of paging from virtual memory.
A simple KV cache implementation stores each sequence contiguously in GPU memory. This requires reserving space for each sequence up front and handling growth as the sequence gets longer.
With paging, the engine divides the KV cache into pages. Each page maps to a physical block that holds a fixed number of tokens, for example 16. A block table indexes the blocks much as a page table does in virtual memory.
Paging avoids having to reserve a large contiguous region and reduces fragmentation.
A KV block is also independent of any particular request and tied to its context. That makes it possible for different requests to share cached entries for a common prefix.
Paged Attention
Paging stores the cache in separate blocks. The original attention computation assumes that the K and V entries for a query are contiguous.
Paged attention therefore needs kernels that can address the blocks indirectly.
Reflection Table
Splitting the cache into blocks makes physical storage easier. A page-table-like mapping still has to tell each request which physical blocks hold its logical blocks.
The simplest representation looks like a dictionary:
request_to_blocks = {
"A": [7, 21, 4],
"B": [7, 21, 12],
}
A mapping entry lasts until its request finishes decoding. The cache manager removes the mapping and manages the physical blocks separately.
prefix cache
Physical blocks can also be shared across conversations with the same prefix. The KV entries match when the token context is the same.
Strictly speaking, the input to layer 0 is a special case where matching local tokens may suffice.
A hash table records the dependency on the prefix.
# ② Mapping from prefix hashes to physical blocks
cached_block_hash_to_block = {
H0: Block(7),
H1: Block(21),
H2: Block(4),
H3: Block(12),
}
The table chains hashes to build this mapping. Here is an example with [A,B,C,D,E,F,G,H,I,J,K,L]: 
Allocation and Eviction
A prefix miss or a new prefill needs fresh blocks. The Free_block_Queue tracks the blocks available for allocation.
A block is free when its
ref_cntis zero, meaning no active request uses it.A block can therefore appear in both the free queue and the cache hash map.
When the cache fills, the manager has to evict cached blocks. The cache trades space for time, and LRU is a straightforward starting point for an eviction policy.
Hicache
If GPU memory is full, the cache can spill into system RAM and then onto disk. Each tier is larger and slower than the previous one, much like the layers of a CPU cache. This is a hierarchical cache.
Eviction can move a block down one tier. On a prefix hit, the system can bring that block back up and evict another block to make room.
Eg
Here is a complete example.
0.bg:
Suppose request R has already finished. Its prefix cache contains:
a0 a1 a2 a3 | a4 a5 a6 a7
Meanwhile, request B is still running:
b0 b1 b2 b3 | b4 b5 b6 b7 | b8
The Transformer has already computed the first eight tokens of B. Token b8 was sampled in the previous iteration, so its KV entries have yet to be computed.
The physical cache now looks like this: 
Here is the state of the CPU-side tables introduced earlier:
# Mapping from requests to blocks
req_to_blocks = {
"B": [8, 3]
}
# Free blocks
free_block_queue = [
6, 1, 7, 2, 5, ...
]
And here is the hash table:
| Block Hash | Physical Block | Ref Count |
|---|---|---|
| H_{a0} | 2 | 0 |
| H_{a1} | 5 | 0 |
| H_{b0} | 8 | 1 |
| H_{ab1} | 3 | 1 |
1. New Request comin
A new request arrives:
Request A:
[a0,a1,a2,a3,a4,a5,a6,a7,a8,a9]
The scheduler first puts it in the waiting queue.
Running Queue:
B
Waiting Queue:
A
2.Allocate Computing Resource
Request B already has KV entries for eight tokens, but b8 has yet to go through the Transformer.
So:
num_computed_tokens["B"] = 8num_needed_tokens["B"] = 1
The scheduler asks the KV cache manager for a KV slot for one more token.
Block 3 is full, so the manager allocates a new physical block, Block 6.
The mapping becomes:
req_to_blocks["B"] = [8, 3, 6]
Block 6 is reserved for b8’s KV entries. Those entries have yet to be written.
3. The Scheduler Looks Up A’s Prefix Cache
A’s token blocks are:
Block 0: [a0,a1,a2,a3]
Block 1: [a4,a5,a6,a7]
Block 2: [a8,a9] # Partial block
The hash lookup finds that the first two blocks can be reused. The scheduler allocates free block 1 for the remaining two new tokens:
req_to_blocks = {
"B": [8, 3, 6],
"A": [2, 5, 1],
}
4. Scheduler Output
The scheduled work is:
num_scheduled_tokens = {
"B": 1,
"A": 2,
}
4.2 The Model Runner Prepares the Batch
input_ids = [
b8, # Request B
a8, a9, # Request A
]
4.3 The Model Runner Prepares Batch Metadata
| Field | Value |
|---|---|
| request_ids | [B, A] |
| input_ids | [b8, a8, a9] |
| query_start_loc | [0, 1, 3] |
| positions | [8, 8, 9] |
| seq_lens | [9, 10] |
| block_table | [[8,3,6], [2,5,1]] |
| slot_mapping | [24, 4, 5] |
The batch now contains all the inputs the kernels need to run.
sglang
kv cache manager
Radix Tree
vLLM uses chained hashes to index its blocks. SGLang uses a radix tree to store shared cached prefixes.
When evicting cache entries, SGLang applies a policy such as LRU to unlocked leaf nodes.