Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);

@RemiBou
Copy link

RemiBou commented Mar 11, 2016

There seems to have some bugs with this feature, and sometimes the client doesn't try to reconnect (We're using the last release nuget package). The best option is to handle the connection exception and reinstantiate the multiplexer.

Loading

@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.

Loading

@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 .

Loading

@nicolas-2008
Copy link

nicolas-2008 commented Feb 22, 2020

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

Loading

@Deepfreezed
Copy link

Deepfreezed commented Mar 13, 2020

@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?

Loading

@darcon77
Copy link

darcon77 commented Mar 17, 2020

@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.

Loading

@Deepfreezed
Copy link

Deepfreezed commented Mar 19, 2020

@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

Loading

@krisvijaykb
Copy link

krisvijaykb commented Mar 22, 2021

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

Loading

@Deepfreezed
Copy link

Deepfreezed commented Mar 25, 2021

@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);
});

Loading

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