Cache Invalidations
π‘ Concept Name
Cache Invalidation refers to the process of removing or updating stale data in a cache to keep the cached information fresh and reliable.
π§ Analogy
Think of a refrigerator storing leftovers β if you donβt throw them out in time, they spoil. Similarly, cache invalidation ensures outdated cached data is cleared, maintaining data freshness.
π§ Technical Explanation
- π TTL (Time-To-Live): Automatically expires cache entries after a set duration.
- β»οΈ LRU (Least Recently Used): Evicts least accessed data to free space for new entries.
- π Write-through / Write-behind: Synchronizes cache with the data source on updates.
- π Manual Invalidation: Explicitly removes cache entries when data changes.
- β οΈ Without proper invalidation, applications risk serving outdated or incorrect data.
π― Invalidation Strategies
- β³ TTL-based expiration for predictable cache refresh cycles.
- π LRU policy to discard least recently used items first.
- π Cache-aside (lazy loading) invalidates and reloads data on demand.
- π οΈ Manual invalidation triggered by data changes or events.
- π Signals or hooks notify cache layers about updates for consistency.
π» Code Example
// Manual cache invalidation using IMemoryCache
public class ProductService
{
private readonly IMemoryCache _cache;
public ProductService(IMemoryCache cache)
{
_cache = cache;
}
public void UpdateProduct(int id, string newData)
{
UpdateInDatabase(id, newData);
_cache.Remove($"product_{id}"); // Remove stale cache entry
}
private void UpdateInDatabase(int id, string data)
{
// Database update logic here
}
}
β Interview Q&A
Q1: What is cache invalidation?
A: The process of removing or expiring stale data in the cache to maintain accuracy.
Q2: Why is cache invalidation important?
A: It prevents users from seeing outdated or incorrect information.
Q3: What are common invalidation techniques?
A: TTL expiration, manual removal, LRU eviction, and event-based invalidation.
Q4: What happens if invalidation is neglected?
A: The system may serve stale or inconsistent data.
Q5: Explain the LRU policy.
A: Least Recently Used evicts the cache entries that haven't been accessed recently to make space for new data.
π MCQs
Q1. What is cache invalidation?
- Removing outdated cache entries
- Compressing cache
- Resizing cache
- Indexing cache
Q2. What does TTL mean?
- Total Table Load
- Time To Live
- Token To Lock
- Temporary Table Label
Q3. Which strategy uses usage patterns?
- TTL
- LRU
- FIFO
- Manual
Q4. What happens if cache is never invalidated?
- Nothing
- More speed
- Stale data is served
- Increased accuracy
Q5. What .NET interface supports invalidation?
- IDictionary
- IMemoryCache
- IInvalidator
- IDataCache
Q6. Which method removes a cache entry?
- Delete()
- Purge()
- Remove()
- Evict()
Q7. Which policy discards least used items?
- MRU
- LRU
- TTL
- RoundRobin
Q8. When should manual invalidation be used?
- During startup
- On error
- After DB update
- Never
Q9. What is the default expiration method in IMemoryCache?
- SlidingWindow
- AbsoluteExpiration
- HardExpire
- None
Q10. What is signal-based invalidation?
- Preloaded cache
- Cache is notified on data change
- Data polling
- Flush by default
π‘ Bonus Insight
Cache invalidation is complex but crucial. Balancing data freshness and system performance is key to effective caching strategies.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!