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 named provider
Section titled “Configure a named provider”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].
Configure the default provider
Section titled “Configure the default provider”Use AddRedisGrainStorageAsDefault when grains should use Redis without specifying a storage name:
siloBuilder.AddRedisGrainStorageAsDefault(options =>{ options.ConfigurationOptions = new ConfigurationOptions { EndPoints = { "localhost:6379" } };});Storage behavior
Section titled “Storage behavior”RedisStorageOptions supports:
- ConfigurationOptions for the StackExchange.Redis connection.
- DeleteStateOnClear to delete the Redis key instead of resetting the record.
- EntryExpiry to expire records.
- CreateMultiplexer to supply a shared or custom
IConnectionMultiplexer. - GetStorageKey to customize key generation.
- GrainStorageSerializer to customize the stored representation.
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.
