.NET Architecture & System Design Interview Question #30

When should you use a distributed cache in ASP.NET Core?

Caching, CDN, Queues & Asynchronous Processing Junior Beginner

Quick Interview Answer

Use a distributed cache when multiple application instances need access to shared cached data or when cached state should survive the replacement of an individual web server.

Detailed Explanation

Use a distributed cache when multiple application instances need access to shared cached data or when cached state should survive the replacement of an individual web server.

ASP.NET Core provides IDistributedCache as an abstraction. Redis is a common implementation. The design must account for serialization, expiration, network failures, cache capacity, and the fact that the cache can be temporarily unavailable.

Code Example

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = configuration;
});

var bytes = await cache.GetAsync(key);