vLLM's KV cache manager: blocks, hashes and prefix caching
How vLLM at v0.34.0 hands out KV cache memory: blocks with reference counts, a free queue that doubles as the prefix cache's LRU list, chained block hashes with extra keys for LoRA and images, allocate_slots and get_computed_blocks, how the block size is chosen, and what the kv_cache_usage metric does and does not count. With exercises that predict cache behaviour from the code.
By Sandip Maity, Applied AI Engineer
Key takeaways
- Each KV block has an ID, a reference count and, once full, a hash. Blocks with a reference count of zero live in one free queue.
- Freed blocks keep their hash and stay in the free queue as cache entries. They are evicted lazily, only when the queue hands them out for new work.
- A block's hash covers its parent's hash, its tokens and extra keys such as the LoRA adapter, so a hit means the whole prefix matches.
- The free queue orders reuse: blocks that were never cached go to the front, cached ones to the back, which makes eviction least recently used.
- kv_cache_usage counts blocks held by requests. Cached blocks that nobody holds count as free.
Goal: understand exactly how vLLM allocates, shares and evicts KV cache blocks, so you can reason about prefix-cache hit rates, KV usage metrics and preemption from first principles, and change the code safely in the lab.
You need: a checkout of vLLM at v0.34.0. No GPU. Background: the scheduler, caching: prefix, semantic and KV and the original PagedAttention idea.
Honest limits. At v0.34.0 the KV cache code supports many layer types (sliding window, Mamba state, chunked local attention, cross-attention, sparse variants) through a coordinator and one manager per layer type. This lesson follows the common case: a decoder-only model where every layer uses full attention, so there is one KV cache group, handled by FullAttentionManager under a UnitaryKVCacheCoordinator. The project's own docs/design/prefix_caching.md and hybrid_kv_cache_manager.md cover the rest.
#The pieces
| Class | File | Job |
|---|---|---|
KVCacheManager |
vllm/v1/core/kv_cache_manager.py |
What the scheduler calls: get_computed_blocks(), allocate_slots(), free(), usage |
KVCacheCoordinator |
vllm/v1/core/kv_cache_coordinator.py |
Splits work across KV cache groups (one group in the common case) |
FullAttentionManager |
vllm/v1/core/single_type_kv_cache_manager.py |
Per-request block lists for one group, prefix-cache lookups |
BlockPool |
vllm/v1/core/block_pool.py |
All blocks, the free queue and the hash-to-block map |
KVCacheBlock, FreeKVCacheBlockQueue, hash_block_tokens |
vllm/v1/core/kv_cache_utils.py |
The block record, the free list, the hash function |
None of these touches GPU memory. They manage block IDs. The workers hold the actual KV tensors, and the block IDs in each step's SchedulerOutput tell them where to write and read.
Contiguous: reserve the maximum length (6 blocks) per sequence
Paged: blocks handed out as tokens arrive
Sequence A starts
Its first 16 tokens fill one block. Contiguous allocation (top) must reserve room for the maximum length up front.
Each cell is a 16-token block of KV memory. Colour = which sequence owns it; hatched = reserved but empty.
The rest is Pro
Keep reading the rest of this lesson
Pro unlocks the rest of this lesson, including 1 animated diagram, every advanced lesson, the hands-on labs and completion certificates.
One year
$25
365 days, no auto-renewal
Lifetime Best value
$75
pay once, keep it forever
7-day refund, no questions asked. One payment, no auto-renewal.Compare plansHave a licence key?
Not ready? Keep learning free
Free lessons from vLLM Internals, no sign-up. Or follow the free roadmap →
Not ready? Take the free 5-day email course
One short email a day for five days, each built on a free lesson, then the occasional new lesson or offer. Unsubscribe in one click.
Still to come in this lesson
- A block
- The free queue is the cache
- Hashing a block
- The two calls the scheduler makes
- Block size
- What kv_cache_usage means
- Exercises
- Where to go next
You will be able to answer
- A request finishes. What happens to its full blocks when prefix caching is on?
- Two prompts share their first 200 tokens and the block size is 64. How many blocks can the second prompt reuse?
- Why does each block's hash include its parent block's hash?
- The dashboard shows KV cache usage at 40%, yet the prefix cache hit rate is high and steady. What does usage measure?