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

Eviction, recomputation and preemptionCancellation and terminal cleanup
1

Read

In this order, only the parts listed

0 of 3 done

  1. 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.

  2. Nano-vLLM: preempt and finishNano-vLLM - Block Manager and Sequence Lifecycle

    preempt and postprocess, including deallocation on end-of-sequence or output limit

  3. Mini-SGLang: protection and leaf evictionMini-SGLang - Radix Cache and Cache Tests

    lock_handle, evict and _collect_leave_nodes_for_evict; follow protected and evictable sizes

    Trace the state transitions. Add cancellation in your own model; neither small engine is a complete cancellation specification.

2

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.
3

Try it yourself

Closed-book, before checking anything

Draw and execute a deterministic CPU history: allocate a request, fork it, cancel one branch twice, preempt the survivor, recompute its committed token history, then finish it. Keep a separate token-list oracle and an ownership ledger after each event. With prefix retention enabled, request references must end at zero while retained blocks remain reclaimable; explicitly evict them and require full pool recovery. Inject a late completion from the cancelled generation and reject it without releasing storage again.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 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. 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