Performance is one of the most important characteristics of a modern web application. Users expect APIs and web applications to respond almost instantly, regardless of traffic volume. However, repeatedly querying databases, calling external APIs, or performing expensive calculations for every request can quickly become a bottleneck.
Caching is one of the simplest and most effective ways to improve application performance. By temporarily storing frequently requested data, applications can significantly reduce database load, decrease response times, and improve scalability.
ASP.NET Core 10 provides multiple caching mechanisms, each designed for different scenarios. Understanding when to use In-Memory Cache, Distributed Cache, and Output Cache is essential for building high-performance production applications.
In this article, you'll learn how each caching strategy works, where it should be used, and how to implement caching without introducing stale data or consistency issues.
Why Caching Matters
The Cost of Repeated Operations
Consider an API that retrieves product information.
Without caching, every request follows this path:
Client
│
▼
ASP.NET Core API
│
▼
Business Service
│
▼
SQL Database
If thousands of users request the same product every minute, the database executes the same query repeatedly.
With caching:
Client
│
▼
ASP.NET Core API
│
├────────► Cache
│ │
│ ▼
│ Cached Result
│
└────────► Database (Cache Miss)
Most requests are served directly from the cache, reducing database traffic and improving response times.
In-Memory Caching
What Is In-Memory Cache?
In-Memory Cache stores data inside the application's process memory.
Register the cache service:
builder.Services.AddMemoryCache();
Using the cache:
public class ProductService
{
private readonly IMemoryCache _cache;
public ProductService(IMemoryCache cache)
{
_cache = cache;
}
public async Task<Product?> GetProductAsync(int id)
{
return await _cache.GetOrCreateAsync(
$"product-{id}",
async entry =>
{
entry.AbsoluteExpirationRelativeToNow =
TimeSpan.FromMinutes(5);
return await LoadProductFromDatabase(id);
});
}
}
Why Use In-Memory Cache?
The first request retrieves data from the database.
Subsequent requests within five minutes are served directly from memory, reducing latency and unnecessary database queries.
This approach works well for single-server applications where cached data doesn't need to be shared across multiple instances.
Distributed Caching
What Is Distributed Cache?
Distributed Cache stores cached data in an external service such as Redis.
Unlike in-memory caching, multiple application instances share the same cached data.
Configuration:
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration =
builder.Configuration.GetConnectionString("Redis");
});
Using the distributed cache:
public class ProductCache
{
private readonly IDistributedCache _cache;
public ProductCache(IDistributedCache cache)
{
_cache = cache;
}
}
Why Use Distributed Cache?
Distributed caching ensures all application instances access the same cached information.
This is particularly valuable for:
Load-balanced APIs
Kubernetes deployments
Cloud-native applications
Microservices
Without a distributed cache, each application instance would maintain its own cache, leading to inconsistent data and lower cache hit rates.
Output Caching
What Is Output Caching?
Output Caching stores complete HTTP responses.
Enable Output Cache:
builder.Services.AddOutputCache();
Configure middleware:
app.UseOutputCache();
Apply caching to an endpoint:
app.MapGet("/products", GetProducts)
.CacheOutput();
Why Use Output Cache?
Instead of executing the endpoint for every request, ASP.NET Core returns the cached HTTP response.
This avoids executing:
Business logic
Database queries
Serialization
Response generation
Output caching is especially effective for frequently requested read-only endpoints.
End-to-End Implementation
Consider an e-commerce application.
Architecture:
Customer
│
▼
ASP.NET Core API
│
┌───┴─────────────┐
▼ ▼
Output Cache Product Service
│
Distributed Cache
│
▼
SQL Database
Workflow:
A client requests product details.
Output Cache checks for a cached HTTP response.
If unavailable, the Product Service checks Redis.
If Redis contains the data, it is returned immediately.
Otherwise, the database is queried.
The result is stored in Redis.
The HTTP response is cached.
Future requests are served without accessing the database.
This layered approach minimizes expensive operations while maintaining fast response times.

Join the conversation! Your thoughts help the community grow.