
Introduction
When you build an ASP.NET Core application, one of the biggest challenges is performance. As your application grows and more users start using it, your database gets hit again and again for the same data. This slows down your application and increases server load.
This is where distributed caching with Redis helps.
Instead of calling the database every time, you store frequently used data in a fast in-memory system like Redis. The next time someone requests the same data, it is returned instantly from the cache instead of the database.
In this guide, you will learn step-by-step how to use Redis distributed caching in ASP.NET Core in simple and practical terms.
What is Distributed Caching?
Distributed caching means storing your cached data outside your application, usually in a separate service like Redis.
In a normal (in-memory) cache, data is stored inside a single server. If your application runs on multiple servers, each server will have its own cache, which can cause inconsistency.
But in distributed caching:
All servers share the same cache
Data remains consistent across the application
It works well in scalable environments
In simple words, distributed cache is like a shared memory that all your application servers can use.
What is Redis?
Redis is a very fast, in-memory data store that is widely used for caching.
Think of Redis as a super-fast dictionary where:
You store data using a key
You retrieve data using the same key
Why developers prefer Redis:
It is extremely fast because it stores data in memory
It supports simple key-value storage
It is easy to integrate with ASP.NET Core
It works great for scalable and cloud-based applications
Prerequisites
Before starting, make sure you have the following ready:
.NET SDK installed on your system
A working ASP.NET Core project
Redis installed locally or access to a cloud Redis service
Basic understanding of controllers and services in ASP.NET Core
If Redis is not installed locally, you can use Docker or any cloud provider.
Step 1: Install Required NuGet Package
First, you need to install the Redis caching package.
Run the following command:
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
This package connects your ASP.NET Core application with Redis and enables distributed caching support.
Step 2: Configure Redis in ASP.NET Core
Next, you need to tell your application how to connect to Redis.
Open your Program.cs file and add the following configuration:
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost:6379";
options.InstanceName = "SampleApp_";
});
Explanation in simple terms:
localhost:6379→ This is the address where Redis is runningInstanceName→ A prefix added to all cache keys (helps in organizing data)
If you are using cloud Redis, replace the localhost value with your connection string.
Step 3: Inject IDistributedCache Service
ASP.NET Core provides a built-in interface called IDistributedCache to work with Redis.
You need to inject it into your service or controller.
public class ProductService
{
private readonly IDistributedCache _cache;
public ProductService(IDistributedCache cache)
{
_cache = cache;
}
}
Join the conversation! Your thoughts help the community grow.