Set up Redis distributed caching in the AppHost
This article covers the minimum AppHost configuration required to use the Redis distributed caching client integration. The underlying hosting integration is the same Aspire.Hosting.Redis package used by the regular Redis integration — there is no separate “distributed” hosting package. For the complete AppHost API surface — data volumes, data bind mounts, persistence, Redis Insight, Redis Commander, parameters, custom environment variables, existing instances, and hosting health checks — see the Redis Hosting integration reference.
If you’re new to Redis distributed caching with Aspire, start with Get started with Redis distributed caching. For how consuming C# apps register IDistributedCache, see Connect to Redis distributed caching.
Installation
Section titled “Installation”Install the 📦 Aspire.Hosting.Redis NuGet package in your AppHost project:
aspire add redisLearn more about aspire add in the command reference.
Or, choose a manual installation approach:
#:package Aspire.Hosting.Redis@*<PackageReference Include="Aspire.Hosting.Redis" Version="*" />aspire add redisLearn more about aspire add in the command reference.
This updates your aspire.config.json with the Redis hosting integration package:
{ "packages": { "Aspire.Hosting.Redis": "13.3.0" }}Add Redis resource
Section titled “Add Redis resource”Once the hosting integration is installed, add a Redis resource in your AppHost and reference it from the apps that need distributed caching:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(cache);
// After adding all resources, run the app...import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const cache = await builder.addRedis("cache");
await builder.addNodeApp("api", "./api", "index.js") .withReference(cache);
// After adding all resources, run the app...-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
docker.io/library/redisimage, it creates a new Redis instance on your local machine. -
The AppHost reference call configures a connection in the consuming project named after the referenced Redis resource, such as
cachein the preceding example. -
The consuming C# project calls
AddRedisDistributedCache("cache")in Program.cs to registerIDistributedCacheusing that connection. See Connect to Redis distributed caching for details.
Full AppHost API reference
Section titled “Full AppHost API reference”The Redis distributed caching integration shares the Redis hosting integration with the rest of the Redis ecosystem. For all available AppHost APIs, see the Redis Hosting integration reference, which covers:
WithDataVolume— persist Redis data outside the container lifecycle.WithDataBindMount— bind-mount a host directory into the container.WithPersistence— configure snapshotting interval and key-change threshold.WithRedisInsight/WithRedisCommander— attach management UIs.AddParameterfor explicit port and password — override auto-generated values.- Custom environment variables — map connection properties to app-specific variable names.
AsExisting— connect to an externally managed Redis instance instead of starting a container.- Hosting health checks — automatically verify the Redis container is reachable.