Cache Invalidation
1. Executive Summary
Cache invalidation is the process of removing or updating cached entries so clients do not observe indefinitely stale data relative to the authoritative source. Phil Karlton's aphorism—"There are only two hard things in Computer Science: cache invalidation and naming things"—remains relevant because invalidation spans multiple cache tiers, concurrent writers, eventual propagation delay, and ambiguous ownership of who invalidates what.
Strategies include TTL-only (accept bounded staleness), write-invalidate (delete keys on mutation), write-update (push new value to cache), version stamps (compare-and-swap semantics), pub/sub fan-out (notify all app instances), CDC-driven invalidation, and CDN purge APIs. No single strategy fits all data—prices demand immediate invalidation; aggregate counters may tolerate seconds; static assets use content-hash immutability.
This chapter covers invalidation patterns, consistency models, multi-tier propagation, failure modes, CDN specifics, stampede prevention, and principal-level design for correct cached systems.
2. Why This Topic Matters
Interviewers probe beyond "add Redis": "Product price changes—how fast do users see it?" Weak answers say "short TTL."
Strong candidates explain:
- TTL is a staleness bound, not guaranteed freshness after writes.
- Delete-on-write beats update-on-write for race avoidance in cache-aside.
- CDN purge is slow and rate-limited—immutable URLs often beat purge.
- Invalidation storms can overload origin during mass updates.
- Derived cache keys (aggregates, joins) multiply invalidation surface.
Production bugs include double-discount from stale coupons, sold-out items still purchasable, permissions cached after revocation, and cross-region stale reads after partial purge. Invalidation is where caching becomes a correctness problem, not just performance.
3. Problems Being Solved
| Problem | Naive TTL-only | Invalidation strategy |
|---|---|---|
| Stale writes visible | Wait until TTL | Proactive delete on write |
| Multi-tier caches | Each tier diverges | Coordinated purge or versioned URLs |
| Derived data | Orphan stale aggregates | Track dependencies or recompute |
| Global CDN | Hours of stale content | Purge API or immutable asset pattern |
| Permission revocation | Security window | Short TTL + explicit invalidation |
Invalidation solves freshness correctness within defined SLAs. It does not provide strong linearizability across all readers without synchronous read-through or bypassing cache on critical reads.
4. Assumptions and System Model
Assume multiple writers (admin APIs, batch jobs, replication) and multiple cache layers (local, Redis, CDN):
- Authoritative source is database or event log.
- Invalidation messages may be lost, duplicated, or reordered (at-least-once pub/sub).
- Propagation is not instantaneous—milliseconds to minutes depending on tier.
- Clock skew affects TTL expiry times slightly—not for logical versioning.
Staleness SLA examples:
| Data type | Typical SLA | Strategy |
|---|---|---|
| Stock price | < 1s | Invalidate + short TTL |
| Product description | < 5 min | TTL + invalidate on admin |
| Avatar image | Minutes–hours | Immutable URL with hash |
| Session permissions | Immediate | No cache or sub-second TTL + revoke |
5. Essential Terminology
| Term | Definition |
|---|---|
| TTL invalidation | Passive expiry by time. |
| Active invalidation | Explicit delete/update on data change. |
| Write-invalidate | Remove cache entry on write. |
| Write-update | Push new value to cache on write. |
| Version stamp | Monotonic version; readers reject old versions. |
| Cache tagging | Group keys under tag; invalidate tag in one operation. |
| Surrogate key | CDN tag for related objects (e.g., all product pages). |
| Purge vs ban | Purge removes if present; ban prevents future cache (CDN-specific). |
| Thundering herd | Many concurrent misses after invalidation. |
| Torn cache | Partially updated related keys—inconsistent view. |
Mnemonic: Invalidate proactively; TTL is the safety net.
6. Core Mechanism
Write-invalidate (cache-aside)
Figure 1: Delete cache after DB commit—preferred over UPDATE cache for race safety.
Pub/sub fan-out to local caches
Figure 2: Distributed local caches require broadcast invalidation—missed message = stale local entry until TTL.
CDN surrogate key purge
Figure 3: CDN purge is asynchronous—design for brief staleness or immutable URLs.
7. Step-by-Step Walkthrough
Scenario: Blog platform caches article HTML in Redis, local cache, and CDN.
| Layer | Key / mechanism | Invalidation on publish |
|---|---|---|
| DB | articles table | Source update |
| Redis | article:{slug} | DEL on publish webhook |
| Local | Caffeine article:{slug} | Redis pub/sub invalidate:{slug} |
| CDN | URL /articles/{slug} | Surrogate-key purge article-{id} |
Ordering requirement:
| Step | Action | Risk if wrong order |
|---|---|---|
| 1 | Commit DB with new content | — |
| 2 | DEL Redis | Stale if before DB commit |
| 3 | PUBSUB local evict | Brief local staleness if missed |
| 4 | CDN purge | Async delay unavoidable |
Derived key problem:
| Cached key | Depends on |
|---|---|
homepage:featured | Articles 1, 5, 9 |
category:tech:list | All tech articles |
Updating article 5 must invalidate all derived keys—maintain dependency graph or avoid caching aggregates without tagging.
Version stamp alternative:
| Approach | Mechanism |
|---|---|
Store {value, version} in cache | Writer bumps version in DB |
| Reader compares cache version to DB version on read | Reject stale cache |
Higher read cost but simpler fan-out for read-heavy paths.
Two-cache problem (Martin Thompson):
When two independent caches store related data without coordination, readers may observe inconsistent combinations—cache A fresh, cache B stale. Example: product price cache updated but category listing cache still shows old price.
| Solution | Tradeoff |
|---|---|
| Single aggregate cache key | Larger values; over-invalidation |
| Version vector on all related keys | Reader checks all versions match |
| Invalidate all related on any change | Tag-based invalidation |
| Don't cache aggregates | Higher origin load |
Probabilistic early expiration (stampede prevention detail):
Instead of fixed TTL expiry for hot keys, each read has small probability p of treating the key as expired even if TTL remains:
if (now > expiry - jitter) OR (random() < p):
trigger background refresh (single-flight)
return stale value if refresh in progress
This spreads refresh load over time rather than concentrating at exact expiry. Libraries like cachestampede implement variants—verify algorithm against your staleness SLA.
Invalidation via change data capture (CDC):
| Step | Component |
|---|---|
| 1 | Database commit changes row |
| 2 | Debezium reads WAL/binlog |
| 3 | Invalidation service consumes change event |
| 4 | Maps table row to cache key(s) via config |
| 5 | DEL Redis keys; optional pub/sub to local caches |
Advantage: Writers do not need cache knowledge—decouples teams. Disadvantage: CDC lag (typically ms–seconds) creates invalidation delay; derived key mapping must be maintained in invalidation service config.
Legal and compliance invalidation:
GDPR right to erasure requires deleting personal data from all cache tiers including CDN edge nodes—not just origin database. Maintain a data map listing which cache keys may contain PII and automate purge on erasure requests. Failure to purge CDN cached profile pages is a documented compliance gap in several enforcement actions—treat CDN as part of data storage scope.
Cache-aside race conditions (detailed):
| Race | Sequence | Outcome |
|---|---|---|
| Read stale after write | T1: Writer updates DB; T2: Reader misses cache (not yet invalidated); T2: Reader loads OLD DB value if read replica lags; T2: Reader SETs stale value | Extended staleness |
| Thundering populate | N readers miss; N concurrent DB queries | Origin spike |
| Lost invalidation | Invalidation message lost; TTL only recovery | Stale until TTL |
Fix for populate race: Use per-key lock (mutex) or SETNX with short TTL during populate—only one loader per key.
Write-behind consistency window:
Write-behind caches acknowledge writes immediately and batch-flush to DB asynchronously:
| Benefit | Risk |
|---|---|
| Sub-ms write latency | Data loss if cache node crashes before flush |
| Absorbs write spikes | Complex conflict resolution on flush |
Use write-behind only when business tolerates seconds of durability risk and you have WAL/replay on cache failure—or restrict to non-critical counters.
CDN cache key normalization:
CDNs cache by full URL including query string—?v=1 and ?v=2 are different cache entries. Standardize:
- Version in path (
/assets/app.v42.js) not query for cache efficiency. - Normalize query parameter ordering if query-based keys unavoidable.
- Use surrogate keys for logical invalidation across URL variants.
Invalidation SLA documentation template:
Every cached entity should have an entry in the cache contract:
| Field | Example |
|---|---|
| Entity | Product.price |
| Cache keys | product:{id}, CDN surrogate product-{id} |
| Max staleness | 5 seconds |
| Invalidation trigger | ProductUpdated event, admin API |
| Owner team | Catalog |
| Fallback | TTL 60s |
This template forces explicit ownership—gaps surface in architecture review rather than production incidents.
Multi-writer invalidation coordination:
When multiple services can update the same entity (e.g., admin API and batch price sync job), invalidation must be centralized or idempotent:
| Approach | Description |
|---|---|
| Single writer principle | Only one service mutates; others request via API |
| Shared invalidation topic | All writers publish InvalidateKey events |
| Version monotonic | Highest version wins; stale invalidations harmless |
Without coordination, race between two writers can leave cache inconsistent with DB even after both invalidate—last writer to DB must win; use DB version in cache value.
Principal review checkpoint: Ask "what is the maximum staleness a customer can observe after an admin update?" If the team cannot answer with a number and tier breakdown (local, Redis, CDN), the invalidation design is incomplete.
Hands-on validation: Measure end-to-end staleness with integration tests that write to DB, poll CDN edge and Redis until fresh or timeout—automate this in CI for price-sensitive entities.
8. Invariants and Guarantees
| Property | Type | Statement |
|---|---|---|
| Fresh read after invalidation | Safety (eventual) | After propagation completes |
| No stale read forever | Liveness | TTL provides ultimate bound |
| Immediate global consistency | Not guaranteed | CDN and local tiers lag |
| Invalidation idempotency | Safety | Duplicate DEL safe |
9. Failure Scenarios
Scenario 1: Invalidation before DB commit
Setup: DELETE cache then DB transaction rolls back.
Effect: Cache empty; next read loads old DB value—OK. Worse variant: UPDATE cache then rollback leaves wrong cache.
Mitigation: Invalidate only after commit; transactional outbox for invalidation events.
Scenario 2: Missed pub/sub message
Setup: App instance offline during invalidation broadcast.
Effect: Stale local cache until TTL.
Mitigation: Short local TTL; periodic version check for critical data.
Scenario 3: Partial derived invalidation
Setup: Product update; homepage aggregate not invalidated.
Effect: Wrong featured price on homepage.
Mitigation: Tag-based invalidation; recompute aggregates from events.
Scenario 4: CDN purge rate limit
Setup: Black Friday mass price update; 10k purges.
Effect: Purge queue backlog; widespread stale prices.
Mitigation: Immutable versioned URLs (/price/v42); batch surrogate purge; accept SLA.
Scenario 5: Invalidation stampede
Setup: DEL hot key; 50k requests miss simultaneously.
Effect: Origin overload.
Mitigation: Single-flight reload; soft TTL with background refresh.
Scenario 6: Cross-region replication lag
Setup: Write to primary region; read from replica before replicate.
Effect: Cache repopulated with stale replica data.
Mitigation: Read-your-writes routing; invalidate after replication ack.
10. Performance Characteristics
| Strategy | Write path cost | Read path cost | Freshness |
|---|---|---|---|
| TTL only | Low | Low | Bounded stale |
| Write-invalidate | Medium (DEL) | Low after warm | Good post-propagation |
| Write-update | Medium | Lowest if hit | Race risk |
| Version check each read | Low write | Higher read | Stronger |
| CDC invalidation | Async lag | Low | Near-real-time |
Pub/sub fan-out: O(instances) messages per invalidation—acceptable for admin edits; problematic for high-frequency writes.
11. Scalability Limits
- Tag purge cardinality—some CDNs limit tags per object.
- Dependency graph maintenance complexity grows with aggregate caches.
- Invalidation rate during flash sales may exceed purge API quotas.
- Global CDN purge propagation 150ms–minutes (vendor-dependent; verify docs).
12. Operational Considerations
- Invalidation audit log—who purged what, when.
- Dashboards: stale read reports, purge queue depth, post-deploy cache hit drop.
- Runbooks: emergency global purge; disable cache toggle flag.
- Integration tests: publish → assert all tiers within SLA.
- Feature flag to bypass cache for debugging.
13. Security Considerations
- Authorization cache must invalidate on role change—security-critical.
- Purge API keys highly privileged—prevent attacker mass purge DoS.
- Cache key guessing after invalidation may timing-attack—low risk but awareness.
- GDPR erasure requires delete across all tiers including CDN.
14. Cost Considerations
- CDN purge API may be priced per request at scale.
- Over-invalidation increases origin load—balance freshness vs cost.
- Immutable asset pattern reduces purge needs—storage for multiple versions.
- Engineering time for dependency tracking—expensive for complex aggregates.
15. Production Implementations
Redis keyspace notifications / pub/sub
Lightweight app instance coordination—not durable; missed messages possible.
Debezium CDC → invalidation service
Database change stream triggers cache deletes—decouples writers from cache knowledge.
Varnish / Fastly surrogate keys
Tag-based bulk purge for related HTML fragments.
CloudFront invalidation
Path-based purge; limits on concurrent invalidations—check AWS current quotas.
Hibernate second-level cache
Region invalidation on entity update—ORM-managed; understand session boundaries.
GraphQL @cacheControl
Schema-level TTL and scope—CDN integration in Apollo Router and similar.
16. Alternatives and Tradeoffs
| Approach | Freshness | Complexity | Write amplification |
|---|---|---|---|
| TTL only | Poor on change | Lowest | None |
| Write-invalidate | Good | Medium | Low |
| Immutable URLs | Excellent for assets | Medium | New object per version |
| No cache on critical reads | Perfect | Low read perf | None |
| Event-driven materialized views | Eventual | High | Recompute on event |
17. Common Misconceptions
| Misconception | Reality |
|---|---|
| "Zero staleness with CDN" | Purge is async; plan for window. |
| "UPDATE cache is simpler than DELETE" | Races cause stale writes winning. |
| "One TTL fits all" | Entity-specific SLAs required. |
| "Invalidation is instant everywhere" | Multi-tier propagation takes time. |
| "CDC solves everything" | Lag + derived keys still hard. |
18. Principal Architect Perspective
- Classify data into freshness tiers before choosing strategy.
- Prefer immutable URLs for static content over purge-heavy workflows.
- Delete not update cache on write in cache-aside.
- Tag related keys early—retrofit is painful.
- Test invalidation in CI as first-class scenario.
Governance: Cache invalidation contracts in API design—every write endpoint documents which keys/tags it invalidates.
19. Architecture Review Exercise
Scenario: Marketplace caches seller ratings aggregate; individual review updates don't invalidate; 1-hour TTL.
Review prompts:
- Fraudulent review hidden—visible in aggregate?
- Legal takedown timeline met?
- Redesign invalidation?
Expected findings: Invalidate aggregate on review change; event-driven recompute; shorter TTL for legal-sensitive data.
20. Whiteboard Explanation
90-second version:
"Cache invalidation keeps cached data from going stale wrong. TTL alone means users see old data until expiry—use TTL as safety net, not primary strategy. On writes, commit to database first, then delete cache keys—delete beats update because concurrent reads won't write stale values back. Multiple tiers need coordinated invalidation: Redis DEL plus pub/sub to evict local caches plus CDN surrogate-key purge. CDN purge is async and rate-limited—use content-hash immutable URLs when possible. Derived caches like homepage lists need tag invalidation or dependency tracking. Watch thundering herd after mass invalidation—single-flight reload. Classify data by staleness SLA—prices aren't like profile bios."
21. Interview Questions
-
TTL vs active invalidation?
- Signals: Bound vs proactive; combine both.
-
Delete vs update cache on write?
- Signals: Delete avoids races.
-
CDN stale after purge?
- Signals: Async propagation; immutable URLs.
-
Invalidate local + Redis?
- Signals: Pub/sub, short local TTL.
-
Derived aggregate invalidation?
- Signals: Tags, dependency graph, avoid cache.
-
CDC for invalidation?
- Signals: Decouple; eventual lag.
-
Security-sensitive cache?
- Signals: Short TTL, explicit revoke, no cache.
-
Thundering herd after purge?
- Signals: Single-flight, soft TTL.
-
Design price change flow.
- Signals: DB commit, DEL keys, CDN purge, SLA.
-
Karlton quote implications?
- Signals: Multi-tier, races, ownership complexity.
Scoring rubric:
| Dimension | Strong | Weak |
|---|---|---|
| Ordering | Invalidate after commit | Before commit |
| Multi-tier | CDN + Redis + local | Redis only |
| Derived data | Tags/dependencies | Single key focus |
22. Interview Follow-Ups
-
Mass invalidation during sale—strategy?
- Signals: Versioned URLs, batch surrogate purge, pre-warm.
-
Invalidation event lost?
- Signals: TTL backup, durable invalidation log, version check.
-
Global active-active invalidation?
- Signals: Cross-region pub/sub, CRDTs rare; often sticky routing.
22A. Production Low-Level Design (Interview + Game Day)
Write-path invalidation with stampede protection on hot keys. Production LLD and Game Day Standard.
Control vs data plane
| Plane | Components |
|---|---|
| Control | Outbox / PriceChanged bus, invalidation matrix per entity, version monotonic |
| Data | Regional Redis DEL, local LRU evict (pub/sub), CDN surrogate purge, backup TTL |
Step-by-step flow:
| Step | Action | Failure if skipped |
|---|---|---|
| 1 | Commit DB + bump price_version | Stale authoritative data |
| 2 | Emit durable invalidation event (outbox) | Lost DEL on crash |
| 3 | Regional Redis DEL / SET new value | Cross-region staleness |
| 4 | Pub/sub flush in-process LRU | Pod serves stale until local TTL |
| 5–7 | Singleflight on miss + jittered TTL | Thundering herd melts origin |
Contract
| Field | Example |
|---|---|
| Max staleness SLA | 5s p99 price (active invalidate; TTL 60s backup) |
| Idempotency | Duplicate PriceChanged → idempotent DEL |
| Stampede | Per-key lock / singleflight.Group on miss |
One SLO
Price freshness: time from DB commit → p99 edge serving version ≥ committed < 5s; alert on invalidate lag p99 > 3s.
22B. Game Day Playbooks
| # | Drill | Inject | Detect | Mitigate | Verify |
|---|---|---|---|---|---|
| GD-1 | Lost invalidation message | Drop pub/sub to 10% pods | Stale price probe fails | Short local TTL + version check on read | p99 freshness < SLA within 60s |
| GD-2 | Thundering herd after mass purge | DEL 1k hot keys; spike traffic | Origin QPS 10×; p99 latency | Enable singleflight; soft TTL refresh | DB QPS returns to baseline |
| GD-3 | CDN purge backlog | Rate-limit purge API | Surrogate-key queue depth | Serve price via uncached API JSON | Probe edge freshness < 5s |
GD-2 — Thundering herd after invalidation
23. Strong Answer Example
Question: "Ensure product price updates visible within 5 seconds globally."
Answer this as a 45-minute design discussion using the PRACTICE framework. Below is how a strong student sounds aloud—active invalidation across tiers, not "TTL=5s."

