Course contentsShow
← System Design
Lesson 157 of 1,9194. Caching StrategiesFree lesson

Active Invalidation on Write

Explicitly removing or updating cache entries immediately when underlying data changes.

Active Invalidation on Write

What you'll learn: How to keep your cache accurate by immediately removing stale data the moment the underlying data changes.


What Is Active Invalidation?

Active invalidation on write means your system explicitly deletes or updates cache entries the instant you modify the source data. Instead of waiting for cached data to expire via TTL (which you learned in the previous lesson), you proactively clear or refresh the cache immediately after a database write, update, or delete operation.

How It Works

When your application writes new data to the database, it also sends a command to the cache layer (like Redis or Memcached) to remove the old cached entry. This ensures the next read will fetch fresh data from the database rather than serving stale information.

Example flow:

  1. User updates their profile (name change)
  2. Application writes the new name to the database
  3. Immediately after, application deletes user:123:profile from cache
  4. Next time someone requests that profile, cache misses, fetches from database, and caches the fresh data

Why Use Active Invalidation?

This approach guarantees strong consistency between your cache and database. Unlike TTL-based expiration where stale data might linger for seconds or minutes, active invalidation ensures users see changes instantly.

The Challenge

You must update both the database and cache reliably. If the cache invalidation fails but the database write succeeds, you've got stale data. This requires careful error handling and sometimes distributed transactions.

Key Takeaway: Active invalidation on write removes cache entries immediately when data changes, providing strong consistency at the cost of more complex write operations that must coordinate database and cache updates.