RAG & Retrieval·Question 49 of 100Advanced

What caching strategies apply to RAG systems, and what are the invalidation traps?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Four cache layers, in order of increasing risk:

Embedding caches (safest): cache chunk embeddings keyed by content-hash + model-version - re-ingesting unchanged documents costs nothing; likewise cache query embeddings for repeated/popular queries. Invalidation is trivial (content hash changes with content; key includes model version so migrations can't mix vector spaces).

Full worked answer with diagram

Four cache layers, in order of increasing risk:

Embedding caches (safest): cache chunk embeddings keyed by content-hash + model-version - re-ingesting unchanged documents costs nothing; likewise cache query embeddings for repeated/popular queries. Invalidation is trivial (content hash changes with content; key includes model version so migrations can't mix vector spaces).

Retrieval-result caches: cache (normalized query → top-k chunk IDs) with a short TTL. Helps for head queries - most products have a Zipfian query distribution where the top few hundred queries are a large traffic share. Traps: must be keyed by the permission context (tenant + user entitlements), not just query text, or user A's result set leaks scope to user B; and it must be invalidated (or TTL'd aggressively) on index updates, or freshness work upstream never reaches users.

Prompt/prefix caches (provider-level): structure prompts so the expensive static parts - system prompt, tool definitions, few-shot examples, and in conversational RAG the accumulated history - form a stable prefix; cached input tokens are steeply discounted (commonly ~75-90% off on current models, with provider-specific write surcharges and TTLs) and TTFT drops substantially. Design consequence: put volatile content (retrieved chunks, current question) last, and keep the prefix byte-stable - a timestamp in the system prompt destroys the cache. Contextual retrieval's ingest-time LLM calls also lean on this: the full document is a shared cached prefix across its chunks' enrichment calls.

Semantic answer caches (riskiest, highest payoff): if a new query is sufficiently similar to a cached one (embedding similarity above a threshold), serve the cached answer - skipping the whole pipeline, ~100× cost and latency reduction on hits. Traps are the interview meat: false positives ("cancel my subscription" vs "renew my subscription" are embedding-close; a wrong-answer cache hit is worse than a slow answer - threshold conservatively and consider a fast verification step), staleness (key entries to document versions of the chunks that produced the answer; invalidate on their change), permissions (key by ACL context, accepting the hit-rate loss, or restrict semantic caching to public corpora), and personalization (answers that depend on the user's state must never be shared).

General rule: caches inherit every correctness requirement of the layer they bypass - freshness, authorization, personalization - so each cache key must encode everything the bypassed computation would have consulted.

Worth sketching. Drawing the invalidation arrows back from a document change is what separates cache design from cache enthusiasm.

flowchart LR
    Q["Query"] --> S{"Semantic answer cache,<br/>keyed by ACL and doc versions"}
    S -->|"hit"| A["Return cached answer"]
    S -->|"miss"| R{"Retrieval cache, keyed by<br/>query plus entitlements"}
    R -->|"hit"| G["Generate on a<br/>stable cached prefix"]
    R -->|"miss"| RT["Full hybrid retrieval"]
    RT --> G
    G --> A
    D["Document updated"] -->|"invalidate by doc version"| S
    D --> R

What a good answer covers

  • Embedding caches (safest)
  • Invalidation is trivial (content hash changes with content; key includes model version so migrations…
  • Retrieval-result caches: cache (normalized query → top-k chunk IDs) with a short TTL
  • Helps for head queries - most products have a Zipfian query distribution where the top few hundred…
  • Traps: must be keyed by the permission context (tenant + user entitlements), not just query text, or…

Key terms: Embedding caches · Retrieval-result caches · permission context · Prompt/prefix caches · Semantic answer caches · false positives · staleness · permissions · personalization

Interviewer follow-ups to expect

  1. How do you pick the similarity threshold for a semantic cache - what experiment?

  2. A customer reports seeing an answer referencing a doc they can't access; which cache do you suspect and why?

  3. Estimate the hit rate needed for a semantic cache to pay for its own embedding lookups.

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep