.NET Architecture & System Design Interview Question #27

What is the Cache-Aside pattern?

Caching, CDN, Queues & Asynchronous Processing Junior Beginner

Quick Interview Answer

Cache-Aside is a common caching pattern in which the application first checks the cache. On a miss, it loads the data from the source, stores the result in the cache, and returns it.

Detailed Explanation

Cache-Aside is a common caching pattern in which the application first checks the cache. On a miss, it loads the data from the source, stores the result in the cache, and returns it.

The application is responsible for cache population and invalidation. This pattern is simple and flexible, but stale data can appear if the underlying data changes without the cache entry being removed or refreshed.

Code Example

var value = await cache.GetAsync(key);
if (value is null)
{
    value = await repository.GetAsync(id);
    await cache.SetAsync(key, value);
}
return value;