Figure A: PRACTICE map — Problem → Requirements → Assumptions → Capacity → Topology → Invariants → Constraints → Evolution.
Student interview script (PRACTICE)
P — Problem framing
"A 5-second global freshness SLO for product prices means: after the pricing write commits, users in every region should observe the new price within 5s (I'd define p99 of regions/edges).
TTL alone fails. A 5-minute Redis TTL can leave stale prices for minutes. Even TTL=5s means up to 5s average wait without tying expiry to the write—and it hammers origin. TTL is a safety net, not the primary control.
Short answer: Commit price + version to DB → durable invalidate fan-out (outbox) to all regional Redis + flush local caches → keep price JSON off long-lived CDN (or purge surrogate keys) → single-flight on miss → measure end-to-end staleness."
R — Requirements
| Requirement | Design implication |
|---|---|
| ≤5s visible globally (p99) | Active invalidate; budget propagation ≤ ~3–4s |
| No forever-stale on lost message | Short backup TTL (e.g. 30–60s) + version checks |
| Don't melt DB on mass sale updates | Single-flight; soft TTL refresh; batch/tag purge |
| Correct vs description freshness | Price path stricter than blurb/images |
| Multi-tier (local, Redis, CDN, HTML) | Invalidate every tier that embeds price |
"I'd ask: is price in API JSON only, or also SSR HTML / CDN edge? How many regions?"
A — Assumptions (state aloud)
| Assumption | Why it matters |
|---|---|
| Multi-region Redis / app pods | Must fan out, not DEL one cluster |
| At-least-once invalidation bus | Handlers idempotent; versions monotonic |
| CDN may cache HTML with embedded price | Biggest hidden staleness source |
| DB is source of truth | Cache never wins over committed version |
| 5s is p99, not mathematical zero | CDN purge can be async—design around it |
"I'd say out loud: immutable hashed images don't need 5s; price fields do."
C — Capacity
Admin / batch price updates → outbox → invalidate workers (not sync purge in request path)
Cross-region fan-out → dominate the 5s budget; parallel DELs per region
Pub/sub to N app instances → OK for admin rates; careful under huge write storms
CDN purge quotas → avoid per-SKU HTML on CDN; or surrogate-key batch purge
Backup TTL 30–60s → bounds damage if invalidate lost; still not primary
"I'd spend the latency budget on reliable fan-out, not on hoping TTL aligns with the write."
T — Topology (draw this)

