Skip to content
mentoor

Topic 5 of 5 · 5 min read

Stale, leaked and wrong cached answers

Goal: Find the flaw in a cache design that would serve a stale, wrong or other user's answer and fix it.

A cache copies one answer and serves it many times, so every flaw in that answer is multiplied and every missing scope becomes a leak. Review a cache design by asking what it would serve to whom, and for how long.

Check yourself · 1 of 3

A cached answer says "Your balance is 240" and the key is the question text. What is the main risk?

Choose one answer

Why it matters

OWASP lists sensitive information disclosure and misinformation among the main risks of LLM applications. A cache turns a single occurrence of either into a repeated one. A hallucinated policy, stored once, reaches every user who asks a similar question until it expires.

The four failure modes

  1. Stale. The answer was true when stored. The refund policy changed on Monday; the cache still serves last week's text until the TTL ends.
  2. Leaked. The answer was personalised ("Your plan limit is 50 seats") and was stored under a key without the user or tenant. The next asker sees it.
  3. Wrong, then repeated. The model made something up. Nobody checked. Now it is stored and served instantly and confidently.
  4. Out of date with the system. You changed the prompt template, the model or the retrieval index, and old entries from the previous behaviour are still served.

How to defend

  • Scope the key by tenant and user whenever the answer used account data, and never cache a response that includes personal data unless the key covers it.
  • Tie entries to their sources. Store the source document ids and versions with the answer; when a source changes, delete the entries that cite it.
  • Version everything. Put the prompt version, model name and index version in the key so a change makes old entries unreachable.
  • Only cache vetted answers. Cache after a validation step, such as groundedness checks or a thumbs-up, and keep a way to purge one entry by id quickly.
  • Log hits. Record that an answer came from cache, with its age, so a bad answer can be traced and every user who got it found.

Before and after

- key = sha256(user_message)
- cache.set(key, answer, ttl=7*86400)
+ key = sha256(json.dumps({"tenant": tenant_id, "user": user_id,
+     "prompt": PROMPT_VERSION, "model": MODEL, "msg": user_message}))
+ cache.set(key, answer, ttl=3600, sources=doc_versions)

The old line serves a week-old, user-blind answer. The new one scopes, versions and expires it, and records the sources.

What goes wrong

  • Long TTL as the only defence. A week looks efficient until a policy change.
  • Purge by hand. Without source links, nobody knows which entries to delete.
  • No marker that a hit was cached. Support cannot tell a stale answer from a fresh one.

Examples

The leaked plan limit

A bot answers "What is my seat limit?" with the caller's account in the prompt. The cache key is the question text. Customer A asks first and the answer, 50 seats, is stored. Customer B asks the same words and gets 50 seats. Fix: put the account id in the key, or do not cache responses built from account data.

A hallucination, repeated

The model invents a 90-day refund window when the real policy is 30 days. It is stored with a 24-hour TTL. In that day, 600 users get the wrong promise. A grounding check before writing, and a purge by entry id, would have limited it to one answer.

Key points

  • Caching multiplies a bad answer: stale, personalised or hallucinated text is served to everyone who matches.
  • Include tenant, user, prompt version, model and index version in the key whenever they affect the answer.
  • Store source document ids and versions with each entry so a source change can delete exactly what it affects.
  • Cache only answers that passed validation, and keep a one-command purge by entry id.
  • Log whether each answer came from cache and how old it was.

Common mistakes

  • Relying on a long TTL as the only freshness control.
  • Caching personalised answers under a key that lacks the user or tenant.
  • Writing every model answer to the cache before any validation.
  • Changing the prompt or model without bumping the version in the key.

Check yourself · 2 of 3

Your team ships a new prompt version. What keeps old cached answers from being served?

Choose one answer

Check yourself · 3 of 3

A policy document changes today. What design lets you remove only the affected cached answers?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic