Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Synchronizing Multi-Tier L1
& L2 Caches.

Updating a price in Postgres and deleting L2 Redis leaves 39 other pods serving stale L1 Caffeine values for up to 5 minutes, and concurrent DB reads can re-pin stale data into L1.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a single bottleneck threatens end-to-end availability, synchronize L1 and L2 caches by broadcasting invalidations over Redis Pub/Sub (with short 30–60s L1 TTLs as a safety net) and fencing L1/L2 writes with monotonic version tokens to prevent stale fill races.

TL;DR30-second simplified summary
▾

Core Mechanics: Running 40 pods with in-process L1 Caffeine caches (TTL = 5 min, 82% hit rate) in front of L2 Redis Cluster (TTL = 30 min, 16% hit rate) and PostgreSQL 16 (2% miss) delivers sub-millisecond reads.

Visual & Structural Flow: Depicts L2 Redis DEL Leaves Peer Pods Stale for 5 Minutes (Pod 1 (Writer) -> Postgres 16 -> Redis L2 -> Pods 2..40 (L1)); Version-Fenced Caffeine L1 + Redis L2 + Pub/Sub Bus (40x Pod L1 (Caffeine) -> Redis Pub/Sub Bus -> Redis L2 (Lua CAS) -> PostgreSQL 16 (price_version)).

Trade-Offs & Production: Redis Pub/Sub is fire-and-forget—if Pod 12 briefly disconnects or reconnects, it misses invalidation messages. Achieve <= 200 ms invalidation p99 and bounded <= 30 s worst-case staleness: Redis Pub/Sub Invalidation (PUBLISH cache:inv <key>:<ver>): Broadcasts price updates to all.

Failure Modes & Application: Two-tier L1+L2 caching powers ultra-low-latency read paths: E-Commerce Pricing & Promotions: Keeps 80%+ hits in-process at < 0.1 ms while propagating price edits in < 200 ms.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why L2 Redis DEL leaves 39 other pods serving stale L1 Caffeine data.

Why this matters: The mechanism: understand how Why L2 Redis DEL leaves 39 other pods serving stale L1 Caffeine data works from first principles.

Think of Why L2 Redis DEL leaves 39 other pods serving stale L1 Caffeine data as the intuition layer for Synchronizing Multi-Tier L1 & L2 Caches: The mechanism: understand how Why L2 Redis DEL leaves 39 other pods serving stale L1 Caffeine data works from first principles.

Running 40 pods with in-process L1 Caffeine caches (TTL = 5 min, 82% hit rate) in front of L2 Redis Cluster (TTL = 30 min, 16% hit rate) and PostgreSQL 16 (2% miss) delivers sub-millisecond reads. However, when a merchant updates a price and the handling pod calls redis.del(key), the other 39 pods keep serving the old price from their local JVM memory for up to 5 minutes.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

5-minute checkout price mismatches and stale L1/L2 re-pins.

Why this matters: The problem: see what breaks without 5-minute checkout price mismatches and stale L1/L2 re-pins under real concurrency or scale.

Two failure modes break checkout validation (<= 500 ms lag SLA):

  1. 39-Pod L1 Drift: Deleting Redis does not touch in-process Caffeine maps on peer pods.
  2. Out-of-Order Stale Fill: A concurrent DB read on Pod 7 fetching v1 can populate L1/L2 after Pod 1 committed v2 and broadcast invalidation.
L1/L2 Cache Desynchronization

L2 Redis DEL Leaves Peer Pods Stale for 5 Minutes

Pod 1 (Writer)Postgres 16Redis L2Pods 2..40 (L1)1. UPDATE price=v2 (ver=9)2. Naive: DEL key only3. Pods 2..40 serve v1 for 5 min!4. Fix: PUBLISH inv key:9 (<5ms)5. All 40 L1s evict in <=200ms

