Retrieval caches: validate the model, corpus, and access dependencies
Distinguish embedding, retrieval, context, and answer caches, with version-aware keys and explicit freshness and revocation behavior.
On this page
The question for today
What you’ll understand
- Identify dependencies at each retrieval-generation cache layer.
- Distinguish cache-key equality from current authorization and evidence validity.
- Plan corpus and ACL invalidation without assuming TTLs establish correctness.
A cache can reuse a costly embedding, a ranked identity list, a packed context, or a completed answer. Those objects depend on different versions and permissions. Reusing one under an incomplete key can produce a fast but stale or unauthorized result. Name the cached artifact, its dependencies, and its serving contract before optimizing the hit rate.
Different cache layers need different identities
A document embedding depends on source content, preprocessing, and the embedding model artifact. A retrieval result additionally depends on query representation, index generation, retrieval configuration, filters, and principal scope. A packed context depends on selected source versions, order, rendering, tokenizer, and packing policy. A generated answer depends on those inputs plus the generation model, prompt, decoding configuration, and any tools or conversational state affecting the result. Store these dependencies explicitly rather than reusing one query-only key at every stage.
Canonicalize key fields carefully and retain enough structured metadata to detect incompatible reuse. A content hash can identify immutable text but does not imply matching permissions, model behavior, or source authority. A model alias that changes behind an unchanged name can invalidate reproducibility assumptions. Some dependencies can be represented by a conservative pipeline version or corpus generation, at the cost of more misses. Finer invalidation can improve reuse only when its dependency tracking is complete and its compatibility rules are tested.
Version misses and authoritative rechecks address different problems
A matching key is a candidate for reuse, not proof that current permission remains valid. Validate the artifact's source dependencies and required access policy before delivery to each consumer. A changed ACL can revoke access without changing document text, while a changed corpus can alter the top results even for a query whose previous result did not mention the updated document. Invalidation based only on old result membership can therefore miss newly relevant additions or ranking changes. Negative-result caches need updates too.
A TTL bounds age only under its actual enforcement rules; it does not establish correctness until expiration or immediate revocation. Event-based invalidation can be delayed or lost, so use an authoritative version or validation mechanism that meets the serving contract. Distinguish immutable historical snapshots, which may remain valid for an authorized historical request, from answers claiming to reflect the current corpus. HTTP cache directives in RFC 9111 have specific semantics; an application-level RAG cache still needs its own source and permission dependency design.
Measure valid reuse and test stale paths
Test a content edit, new relevant document, source deletion, permission-only change, embedding-model replacement, prompt update, and generator change. Include caches populated by another principal and in-flight invalidation races. Verify that citation resolution and derived summaries follow the same snapshot and policy rules as answer serving. Report valid-hit rate separately from raw key matches, alongside stale-response and authorization failures. A lower hit rate after correct invalidation can be the intended outcome rather than a performance regression.
The cache lab demonstrates only deterministic LRU replacement. Reset, set C=2 and s=4, predict zero hits among sixteen initial requests, then Play or Step. Separately Reset, set C=4 and s=4, predict twelve initial hits, then Play or Step. Both cycle over four addresses, and only the larger capacity holds them all. The instrument has no corpus generations, ACLs, model versions, TTLs, or invalidation events. Use it for capacity-versus-working-set intuition while testing dependency validity in the actual pipeline.
Work it through
A matching query is not a matching answer artifact
One cached answer has dependency tuple (corpus=7, access-scope=4, generator=2), with all other fields fixed. Four requests require tuples [(7,4,2),(8,4,2),(7,5,2),(7,4,3)]. Use exact-version matching for this teaching cache.
Only the first request has the same tuple. The second changes corpus generation, the third access scope, and the fourth generator version.
There is one candidate key match among four requests, or 25%; the other three are dependency misses even though the query text is identical.
The matching request still requires the stated authoritative permission and provenance validation before serving. A candidate match is not automatically a valid hit or proof that the cached evidence remains accessible.
The example has a 25% candidate-match rate under exact-version keys. Valid reuse adds the required current checks.
The living notebook
Explore the model
Interactive lab · cache
Capacity hits do not validate a cache artifact
Reset, set C=2 and s=4, predict zero initial hits in 16 requests, then Play or Step. Separately Reset, set C=4 and s=4, predict 12 initial hits, then Play or Step. The modulo-16 LRU model has no corpus, access scope, versions, TTL, or invalidation channel.
- Hits / requests
- 0 / 16
- Hit rate
- 0%
- Address-stream period
- 16 addresses
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 forOptimize only reuse that satisfies the artifact's version and permission contract.
Pause & reason
Check your understanding
Your turn
Try it yourself
An answer cache has a 300-second TTL. A source permission is revoked thirty seconds after insertion. If the application relies only on TTL and serves the entry until expiry, how long can that stale-permission window remain?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Subtract elapsed age from the TTL; then distinguish a duration bound from authorization validity.
Compare with the solution
The remaining interval is 300−30=270 seconds. This hypothetical behavior does not meet an immediate-revocation contract merely because it has a finite bound. Apply the actual permission consistency and validation policy; TTL age is a different condition from source access.
Keep the idea
Take it with you
- Embedding, retrieval, context, and answer caches have different dependencies.
- Matching keys and unexpired TTLs do not establish current authorization.
- Corpus additions and negative results require invalidation beyond old-result membership.
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.