Beginner LevelSystem Design & Architecture
Why is Cache Invalidation considered one of the hardest problems in computer science?
β Interview Prompt: Explain Cache Invalidation, stale data reads, Event-Driven Cache Eviction, Time-to-Live (TTL), and synchronization challenges.
Phil Karlton famously noted: "There are only two hard things in Computer Science: cache invalidation and naming things."
Why Cache Invalidation is Difficult:
When data mutates in the primary database, all distributed cache copies must be updated or evicted immediately. If invalidation fails or experiences network lag, clients receive stale data.
β’
Mitigations: Set sensible Time-to-Live (TTL) expirations, and execute event-driven cache purges (via CDC or Pub/Sub) on data mutations.
Code Implementation Example
typescript
// Event-Driven Cache Invalidation on User Profile Update
export async function updateUserProfile(userId: string, data: ProfileData) {
await db.user.update({ where: { id: userId }, data });
// Immediately invalidate cached profile key!
await redis.del('user:\${userId}');
}π‘Evicting cached entries immediately upon database updates.
π― Interview Key Takeaway
Combine TTL expirations with event-driven cache purges to prevent stale data reads.
#System Design#Cache Invalidation#TTL#Caching#Beginner
Back to all System Design & Architecture Questionsβ