Skip to content

Instantly share code, notes, and snippets.

@JonCole
Last active March 25, 2021 15:55
Show Gist options
  • 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);

@JonCole
Copy link
Author

JonCole commented Apr 5, 2016

Some systems have more advanced requirements, such as using a pool of ConnectionMultiplexers, and it is perfectly fine for such apps to make the decision to take ownership of the connection management. For most of the customers that I work with on these types of issues, setting abortConnect to false greatly improves the behavior and is sufficient.

@gauravsatpute
Copy link

gauravsatpute commented Sep 25, 2019

I have set the AbortOnConnectFail to false. But still I am not able connect to others slaves server.
My Sever configuration is like following
1.) Master M1
2.)slaves S1
3.)slaves S2
When My master is down I am getting this exception. As my S1 and S2 are in running mode.
Following is my code :
services.AddSingleton(provider => ConnectionMultiplexer.Connect(redisConfiguration.Connection.Configuration));
services.AddSingleton(provider => ConnectionMultiplexer.Connect(redisConfiguration.Connection.Configuration).GetDatabase());
please help me .
thanks in advance .

@nicolas-2008
Copy link

@gauravsatpute,
You are using singleton for database instance which is not recommended

@Deepfreezed
Copy link

@gauravsatpute

This info is from StackExchange.Redis docs: "ConnectionMultiplexer does a lot, it is designed to be shared and reused"
https://stackexchange.github.io/StackExchange.Redis/Basics.html

If not singleton, how can this be shared globally within an application?

@darcon77
Copy link

@Deepfreezed

Remove this: services.AddSingleton(provider => ConnectionMultiplexer.Connect(redisConfiguration.Connection.Configuration).GetDatabase());

Instead get an instance of ConnectionMultiplexer DI container and call GetDatabase() to get IDatabase instance wherever you need and dispose right after the use.

@Deepfreezed
Copy link

@darcon77 Got it, Thanks !

Anyone knows how to reconnect when using Microsoft IDistributedCache (Microsoft.Extensions.Caching.StackExchangeRedis). I can get the Redis connection re-established using above solution. Although cannot make any queries. Get error below:

UnableToConnect on X.X.X.X:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0

@krisvijaykb
Copy link

@Deepfreezed, Facing the same issue. Any luck in resolving it?

@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