Skip to content

Redis grain persistence

The Microsoft.Orleans.Persistence.Redis package stores grain state in Redis. The provider uses atomic Redis scripts and ETags to reject stale writes.

Configure a provider with AddRedisGrainStorage:

var builder = Host.CreateApplicationBuilder();
builder.UseOrleans(siloBuilder =>
{
siloBuilder.AddRedisGrainStorage(
name: "redis",
configureOptions: options =>
{
options.ConfigurationOptions = new ConfigurationOptions
{
EndPoints = { "localhost:6379" },
AbortOnConnectFail = false
};
});
});
using var host = builder.Build();

The provider name must match the storage name in [PersistentState].

Use AddRedisGrainStorageAsDefault when grains should use Redis without specifying a storage name:

siloBuilder.AddRedisGrainStorageAsDefault(options =>
{
options.ConfigurationOptions = new ConfigurationOptions
{
EndPoints = { "localhost:6379" }
};
});

RedisStorageOptions supports:

The default key format is {ServiceId}/state/{grainId}/{grainType}.

Changing the key function or serializer doesn’t migrate existing Redis entries. Plan a data migration or retain compatibility with the previous representation.