Unit 6 of 11 · about 4 hours · explain, implement
Eviction, recomputation, preemption and terminal cleanup
A request can stop owning KV while the prefix cache retains it. Cleanup must distinguish safe reuse from a leak, and preemption must preserve enough history to resume correctly.
You'll be able to
- Implement distinct transitions for cache eviction, request preemption and terminal release.
- Resume a preempted request from retained token history without resampling its past.
- Prove cancellation is idempotent and cannot free another owner's state.
Covers
Read
In this order, only the parts listed
0 of 3 done
- PagedAttention: recovering preempted stateEfficient Memory Management for Large Language Model Serving with PagedAttention
arXiv:2309.06180v1, Section 4.5, Scheduling and Preemption; Section 7.3, Comparing Recomputation and Swapping
Read recomputation and swap as the paper's alternatives, not claims about today's vLLM scheduler defaults.
- Nano-vLLM: preempt and finishNano-vLLM - Block Manager and Sequence Lifecycle
preemptandpostprocess, including deallocation on end-of-sequence or output limit - Mini-SGLang: protection and leaf evictionMini-SGLang - Radix Cache and Cache Tests
lock_handle,evictand_collect_leave_nodes_for_evict; follow protected and evictable sizesTrace the state transitions. Add cancellation in your own model; neither small engine is a complete cancellation specification.
Focus on
- Eviction removes reusable index entries and makes unpinned storage reclaimable. Preemption pauses a live request and releases or moves its KV. Terminal cleanup removes request ownership permanently.
- Recomputation replays the original prompt plus committed generated tokens as inputs. It does not regenerate those tokens or advance the sampling RNG for them.
- A radix leaf may be evictable only when no active handle protects it. Remove descendants before ancestors so remaining prefix paths stay valid.
- Cancellation releases each owner once and invalidates future work for that request generation. Pending readers, writers or transfers must complete or acknowledge cancellation before their blocks can be overwritten.
Common mistakes · 3
- Calling every retained block a leak. A leak has no valid owner or bounded reclamation path; a cached prefix can legitimately outlive its request.
- Resampling old output during recomputation. The resumed history and sampling state then differ from the original request.
- Treating a duplicate cancellation or late callback as a second release. Guard terminal state and request generation before decrementing references.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Can a request be finished while a prefix derived from it remains cached?Answer
Yes. Request ownership has ended, but a reusable index can retain reclaimable state. It must remain bounded and evictable when unpinned.
2.Does preemption always reclaim every page in the request table?Answer
No. Other requests or pending operations may still pin shared pages. Release this request's references and count only newly reclaimable physical pages.
3.Which past tokens are fed to recomputation after a decode preemption?Answer
The prompt and already committed generated tokens, with their original positions and model state contract. Past sampling decisions are retained, not repeated.
You're done when
The history preserves surviving tokens, releases each owner exactly once, rejects stale events and recovers the full pool after retained entries are explicitly evicted.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 11 more in the section
- Efficient Memory Management for Large Language Model Serving with PagedAttentionvLLM Project · primary
Read the memory-waste motivation, block-table design, sharing and copy-on-write sections, then reproduce the logical-to-physical translation on paper. Separate the paper's reported workloads from results on your engine and hardware.
- Nano-vLLM - Block Manager and Sequence LifecycleGeeeekExplorer · required
Trace nanovllm/engine/block_manager.py, sequence.py, scheduler.py and attention.py. Draw allocate, append, share, preempt and free transitions, then implement randomized allocator tests in your own project before touching production-engine code.
- Mini-SGLang - Radix Cache and Cache TestsSGLang Project · required
Read docs/structures.md, the cache allocation code and tests/core/test_cache_allocate.py. Compare exact prefix identity and lifecycle with Nano-vLLM, then add tenant and version fields to your own prefix index.
Also in this section
- SGLang: Efficient Execution of Structured Language Model ProgramsSGLang Project · required
Study RadixAttention, longest-prefix matching, eviction and cache-aware scheduling. Compare its sequence-level radix index with vLLM's block-level paging instead of treating them as competing names for one mechanism.
- Hands-On LLM Serving and Optimization - Prefix Caching and Advanced KV CachingChi Wang and Peiheng Hu (O'Reilly) · required
Read "Prefix Caching" in Chapter 6 (RadixAttention, use cases, best practices, scaling the prefix cache) and "Advanced KV Caching" in Chapter 7 (long context, cost and latency, LMCache). Separate exact-reuse mechanisms from capacity extensions such as offload.
- AI Systems Performance Engineering - Chapter 18, tuning KV-cache utilizationChris Fregly (O'Reilly) · support
Read "Tuning KV Cache Utilization and Management" in Chapter 18 and "Prefix Caching" in Chapter 16. Leave the disaggregated pool and transfer sections for INF-7.
- vLLM Automatic Prefix Caching DesignvLLM Project · required
Check block hashing, extra hash keys (multimodal inputs, LoRA, cache salt), isolation and eviction against v0.31.0, the version pinned here; cache-key fields change between releases.
- SGLang documentation - Radix cache eviction and HiCacheSGLang Project · support
Use the radix eviction policy and session radix cache pages to compare eviction choices with vLLM, and the HiCache design page for a GPU, host and storage hierarchy.
- AI Performance Engineering - KV Cache SystemsWafer · reference
A curated index. Use Section 4 only to find a primary KV-cache source the other entries lack.
- 100 Days of LLM Inference - Days 5, 9, 10 and 16Elizabeth Thomas · support
Use the four notebooks for KV arithmetic, paged allocation, radix reuse and prompt-layout experiments. Re-run conclusions against your own property tests and workload because these notebooks are a supplementary learning journal.
- TensorTonic Language Modeling from Scratch - Find Reusable Prefixes in a KV TrieTensorTonic · support
Solve after the prefix-index unit and add the cases its public tests leave out to your own property tests.
- LLM Inference: From scratch, one token at a time - Modules 02 and 06Tushar Amrit · support
Use The KV Cache for fragmentation and reuse and Memory Optimization for paging, prefix sharing and KV compression. Work the checkpoints before the matching units.
- LLM Inference Systems Laboratory - KV StateRoadmap-Final · support
Implement the paged allocator and prefix index with differential, randomized, cancellation, collision, fork and pressure tests. Retain one reuse workload that improves hit rate but not goodput.
- vLLM 0.31.0 - cache specifications and connector lifecyclevLLM Project · reference
Read the per-layer attention, MLA and Mamba specifications for hybrid state contracts; read the V1 connector metadata, load synchronization and deferred-free methods for transfer ownership. These fill the pool's hybrid-state and connector-contract gaps, without requiring distributed execution.