SOLID & Design Patterns Interview Question #94
What is the Cache-Aside Pattern?
DDD Boundaries, Resilience, Caching & Practical ASP.NET Core Design Mid-Level Intermediate
Quick Interview Answer
In Cache-Aside, application code first checks the cache. On a cache miss, it reads from the primary data source and then stores the result in the cache for future requests.
Detailed Explanation
In Cache-Aside, application code first checks the cache. On a cache miss, it reads from the primary data source and then stores the result in the cache for future requests.
When data changes, the application must update or invalidate the relevant cache entries. Cache expiration, stale data tolerance, and cache stampedes should be considered explicitly.
Code Example
var key = $"product:{id}";
var product = await cache.GetAsync<Product>(key);
if (product is null)
{
product = await repository.GetByIdAsync(id);
await cache.SetAsync(key, product);
}
return product;