This is an old revision of the document!
Table of Contents
Cache Invalidation Patterns in Real Production Systems
Overview
Cache invalidation is the process of keeping cached data consistent with the database when data changes.
In real production systems, the main challenge is not caching itself, but:
Ensuring consistency between cache and database at scale in distributed systems.
Most real systems use multiple patterns together, not just one.
1. Cache-Aside (Most Common Pattern)
Concept
Application manages cache manually.
Cache is treated as a read-through layer, and DB remains the source of truth.
Read Flow
Client → Application → Cache ↓ miss Database → Cache → Response
Write Flow
Client → Application → Database update → delete cache key
Key Idea
Cache is disposable. If wrong → just delete it.
Where it is used
E-commerce systems Social platforms Microservices architectures Most production APIs
Pros
Simple Safe Easy to debug Works well in distributed systems
Cons
First read after cache miss is slow Possible stale window (very short)
2. Write-Through Cache
Concept
Every write goes through cache first, then DB.
Flow
Client → Cache → Database
Where it is used
Strong consistency systems Limited financial systems Some controlled caching layers
Pros
Cache always consistent No stale data
Cons
Slower writes Higher write cost
3. Write-Behind (Write-Back) Cache
Concept
Write goes to cache first, DB update happens asynchronously.
Flow
Client → Cache (ACK immediately) ↓ async Database
Where it is used
Gaming leaderboards Analytics systems High-throughput systems
Pros
Very fast writes High performance
Cons
Risk of data loss Complex recovery logic
4. TTL-Based Invalidation
Concept
Cache automatically expires after a fixed time.
Example
user:123 → expires in 5 minutes
Where it is used
Everywhere as fallback safety Session caching API response caching
Pros
Very simple No coordination needed
Cons
Data may be stale until expiration
5. Event-Driven Invalidation
Concept
When database changes, an event is published to notify cache systems to invalidate or update data.
Flow
Service A → Database update → Event (UserUpdated) ↓ Cache Service → delete/update cache
Technologies
Kafka RabbitMQ AWS SNS/SQS Redis Pub/Sub
Where it is used
Uber-like systems Airbnb-like systems Large-scale microservices
Pros
Decoupled architecture Scales well Works across services
Cons
Eventual consistency only Possible event delay or loss
Production fixes
Retry mechanisms Idempotent consumers Dead Letter Queue (DLQ) Event versioning
6. Hybrid Pattern (Real Production Standard)
Concept
Real systems combine multiple patterns together.
Typical architecture
Write Path: Client → Service → Database → publish event → delete cache Read Path: Client → Service → Cache ↓ miss Database → Cache Safety Net: TTL expiration always enabled
Why hybrid is used
Each pattern solves a different problem:
Cache-aside → simplicity Event-driven → consistency TTL → safety fallback
7. Outbox Pattern (Reliability Layer)
Concept
Ensures database updates and events are consistent.
Flow
Service → Database transaction → Outbox table insert Worker → reads outbox → publishes event
Why it is used
Prevents lost events Ensures reliable event delivery
Where it is used
Large microservices systems Financial systems E-commerce platforms
8. Common Production Problems
1. Cache Stampede
Many requests miss cache at the same time.
Fix:
Mutex lock Single-flight Request coalescing
2. Race Condition (Stale Cache Overwrite)
Old data overwrites new cache value.
Fix:
Versioning Timestamp checks Event ordering
3. Event Loss
Fix:
Kafka persistence Retry + DLQ Outbox pattern
9. Summary
In real production systems:
Cache-aside is the base Event-driven invalidation handles updates TTL provides safety net Outbox ensures reliability
Final Mental Model
Event-driven invalidation ↓ Cache-aside ←→ Database ↓ TTL fallback ↓ Outbox pattern (reliability)
