I'm not familiar with Elasticache, but the StackExchange.Redis ConnectionMultiplexer
will automatically retry in the background if the connection is dropped, and it will discover recovered nodes.
Of course, upon failures you will get exceptions when accessing the database, but if you treat the errors correctly you'll not need to re-create the ConnectionMultiplexer
.
I used the following code to test this in cluster-mode and standalone-mode:
var mul = ConnectionMultiplexer.Connect("192.168.15.15:7000,192.168.15.15:7001,...,connectRetry=10,syncTimeout=5000,abortConnect=false,keepAlive=10,allowAdmin=true");
RETRY:
Thread.Sleep(1000);
var k = Guid.NewGuid().ToString();
var v = Guid.NewGuid().ToString();
try
{
var db = mul.GetDatabase();
db.StringSet(k, v);
if (db.StringGet(k).ToString() != v)
{
throw new OperationCanceledException("ABORT");
}
}
catch(RedisServerException ex)
{
Console.WriteLine("Redis Server Exception {0}", ex.Message);
goto RETRY;
}
catch(RedisConnectionException ex)
{
Console.WriteLine("Redis Connection Exception {0}", ex.Message);
goto RETRY;
}
catch(TimeoutException ex)
{
Console.WriteLine("Timeout Exception {0}", ex.Message);
goto RETRY;
}
Console.WriteLine("OK");
goto RETRY;
I've received three types of exceptions when shutting down/crashing the different servers: RedisServerException
, RedisConnectionException
and TimeoutException
. And stopped receiving exceptions once the server/cluster is up and running again.