ASP.NET Core Interview Question #183
What is cache invalidation?
Performance, Caching & HTTP Senior Advanced
Quick Interview Answer
Cache invalidation is the process of removing or replacing cached data when it is no longer valid. It is one of the most important challenges in cache design.
Detailed Explanation
Cache invalidation is the process of removing or replacing cached data when it is no longer valid. It is one of the most important challenges in cache design.
Common strategies include short expiration, explicit removal after updates, versioned cache keys, dependency-based invalidation, or event-driven invalidation. The strategy should reflect how stale the data is allowed to become.
A cache should generally be treated as an optimization rather than the authoritative source of business data.
Code Example
public async Task UpdateProductAsync(Product product)
{
await _repository.UpdateAsync(product);
_cache.Remove($"product:{product.Id}");
_cache.Remove("featured-products");
}