We're using the StackExchange.Redis client. We are also using Sentinel which tells us when the Redis master changes. Since we've create our connection (as a Lazy<IConnectionMultiplexer>
as recommended) we want to know what's the best way to change the connection information to now point to the new Redis master?
We see a few options:
- When the notification comes from Sentinel, create a new
Lazy<IConnectionMultiplexer>
- Change the connection information on the existing connection multiplexer
Is #2 even possible?
Our current way:
private Lazy<IConnectionMultiplexer> CreateRedisConnection()
{
return new Lazy<IConnectionMultiplexer>(
() =>
{
ConnectionMultiplexer multiplexer = null;
//Connect to Sentinel so we can find the redis master
ConnectionMultiplexer sentinelMultiplexer = ConnectionMultiplexer.Connect(SentinelOptions);
string masterNodeAddress = GetMasterNodeAddress(sentinelMultiplexer);
return ConnectionMultiplexer.Connect(masterNode);
}
);
}
And then upon notification, we simply re-call CreateRedisConnection()
.
But this fully recreates the connection multiplexer vs a more lightweight approach (which might not be possible).