It was not possible to connect to the redis server(s)to create a disconnected multiplexer

Today in this article, we will cover below aspects,

Issue Description

Redis connection throws an error,

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()

Resolution

I found the error is due to configuration settings required for a Redis connection being missing.

Before trying this fix, please make sure the Redis server is up and running.

It’s recommended that if you set abortConnect=false in your connection string.

StackExchange.Redis has a setting called AbortOnConnectFail which handles connectivity errors.

Please note that the default value for AbortOnConnectFail setting flag is “True”, meaning connection resiliency will not be in effect for any transient error like network bleep, etc. Due to this application will not re-connect automatically in some cases.

As default behavior, Redis will auto-reconnect if a network has any transient error occurs.

Transient erros are most common in Cloud enviornment due to shared resources.

Setting abortConnect as false will resolve the issue and connection resiliency will be implemented.

Note – when AbortOnConnectFail = false setting is used then a total of 3 retry attempts during the initial connect will be performed. However, this setting can be customized as per need.

I was able to fix the error by using the below code for C# applicaiton

            
               var configurationOptions = new ConfigurationOptions
                {
                    EndPoints = { $"{Configuration.GetValue<string>("RedisCache:Host")}: 
                    {Configuration.GetValue<int>("RedisCache:Port")}" },
                    Ssl = true,
                    AbortOnConnectFail = false,
                };

                var multiplexer = ConnectionMultiplexer.Connect(configurationOptions); ;

Note : For Azure Redis, the cloud instance property is already set with abortConnect=false  as the default behavior.

However, It may not be the case for other cloud providers.

Reference:

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *