Caching
Intermediate 11 min read Caching

Cache Invalidation

Keeping cached answers honest

Every cache is a time bomb of stale answers unless invalidation is explicit. TTL, version keys and event-driven purges keep cached LLM responses honest when products, prices or facts change.

TTLinvalidationversioningstale datacache busting
Prerequisites: Response Caching
Before we start

What you will learn

  • List invalidation strategies and their tradeoffs
  • Version cache keys on model/config changes
  • Design events that purge derived answers
Introduction

What is this?

Invalidation is how a cache forgets: TTL (time-based), version keys (build-based) or purge events (change-based). For LLM apps, staleness matters because an answer that referenced the old pricing or old model becomes a support disaster after a deploy.

Motivation

Why do you need it?

  • Answers go stale when the world changes (prices, policies, product facts).
  • TTLs alone can't coordinate with deploys — version keys can.
  • Purge events matter for facts that invalidate many keys at once (a doc re-baseline).
Think about it

Mental model

Imagine

Invalidation

Think of

A milk carton's date

The carton (TTL) protects you from unseen age; a heat wave (event) can spoil it early and needs its own signal.

Mechanics

How it works

  • TTL: each key expires after N seconds; simplest, bounded staleness.
  • Version key: bake a version into the key (model_id + content_version); bumping the version orphans old entries.
  • Purge events: on content/model deploy, delete keys in affected namespaces.
  • Soft invalidation (TTL) plus hard invalidation (deploy hook) overlap for safety.

Three invalidation signals.

Time
Version
Event
Fresh
Real world

In production

  • A pricing bot bumps the key version on every price deploy — no stale quotes.
  • A doc assistant purges its namespace when documents re-index.
  • A model upgrade flips responses: gateway changes model_version in the key automatically.
Guidelines

Best practices

  • Always TTL, even when you think content never changes.
  • Include product/data version in the cache key at the namespace level.
  • Wire deploys to flush affected namespaces (purge to zero, then rebuild).
  • Monitor staleness ticket rate as the invalidation health metric.
Watch out

Common mistakes

  • Permanent cache on facts that flip overnight.
  • Version bumps that don't actually change the keys' namespaces.
  • Purging everything on every deploy — thundering herd plus cold start.
When things break

Debugging tips

  • Stale answers persist after change → version key not bumped, or TTL longer than deploy cycle.
  • Rebuild storms post-purge → stagger rebuilds or keep a warm small TTL.
  • Some users see old answers → per-region or per-replica cache divergence; centralize.
Interviews

Interview checkpoint

Practice

Hands-on lab

Version-Key Invalidation
25 min · intermediate
In progress
Objectives
  • Bake a deployment version into cache keys
  • Purge a namespace atomically
  • Observe stale answers disappear

Version-Key Invalidation

25 min · intermediate

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Which invalidation matches a code deploy best?

2

TTL alone makes a cache safe from deploy-driven staleness.

Go deeper

Official documentation