59

I have the following piece of code to connect to azure redis cache.

public class CacheConnectionHelper
{
    private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache);
    });

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

And I use it this way

public static List<Models.Module> GetModules()
{
    IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
    List<Models.Module> listOfModules = new List<Models.Module>();        
    listOfModules = (List<Models.Module>)cache.Get("ApplicationModules");
    if (listOfModules == null)
    {
        listOfModules = dbApp.Modulos.ToList();
        cache.Set("ApplicationModules", listOfModules, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
        return listOfModules;
    }
    else {
        return listOfModules;
    }
}

However 1 or 2 times per day I get this exception:

Additional information: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING

The question is how can I refactor this code to go to the database in case the cache connection fails?

Matthias Müller
  • 444
  • 6
  • 15
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

10 Answers10

100

The error you are getting is usually a sign that you have not set abortConnect=false in your connection string. The default value for abortConnect is true, which makes it so that StackExchange.Redis won't reconnect to the server automatically under some conditions. We strongly recommend that you set abortConnect=false in your connection string so that SE.Redis will auto-reconnect in the background if a network blip occurs.

Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
JonCole
  • 2,902
  • 1
  • 17
  • 19
  • 3
    thanks , this connection string worked for me 172.17.0.2:12000,abortConnect=false – Sagar Nov 08 '20 at 18:36
  • 1
    Very useful, thanks. Too bad the default setting is `true`. – Ceco Feb 02 '21 at 21:14
  • It may also be necessary to increase the default timeouts, e.g. `"Redis": "localhost,abortConnect=false,connectTimeout=30000,responseTimeout=30000"` – testpattern Oct 14 '21 at 13:11
21

For beginners who dive in other's code an face this problem:

if (RedisConn == null)
{ 
    ConfigurationOptions option = new ConfigurationOptions
    {
        AbortOnConnectFail = false,
        EndPoints = { redisEndpoint }
    };
    RedisConn = ConnectionMultiplexer.Connect(option);
}
Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
danigitman
  • 730
  • 8
  • 11
  • 1
    Perfect... but what is the failsafe? If it fails on the first network blip we are ok with letting it try again but not indefinitely. Are there additional options? Google pointed me here. – Glenn Jun 20 '17 at 20:20
  • 3
    Found it, it's a ConfigurationOption of ConnectRetry set to the number of retries. Not sure what the default is set to. – Glenn Jun 20 '17 at 20:25
  • 2
    Default is 3 for ConnectRetry - https://stackexchange.github.io/StackExchange.Redis/Configuration.html – vshale Jan 12 '18 at 21:11
5

You should also pay attention to the last part of your error message, as it seems to provide very useful details about the reason why connection have failed.

In your case:

It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING

My case:

It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. Timeout

Zé Carlos
  • 3,627
  • 5
  • 43
  • 51
2

For those maintaining older codebases, you may run into "It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. UnableToResolvePhysicalConnection on PING"

Once I upgraded to a more recent nuget package the error was still present but I got more error information: "The client and server cannot communicate, because they do not possess a common algorithm".

Once I applied the registry keys mentioned here I was ok. For those that don't wish to make that global change I believe there has been a PR for a setting.

JasonCoder
  • 1,126
  • 2
  • 12
  • 24
2

I fixed this by changing:

CacheSettings:ConnectionString=basketdb:6379

to

CacheSettings__ConnectionString=basketdb:6379

in the docker-compose.override.yml

Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
1

This problem was solved in a new release, the version 1.2.6 - you can see in Here

Rathma
  • 1,196
  • 2
  • 33
  • 65
Roger Gusmao
  • 3,788
  • 1
  • 20
  • 17
0

I fixed this by changing my connection string from localhost:6379 to 127.0.0.1:6379

Markus
  • 1,020
  • 14
  • 18
0

For me, the connection string was incorrect. Adding the correct connection string details worked with stackexchange.redis 2.1.58

Nilay
  • 259
  • 2
  • 13
0

You can use it this way.

public class RedisService
{
    private readonly string _host;
    private readonly int _port;
    private ConnectionMultiplexer _connectionMultiplexer;

    public RedisService(string host, int port)
    {
        _host = host;
        _port = port;
    }

    public void Connect() => _connectionMultiplexer = ConnectionMultiplexer.Connect($"{_host}:{_port}");
}

enter image description here

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Veysel
  • 1
  • 1
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 11:06
0

Do ensure that you already have Redis set up. Here are instructions links for the respective OS:

Mac OS: https://gist.github.com/tomysmile/1b8a321e7c58499ef9f9441b2faa0aa8

Windows: https://dev.to/divshekhar/how-to-install-redis-on-windows-10-3e99