CodeMosa

Master LeetCode Patterns

Cache Invalidation

Caching Patterns

TTL, event-driven, and manual invalidation to manage cache freshness

Core Idea

#

Control staleness by expiring, versioning, or actively invalidating cached data. Use TTL with jitter, event-driven invalidation, and versioned keys to balance freshness and performance.

When to Use

#

When correctness matters with cached data, or you operate multiple cache layers and need predictable freshness.

Recognition Cues

#
Indicators that this pattern might be the right solution
  • Users see stale data after updates
  • Cache stampede on popular keys
  • Manual flushes frequently needed
  • Mismatched data across layers (browser/CDN/app cache)

Pattern Variants & Approaches

#

Overview

#
Write-through or write-behind updates publish invalidation events; workers evict or update keys across cache tiers.

Overview Architecture

WriteReadPublishConsumeEvict/Update⚙️Application💾DatabaseCache📬Event Bus⚙️Invalidation Worker

When to Use This Variant

  • Stale data anomalies
  • Multi-layer caches (browser/CDN/app)
  • Need precise freshness windows

Use Case

Feeds, product catalogs, and dashboards requiring fresh-but-fast reads.

Advantages

  • Predictable freshness
  • Reduced stampedes
  • Clear ownership of keys

Implementation Example

# Invalidation flow (pseudocode)
db.update(row)
eventBus.publish({ type: 'invalidate', key })
worker.on('invalidate', key => cache.delete(key))

Tradeoffs

#

Pros

  • Predictable freshness and fewer anomalies
  • Reduced origin load and stampedes
  • Better multi-layer cache coherence

Cons

  • More moving parts and state to track
  • Eventual consistency windows
  • Operational complexity and tooling needs

Common Pitfalls

#
  • No TTLs or very long TTLs by default
  • Inconsistent key naming/namespacing
  • Not using negative caching for misses
  • Event-driven invalidation lost or unordered
  • No single-flight leading to thundering herd

Design Considerations

#
  • Set TTLs with jitter; add soft TTL and SWR
  • Version keys with schema/content hash
  • Event-driven invalidation via CDC/pub-sub
  • Write fences to prevent stale reads post-write
  • Request coalescing/single-flight for hot keys

Real-World Examples

#
Facebook

TAO graph cache invalidation at scale

Billions of entities
Reddit

Event-driven invalidation of hot pages

Hundreds of millions of users
Etsy

Versioned cache keys for releases

Large commerce catalogs

Complexity Analysis

#
Scalability

Horizontal - Distributed caches

Implementation Complexity

High - Hardest problem in CS

Cost

Medium - Infra + engineering time