Get started with the Redis integration
RedisĀ® is the world's fastest data platform for caching, vector search, and NoSQL databases. The Aspire Redis integration enables you to connect to existing Redis instances, or create new instances from .NET with the docker.io/library/redis container image.
Note
This page covers the Redis integration. For Garnet and Valkey (Redis-compatible alternatives), see their respective pages.
In this introduction, you'll see how to install and use the Aspire Redis integrations in a simple configuration. If you already have this knowledge, see Redis hosting integration for full reference details.
Note
To follow this guide, you must have created an Aspire solution to work with. To learn how to do that, see Build your first Aspire app.
Set up hosting integration
To begin, install the Aspire Redis hosting integration in your Aspire AppHost project:
dotnet add package Aspire.Hosting.RedisNext, in the AppHost project, create instances of Redis resources and pass them to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);
builder.Build().Run();
When Aspire adds a container image to the AppHost, it creates a new Redis instance on your local machine.
Tip
If you'd rather connect to an existing Redis instance, call AsExisting
instead.
Set up client integration
To get started with the Aspire Redis client integration, install the package:
dotnet add package Aspire.StackExchange.RedisIn the Program.cs file of your client-consuming project, call the AddRedisClient extension method to register an IConnectionMultiplexer for use via the dependency injection container:
builder.AddRedisClient(connectionName: "cache");
Tip
The connectionName parameter must match the name used when adding the Redis
resource in the AppHost project.
You can then retrieve the IConnectionMultiplexer instance using dependency injection:
public class ExampleService(IConnectionMultiplexer connectionMux)
{
// Use connection multiplexer...
}
See also
[!TIP] Registered trademark Redis is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Any use by Microsoft is for referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and Microsoft.