Zenaique

How a prefix cache hit changes the work that chunked prefill has to do

Short answer·Medium·4.0 · 0·~3 min·Asked atFlowiseKrutrimShopify·Relevant atMicrosoftNVIDIASglang
Attempt it

A continuous batching scheduler (vLLM / SGLang) supports both prefix caching and chunked prefill. What happens to a new request whose first 5,000 tokens hit the prefix cache, when the total prompt is 7,000 tokens and the chunk size is 1,024? Explain how the two optimizations stack within one scheduler iteration.

Free · 2 AI evals / day
TL;DR

The prefix cache hit skips prefill for the first 5,000 tokens. Chunked prefill then splits the 2,000-token tail into 1,024 and 976 token chunks, each batched with other requests' decode steps.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Picture a teacher who needs to read a 7-page essay before grading. With prefix caching, she realizes she already read the first 5 pages last week and has notes. She attaches those notes and starts fresh on page 6. Now she has 2 pages to read. Chunked prefill is her policy of never sitting down for a long uninterrupted read. She reads roughly a page (1,024 tokens), then briefly checks in with five other students' work in progress, then reads the rest of the second page, then checks in again. Every student gets a steady stream of attention instead of waiting an hour while she finishes one essay.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Prefix caching and chunked prefill show why serving stacks schedule around continuous batching. Each targets a different problem (redundant prefix recomputation, and head-of-line blocking from large prefills), and they combine without conflict because they act on different parts of the work. The first reduces how much prefill work exists. The second controls how the remaining prefill work is scheduled relative to other in-flight requests.

In this scenario, a new request arrives with a 7,000-token prompt. The scheduler matches its prefix against the cached KV blocks and finds a 5,000-token hit; those blocks already exist in HBM from prior requests. The scheduler reference-counts and attaches them. The remaining 2,000-token tail enters chunked prefill, which under a 1,024-token chunk size splits it into chunks of 1,024 and 976. Each chunk runs with other requests' decode steps in a single batched forward pass. From the third iteration on, the new request decodes one token per iteration like everyone else.

Prefix caching: how the cache hit happens

In SGLang, the prefix cache is a radix tree whose edges hold runs of tokens and point to their KV tensors (RadixAttention); matching works at token granularity. vLLM instead keys each full 16-token KV block by a hash of its parent block's hash and its tokens, and caches only full blocks. When a new request arrives, the scheduler matches the request's prompt tokens as far as it can. Each matched block already lives in HBM from prior requests.

The match length in this scenario is 5,000 tokens. The scheduler reference-counts the matched blocks (so they cannot be evicted while this request needs them) and attaches them as the prefix of the new request's KV cache. The cost is hashing or tree lookup plus pointer updates, small next to the prefill it skips.

The important property is that the cached blocks contain the exact K and V tensors that running prefill on those tokens would have computed. They are not approximations or summaries; they are the real KV state. The model can attend over them during later decoding without quality loss.

Eviction is the other half. When HBM fills, the cache must release blocks. Reference counts protect in-flight requests. Among unreferenced blocks, vLLM evicts in LRU order from a free queue, and SGLang evicts radix-tree leaves first in LRU order, so a shared prefix outlives the unique tails that branch from it.

Chunked prefill: how the tail gets scheduled
Why the two compose without interference
Tuning and failure modes
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • SGLang's RadixAttention (Zheng et al., NeurIPS 2024) combines prefix caching with continuous batching and is a widely used reference design for this combination.
  • vLLM's V1 engine enables both automatic prefix caching and chunked prefill by default.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QHow does the scheduler decide which prefix-cache blocks to evict when HBM pressure rises?
A

Reference counting plus LRU on unreferenced blocks. Blocks attached to in-flight requests can't be evicted. vLLM keeps unreferenced blocks in an LRU free queue; SGLang evicts radix-tree leaves first in LRU order, so shared prefixes near the root (such as system prompts) stay longest.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Believing the cache hit only speeds up prefill for the hit region; it skips that prefill entirely. Or believing chunked prefill helps the cached portion; it only shapes the remaining work.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • How prefix caching attaches existing KV blocks instead of recomputing prefill

  • Why chunked prefill exists (head of line blocking from large prefills)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy