Cache Invalidation
ELI5 - The Vibe Check
There's an old joke that there are only two hard problems in computer science: cache invalidation, naming things, and off-by-one errors. Caching makes things fast by keeping a copy of data around, but the moment the real data changes, that copy becomes a lie. Cache invalidation is the never-ending job of deciding when to throw that stale copy away before it fools someone.
Real Talk
Cache invalidation is the process of removing or updating cached data once the underlying source data changes, to prevent clients from reading stale values. Common strategies include time-based expiration (TTL), explicit invalidation on write, and event-driven invalidation triggered by change notifications, each trading off staleness risk against cache hit rate.
Show Me The Code
// Invalidate on write instead of waiting for the TTL
await db.user.update({ where: { id }, data });
await redis.del(`user:${id}`);
When You'll Hear This
"We ship a bug, users see stale data, and it's always cache invalidation." / "Set a short TTL instead of trying to invalidate perfectly on every write."
Related Terms
Caching
Caching is saving the result of a slow operation so you can reuse it quickly next time.
Change Data Capture
Change Data Capture (CDC) watches your database for changes and streams them as events.
Redis
Redis is an incredibly fast database that lives entirely in memory (RAM). It's used as a cache, a session store, and a message queue.