advanced29 min studyLesson 703 in Engineering

Prefix reuse needs identical state, explicit ownership, and a trust boundary

Construct a cache identity that covers the effective prefix, count shared physical blocks once, and distinguish active references from evictable retained cache entries.

On this page

What you’ll understand

  • Explain why matching block tokens alone do not identify equivalent KV state.
  • Calculate physical sharing savings and active reference counts.
  • Specify cache isolation and reclamation boundaries without promising complete side-channel protection.

Two requests with the same compatible prefix can avoid recomputing and duplicating its KV state. The word compatible carries the correctness contract: weights, positions, adapters, multimodal inputs, and relevant execution semantics must agree. Sharing also creates an ownership problem because finishing one request cannot invalidate blocks still used by another. Build cache identity, reference tracking, and tenant isolation together so a performance optimization does not turn into stale attention state, cross-request mutation, or an unintended way to probe another user's prompts.

The same visible text is not a complete KV identity

KV values depend on the causal prefix and model state, not just the tokens inside the current block. Identical local tokens after different earlier text can produce different keys and values. Use an effective identity covering the model revision or isolated model namespace, adapter configuration, token IDs, positions and attention semantics, and any nontext inputs that affect embeddings. Two images can share placeholder token IDs while containing different image features. Tokenizer and prompt-template changes can also alter the effective sequence even when a user sees similar text.

The vLLM 0.9.1 V1 design chains each full block's identity through a parent hash and includes block tokens and extra identities such as adapters, multimodal inputs, and isolation salts. Its documented prefix cache stores full blocks, so a shared prefix ending partway through a block does not automatically yield reuse of that partial block. A digest has collision assumptions; a stronger hash reduces practical collision risk rather than logically proving equality for all possible inputs. Pin the implementation and namespace policy rather than presenting one hash tuple as a universal sufficient key for every architecture.

kj=H(kj−1, tokens⁡j, ej)k_j=H(k_{j-1},\,\operatorname{tokens}_j,\,e_j)
Within an isolated compatible model/execution namespace, k_j is a chained block identity and e_j covers relevant extra state. The first-block seed can incorporate a trust-group salt. This conceptual digest construction has no units and needs a declared collision policy; it does not replace exact state-compatibility requirements or authorization.↔ Scroll wide equations horizontally.

Count physical blocks once and keep active references alive

Each request holds a logical list of physical blocks. A block shared by four active requests consumes one physical allocation while its active request-reference count is four. Finishing one request drops that count to three, not zero. Zero references can make a completed full block eligible for an idle cache, where it remains reusable until its storage is reclaimed. Thus active, cached but evictable, and physically unused are different categories. A preallocated pool may keep the same device-byte footprint while occupancy moves among all three states.

Reference counts must change consistently with allocation, cache lookup, cancellation, and completion. Protect pending kernels and transfers separately where their lifetime extends beyond a request's logical state transition; a numerical zero in a request counter alone does not prove that no device operation can read the bytes. In the cited V1 design, cache hits are touched before allocating replacement blocks so useful zero-reference hits are removed from the free queue before they could be evicted. Correct sharing requires both state equivalence and a safe ownership protocol.

rb=∑q∈Qactive1{b∈Rq},Mphysical=∣⋃qRq∣mBr_b=\sum_{q\in\mathcal Q_{\rm active}}\mathbf1\{b\in R_q\},\qquad M_{\rm physical}=\left|\bigcup_q R_q\right|m_B
R_q is the set of physical blocks referenced by request q, with no repeated reference to one block within that request in this simplified layout; m_B is bytes per equal-size block. r_b counts active requests and M counts their unique physical payload in bytes. Idle cached blocks, metadata, and additional in-flight holds require separate accounting.↔ Scroll wide equations horizontally.

Sharing scope is also an information-flow policy

Cache-hit latency can reveal whether a prefix was previously processed. Reuse within a trust group may be useful while reuse across unrelated tenants is inappropriate. The cited vLLM V1 design supports a request salt propagated through the chained key; salts must be assigned and checked consistently with the service's intended trust boundaries. Letting an untrusted caller choose another tenant's isolation value defeats that policy. Isolation reduces the relevant reuse channel but does not establish complete privacy across scheduling, network timing, logs, or every other possible side channel.

The cache lab isolates identity reuse, without ownership or security. Reset, explicitly set BOTH capacity C=4 and stride s=4, predict twelve hits at the initial sixteen requests, then Play or Step. Reset and re-set BOTH C=4 and s=2; predict zero hits because eight cyclic identities exceed capacity four. N=min(4096,16+floor(2t)); edits retain t and recompute from empty. A hit here does not verify model compatibility, a tenant boundary, a live reference, or any saved prefill computation. The experiment only shows how repeated identity and capacity jointly affect a bounded lookup.

Count a shared prefix without counting its bytes four times

Four active requests each reference the same three full prefix blocks and two private blocks. All blocks have equal payload size, and no other cached or in-flight blocks are included in this example.

  1. Without sharing, four copies of five blocks would require 4×5=20 physical blocks. With sharing, the prefix needs three blocks and private suffixes need 4×2=8, totaling eleven.

  2. The saving is 20−11=9 blocks, or 9/20=45 percent of that unshared payload. Each shared prefix block has active request-reference count four.

  3. When one request finishes, each prefix count falls to three and its two private blocks can enter reclamation after outstanding accesses finish. The shared prefix cannot be recycled while the remaining requests still reference it.

The result

Logical request lengths and physical memory diverge under sharing, and request completion releases ownership rather than blindly freeing every referenced block.

Explore the model

Interactive lab

Interactive lab · cache

Repeated identities help only within the cache's capacity

For EACH run Reset, set BOTH C and s, predict, then Play or Step. At t=0/N=16 compare (4,4), twelve hits, with (4,2), zero hits. N=min(4096,16+floor(2t)); edits retain t/recompute from empty. This LRU trace has no prefix equivalence, reference counts, GPU work, tenants, salts, or security guarantees.

Repeated identities help only within the cache's capacityA fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Current readings: Hits / requests: 0 / 16; Hit rate: 0%; Address-stream period: 16 addresses. All parameters are adjustable with the labeled controls below.Fully associative LRU cache after 16 deterministic requests: 0 hits and 16 misses.Most recent 16 requestsaddress = i × 3 mod 160M3M6M9M12M15M2M5M8M11M14M1M4M7M10M13MCurrent cache order · oldest → newest471013Latest address 13 · miss: inserted as newestToy address stream · period 16 · starts empty
Cached addressMiss / evictionMost recent request
Hits / requests
0 / 16
Hit rate
0%
Address-stream period
16 addresses
t = 0 s

A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA reusable identity is only one part of prefix sharing; compatibility, ownership, and isolation need additional contracts.

Check your understanding

Two requests contain the same current block tokens but have different earlier prefixes. Why is reusing that block's KV state generally unsafe?

Try it yourself

Three requests share four full prefix blocks and each owns one private block. Calculate unique active blocks and the saving from unshared storage. If two requests finish, what is each shared block's remaining active count?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Count shared blocks once and private blocks once per request.

Compare with the solution

Unique active count is 4+3=7 blocks versus 3×5=15 without sharing, saving eight blocks. After two requests finish, each shared prefix block has active request-reference count one. It remains protected for the final request, with any additional asynchronous holds handled separately.

Take it with you

  • Prefix identity covers the effective causal state, not only visible local tokens.
  • Active references, evictable cached content, and unused pool slots are different states.
  • Cache isolation must follow the service's trust boundaries and does not prove complete privacy.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.