The fastest database query is the one you never make. Caching temporarily stores the results of expensive operations (complex database joins, external API calls, or HTML rendering) in fast-access memory so subsequent requests execute in milliseconds instead of seconds.
Data is stored directly in the physical RAM of the web server executing the request. It is blindingly fast because there is zero network latency or JSON serialization involved.
If you have 3 load-balanced web servers, and User A logs in on Server 1, their data is cached in Server 1's memory. If their next click routes to Server 2, Server 2 knows nothing about it. Use IMemoryCache strictly for single-instance apps or universally static data (like a list of US States).
// 1. Register in Program.cs
builder.Services.AddMemoryCache();
// 2. Use in Service
public class CatalogService
{
private readonly IMemoryCache _cache;
public CatalogService(IMemoryCache cache) => _cache = cache;
public async Task<List<Category>> GetCategoriesAsync()
{
// GetOrCreateAsync elegantly handles cache misses
return await _cache.GetOrCreateAsync("AllCategories", async entry =>
{
// Set expiration TTL to 1 hour
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
// Execute the slow DB query ONLY if "AllCategories" isn't in memory
return await _db.Categories.ToListAsync();
});
}
}
For scalable, enterprise environments (Azure, AWS), you must use a Distributed Cache. All web servers talk to a centralized caching server (usually Redis) over the network.
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
// 1. Register in Program.cs
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration.GetConnectionString("Redis");
options.InstanceName = "MyApp_";
});
// 2. Use in Service
public class PricingService
{
private readonly IDistributedCache _cache;
public PricingService(IDistributedCache cache) => _cache = cache;
public async Task<ProductPrice> GetPriceAsync(int productId)
{
var cacheKey = $"Prc_{productId}";
// Distributed cache ONLY stores bytes/strings. You must serialize manually.
var cachedString = await _cache.GetStringAsync(cacheKey);
if (cachedString != null)
{
return JsonSerializer.Deserialize<ProductPrice>(cachedString);
}
// Cache miss: Hit DB
var price = await _db.GetPriceComplexCalculationAsync(productId);
// Save to cache for next time
var options = new DistributedCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(10));
await _cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(price), options);
return price;
}
}
Instead of caching raw data, what if you just cache the final, rendered HTML response? .NET 7 introduced Output Caching (replacing the older ResponseCaching).
// Program.cs
builder.Services.AddOutputCache(options =>
{
// Define global policies
options.AddPolicy("CacheFor10Secs", policy => policy.Expire(TimeSpan.FromSeconds(10)));
});
app.UseOutputCache(); // Must be placed appropriately in the middleware pipeline
[OutputCache(PolicyName = "CacheFor10Secs")]
public IActionResult HeavyDashboard()
{
// This action will only execute once every 10 seconds.
// For all other requests, the framework intercepts the request and instantly returns
// the cached HTML straight from middleware.
return View(SlowDatabaseCall());
}
A "Cache Stampede" occurs when a highly searched cache key (e.g., "HomePageData") expires. Suddenly, 5,000 concurrent requests all register a "cache miss" simultaneously, and all 5,000 requests hit the database at the exact same millisecond, instantly crashing the database.
Solution (.NET 9 HybridCache): Modern ASP.NET Core uses locking patterns. The first thread to realize the cache is empty locks the cache key, goes to the database, and forces the other 4,999 threads to wait in line. Once the 1st thread populates the cache, all waiting threads read from the newly refreshed cache, saving the database.
Q: "When configuring cache expiry, what is the difference between Absolute Expiration and Sliding Expiration?"
Architect Answer: "Absolute Expiration defines a hard deadline—for instance, 10 minutes. Exactly 10 minutes after creation, the item is deleted, regardless of how popular it is. Sliding Expiration resets the timer every time the item is accessed. If set to 10 minutes, and I access it at minute 9, it is granted another 10 minutes of life. A dangerous trap is using *only* Sliding Expiration for rapidly updated items; if it's constantly accessed, it never expires, meaning the data becomes permanently stale. Best practice is to combine them: grant a 10-minute sliding window, but strictly enforce a 1-hour absolute cap."