Skip to content

Instantly share code, notes, and snippets.

@JonCole
Last active March 25, 2021 15:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonCole/36ba6f60c274e89014dd to your computer and use it in GitHub Desktop.
Save JonCole/36ba6f60c274e89014dd to your computer and use it in GitHub Desktop.
StackExchange.Redis - "disconnected multiplexer" error

The Symptom

An application is using StackExchange.Redis to connect to their Redis instance and everything is working fine until suddenly exceptions like this start happening.

It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail.
at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl(Func`1 multiplexerFactory, TextWriter log)
at MyApp.Web.Redis.StackExchangeClientConnection..ctor(ProviderConfiguration configuration)
at MyApp.Web.Data.GetInventoryData()

The Cause

Most often this is caused by some type of network blip that occurs where connectivity is briefly lost between the client and the server. StackExchange.Redis has a setting named AbortOnConnectFail that controls how it handles connectivity errors like this. The default value for this setting is "True", meaning that it will not reconnect automatically in some cases (like the one I described above).
Note: If you are using StackExchange.Redis version 1.1.* (or newer) AND you are hitting an Azure Redis instance, then AbortConnectFail is false by default. However, my personal recommendation would be to set this configuration option explicitly regardless.

The Solution

Setting AbortOnConnectFail to false will tell StackExchange.Redis to automatically reconnect in the background when the connection is lost for any reason.

Example using ConfigurationOptions class:

public static ConfigurationOptions GetConfiguration(string host, bool
ssl, string accessKey, string clientName)
{
var configuration = ConfigurationOptions.Parse(host);
configuration.Ssl = ssl;
configuration.ClientName = clientName;
configuration.AbortOnConnectFail = false;
configuration.Password = accessKey; return configuration;
}

Example using a Connection String:

var connectionString = "mycache.redis.cache.windows.net,abortConnect=false, ssl=true,password=..."
ConnectionMultiplexer.Connect(connectionString);

@Deepfreezed
Copy link

@krisvijaykb

I could not get it to work with Microsoft.Extensions.Caching.StackExchangeRedis. Switched to StackExchange.Redis and switched to using Microsoft.AspNetCore.DataProtection.StackExchangeRedis if you are on a load balanced server.

In ConfigureServices
services.AddSingleton(RedisConnection);

public static ConnectionMultiplexer RedisConnection
{
    get
    {
        return lazyConnection.Value;
    }
}

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{    
    var config = new ConfigurationOptions
    {
        EndPoints = { { [RedisIP], [RedisPORT] } },
        AbortOnConnectFail = false, //Setting AbortOnConnectFail to false will tell StackExchange.Redis to automatically reconnect in the background when the connection is lost for any reason
        ConnectRetry = 10,
        ReconnectRetryPolicy = new ExponentialRetry(5000),
        ClientName = "ApiClient"
    };

    return ConnectionMultiplexer.Connect(config);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment