Back to results for “Performance”
.NET › Caching (In-Memory, Distributed & Redis)

What are common cache invalidation strategies, and why is cache invalidation considered a genuinely hard problem?

Intermediate

Answer

Common strategies include: TTL-based expiration (simplest, but stale data possible until expiry), explicit invalidation (removing/updating the cache entry immediately when the underlying data changes, requiring careful tracking of every code path that mutates data), and versioned/tagged cache keys (invalidating whole groups of related entries at once by bumping a version number embedded in the key). It's hard because every single place data can change must remember to invalidate the correct cache entries, and distributed systems make coordinating invalidation across multiple cache instances even trickier.
// Explicit invalidation on write
public async Task UpdateProductAsync(Product product) {
    await _repository.UpdateAsync(product);
    await _cache.RemoveAsync($"product-{product.Id}");  // must remember this every time!
}

// Versioned key strategy
var cacheKey = $"products-v{await GetCacheVersionAsync()}";
Real-world example A bug where stale product prices were shown for hours was traced to a bulk-update code path that updated the database directly via a script, bypassing the normal service method that would have invalidated the corresponding cache entries.

Common follow-ups: Why is 'there are only two hard things in computer science: cache invalidation and naming things' such a common joke?;How do cache tags/groups simplify bulk invalidation?