Course contentsShow
← Cloud and Infrastructure
Lesson 2706 of 2,98958. Caching StrategiesFree lesson

Version-Based Cache Invalidation

Include version identifiers in cache keys to automatically invalidate old data on updates.

Version-Based Cache Invalidation

What you'll learn: How to use version identifiers in cache keys to automatically make old cached data irrelevant when content updates.

The Core Idea

Version-based cache invalidation solves a common caching problem: instead of manually purging cached entries when data changes, you embed a version identifier directly into the cache key itself. When data updates, you increment the version number, which creates a new cache key. The old cached data remains untouched but becomes orphaned—it simply won't be requested anymore because everyone is now looking for the new version.

Think of it like adding a date to your file names: report_v1.pdf, report_v2.pdf, report_v3.pdf. You never delete the old versions; you just stop using them. The moment you save version 3, everyone naturally switches to requesting that filename.

How It Works in Practice

Instead of caching with a simple key like user:12345:profile, you include a version:

user:12345:profile:v3

When the profile updates, you bump the version to v4. Your application now requests user:12345:profile:v4, finds no cached data (cache miss), fetches fresh data from the database, and caches it under the new key. The v3 data sits in cache until its TTL expires, but it's effectively invalidated because nothing requests it anymore.

Benefits

  • No cache purge complexity: No need to track and delete specific keys
  • Zero invalidation failures: Can't fail to invalidate what you're not trying to delete
  • Works across distributed caches: Multiple cache nodes don't need coordination
  • Version rollback friendly: Can revert to previous versions if needed

Key Takeaway: Version-based cache invalidation avoids explicit purging by making version numbers part of the cache key—new versions automatically create new cache entries, leaving old ones unused and harmless.