advanced

Cache invalidation

Connect writes to affected cached reads through keys, tags, events, TTLs, and operational purge paths.

Cache invalidation connects writes to stale reads through TTL expiry, explicit delete, versioned keys, tag-based purge, or event-driven invalidation. There is no universal best strategy — combine short TTL as safety net with proactive invalidation on known dependencies.

Version keys include updated_at or content hash so new writes naturally miss old entries. Tag queues map related keys for bulk purge when a category changes.

On interviews: explain invalidation for "user updates profile photo" across CDN, Redis, and client cache layers.

Common pitfalls: invalidating too little (stale UI) or too much (thundering herd on origin); race between write and invalidation; forgotten edge caches.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • Map each write to affected cache layers.
  • Use TTL plus proactive invalidation.
  • Prefer versioned keys where possible.
  • Test invalidation under concurrent updates.