Figure B: DB commit with price_version → outbox fan-out to regional Redis + local evict; price API not long-CDN-cached; total budget < 5s.

Figure C: TTL-only cannot meet a write-triggered 5s SLO; versioned keys + regional invalidate + edge policy can.
| Tier | Strategy for 5s price SLO |
|---|---|
| DB | Authoritative price + monotonic price_version |
| Redis (each region) | Write-invalidate (DEL) or write-update after commit; backup TTL |
| In-process LRU | Pub/sub / bus flush; short local TTL (seconds) as backup |
| Price API response | Cache-Control: private, max-age=0 / no-store—don't CDN-cache price JSON |
| SSR / product HTML | Surrogate-key purge or fetch price client-side from uncached API |
| Images | Immutable URLs—out of scope for 5s price |
Optional upgrade: write-update (SET new price) after invalidate to warm hot SKUs and shrink miss storms.
I — Invariants (safety)
"Non-negotiable:
- Invalidate only after DB commit (outbox)—never before.
- Every tier that can show price is in the invalidation matrix.
- TTL is backup, not the 5s mechanism.
- Versions in cache values—reject/reload if
cached.version < db.version.- Idempotent invalidation handlers (at-least-once).
- SLO metric defined: time from commit → p99 edge/API serving new version."
C — Constraints (second C)
| Constraint | Design response |
|---|---|
| Lost pub/sub to one pod | Short local TTL; version check on read for critical paths |
| CDN purge rate limits / async lag | Don't put live price in long-lived CDN objects |
| Read replica lag | Read-your-writes on primary for reload after invalidate |
| Black Friday mass updates | Batch/tag invalidation; single-flight; consider SET new value |
| Derived caches (category lists) | Tags / dependency invalidate—or don't embed live price in list cache |
| Multi-writer price jobs | Central invalidate topic; highest price_version wins |
E — Evolution (prove it)
Phased delivery:
| Phase | Deliverable |
|---|---|
| M1 | ADR: 5s p99 price SLO; invalidation matrix per tier |
| M2 | Outbox PriceChanged → regional Redis DEL + local evict |
| M3 | Strip price from CDN-cached HTML / set no-store on price API |
| M4 | price_version in responses/cache; backup TTL 60s |
| M5 | Integration probe: write → poll US/EU/APAC until fresh; alert if >5s |
| M6 | Chaos: drop invalidation message; verify TTL/version recovery |
Failure modes:
| Failure | Mitigation |
|---|---|
| "TTL=5 seconds" | Active invalidate + versions |
| HTML CDN still stale | Purge or uncache price in HTML |
| Local cache not flushed | Bus + short local TTL |
| Invalidate before commit | Outbox after commit |
| Herd after mass DEL | Single-flight / soft refresh |
Observability: invalidate lag p50/p99 by region, stale-version serve rate, CDN purge queue depth, price probe SLO compliance, origin QPS after sales.
Closing line (interview)
"So for prices visible within 5 seconds globally, I won't set TTL=5. I commit price + version, emit PriceChanged via outbox, invalidate every regional Redis and local cache, and keep live price off long-lived CDN (or purge surrogate keys). TTL stays a backup. I'd measure commit→edge freshness per region and alert when p99 exceeds 5s—and use single-flight so mass invalidation doesn't stampede the DB."
Scoring signals
| Strong | Weak / red flag |
|---|---|
| Active invalidate + 5s budget | "Set Redis TTL to 5 seconds" |
| Multi-tier matrix (local/Redis/CDN/HTML) | Redis-only thinking |
| Outbox after commit + versions | Invalidate-before-write / no version |
| Price API cache-control policy | Ignore CDN-embedded prices |
| SLO probe / metrics | No measurement |
| Stampede after mass invalidate | Blind DEL storm |
Compact outline (if time-boxed to 2 minutes)
"Not TTL=5. Commit price+version, outbox fan-out invalidate to all regional Redis and local caches, don't CDN-cache live price JSON/HTML. TTL as backup. Probe p99 freshness <5s. Single-flight on miss."
24. Weak Answer Example
Question: "Ensure product price updates visible within 5 seconds globally."
"Set Redis TTL to 5 seconds."
Why weak / red flags: TTL is not write-triggered freshness; ignores CDN/local tiers and global fan-out; causes origin load; no version/SLO measurement—classic interview trap.
25. Hands-On Exercise
- Cache-aside with Redis; update DB without invalidation—measure stale duration.
- Add post-commit DEL; compare.
- Demonstrate update-cache race with concurrent readers.
- Add Redis pub/sub local eviction.
- Simulate 100 concurrent misses after DEL—add single-flight.
- Model tag invalidation for
category:*keys. - Document invalidation matrix per API endpoint.
26. Knowledge Check
- Invalidate before or after DB commit? (After.)
- DELETE vs UPDATE cache? (DELETE safer.)
- CDN purge synchronous? (No—async.)
- TTL role with active invalidation? (Safety net.)
- Derived cache challenge? (Multiple keys to invalidate.)
27. Flashcards
| # | Front | Back |
|---|---|---|
| 1 | Write-invalidate | DEL cache on write. |
| 2 | Surrogate key | CDN group purge tag. |
| 3 | TTL safety net | Bounds max staleness. |
| 4 | Pub/sub evict | Local cache coordination. |
| 5 | Immutable URL | Hash in path avoids purge. |
| 6 | Thundering herd | Post-invalidation miss storm. |
| 7 | CDC invalidation | DB change stream triggers DEL. |
| 8 | Version stamp | Logical freshness check. |
| 9 | Tag invalidation | Bulk related key purge. |
| 10 | Torn cache | Inconsistent related keys. |
28. Cheat Sheet
WRITE ORDER
1. DB COMMIT
2. DEL cache keys / tags
3. PUBSUB local evict
4. CDN purge (async)
PREFER
DELETE over UPDATE (races)
Immutable URLs for static
TTL as backup not primary
DERIVED KEYS
Tag-based invalidation
Or avoid caching aggregates
HERD
Single-flight after invalidation
Jittered soft TTL
SECURITY
Invalidate on auth change
Short TTL on permissions
29. Related Concepts
- Caching Fundamentals — patterns and tiers
- Distributed Caching — multi-node coordination
- Eventual Consistency — staleness model
- Transactional Outbox — reliable invalidation events
- Message Delivery Semantics — at-least-once invalidation handlers
30. References
Primary sources
- Karlton, P. — aphorism on cache invalidation (widely cited; primary publication unverified).
- Lamport, L. — ordering and staleness reasoning applicable to version-based invalidation.
Engineering
- Fastly Documentation — Surrogate keys.
- Redis Documentation — Keyspace notifications, Pub/Sub.
- Martin Fowler, "Two Hard Things" — naming and cache invalidation discussion.
Distinction
| Claim type | Source |
|---|---|
| Delete-on-write pattern | Engineering practice; Redis patterns |
| CDN purge behavior | Vendor documentation—verify quotas |
| CDC invalidation | Debezium and similar operational guides |