Broadcasting invalidations over Redis Pub/Sub evicts all 40 L1 caches in <5-200ms, while version tokens block stale fills.

Engineering Decision Framework
Problem
How to implement Synchronizing Multi-Tier L1 & L2 Caches without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When a single bottleneck threatens end-to-end availability, synchronize L1 and L2 caches by broadcasting invalidations over Redis Pub/Sub (with short 30–60s L1 TTLs as a safety net) and fencing L1/L2 writes with monotonic version tokens to prevent stale fill races.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Synchronizing Multi-Tier L1 & L2 Caches experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Why fire-and-forget Pub/Sub alone fails during network blips.

Why this matters: The problem: see what breaks without Why fire-and-forget Pub/Sub alone fails during network blips under real concurrency or scale.

Think of Why fire-and-forget Pub/Sub alone fails during network blips as the intuition layer for Synchronizing Multi-Tier L1 & L2 Caches: The problem: see what breaks without Why fire-and-forget Pub/Sub alone fails during network blips under real concurrency or scale.

Redis Pub/Sub is fire-and-forget—if Pod 12 briefly disconnects or reconnects, it misses invalidation messages. Relying solely on Pub/Sub with a 5-minute L1 TTL still leaves Pod 12 stale for 5 minutes; and forgetting l1Cache.invalidateAll() on Pub/Sub reconnect preserves missed updates.

Lifecycle & Failure Recovery

Why fire-and-forget Pub/Sub alone fails during network blips

TTL expiredconcurrent missessingleflight lockvalue cachedCache HitCache MissHot-Key StampedeCoalesced Fill
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: Redis Pub/Sub Broadcast + Version Fencing + 30s L1 TTL.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Achieve <= 200 ms invalidation p99 and bounded <= 30 s worst-case staleness:

  • Redis Pub/Sub Invalidation (PUBLISH cache:inv <key>:<ver>): Broadcasts price updates to all 40 pods in < 5 ms; each subscriber evicts/updates Caffeine if cached.ver < ver.
  • 30s + Jitter L1 TTL + Reconnect Flush: Cap L1 TTL at 30 s (with jitter to avoid stampedes) and call l1Cache.invalidateAll() whenever the Pub/Sub client reconnects.
  • Monotonic price_version Fencing: Store price_version in Postgres; both L2 Redis (via Lua CAS) and L1 Caffeine reject any fill whose version is <= the known latest version.
Synchronized L1 + L2 Architecture

Version-Fenced Caffeine L1 + Redis L2 + Pub/Sub Bus

40x Pod L1(Caffeine)30s+jitter TTL; 82%…Redis Pub/Sub BusEvicts all 40 L1s…Redis L2 (Lua CAS)Version-fenced share…PostgreSQL 16(price_version)Monotonic version…

1.<=200ms Fanout: Redis Pub/Sub notifies all 40 pods simultaneously without polling overhead.

2.Reconnect Invalidation: Flush L1 on Pub/Sub reconnect and bound L1 TTL to 30s as a self-healing safety net.

3.Version Fencing: Both L1 and L2 reject stale DB reads with version < latest committed version.

Pub/Sub fanout, 30s L1 TTLs, reconnect flushes, and monotonic version fencing eliminate multi-tier cache drift.

05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where synchronized L1/L2 caching is used at scale.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Two-tier L1+L2 caching powers ultra-low-latency read paths:

  • E-Commerce Pricing & Promotions: Keeps 80%+ hits in-process at < 0.1 ms while propagating price edits in < 200 ms.
  • Feature Flags & Routing Tables: Uses Pub/Sub or SSE/gRPC streams to push rule changes fleet-wide.
  • Auth Token Revocation Lists: Broadcasts revoked jti IDs to all API gateway pods immediately.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why should a multi-tier L1/L2 cache pair Redis Pub/Sub invalidation with a short L1 TTL (e.g., 30s) and `invalidateAll()` on reconnect?

Recommended Learning Flow