'No connection is available to service this operation (Redis Client )

My app is hosted in azure and i am using azure redis cache.

RedisClient version Redis.StrongName - 1.2.6 .Net Core - 2.1 .Net Framework 4.7.1

My app is getting intermittent Redis Connection exception, and it gets resolved once i restart the app service. I have 1 - secondary and 1 - primary instance, which Redis cache of 6GB capacity

This is exception i got from my client

outer message: No connection is available to service this operation: HSETNX KEYNAME; SocketClosed on MY REDIS SERVER NAME/Interactive, origin: ProcessReadBytes, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 280531s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablished, in: 0, ar: 0, last-heartbeat: 0s ago, last-mbeat: 0s ago, global: 0s ago, mgr: Inactive, err: never; IOCP: (Busy=1,Free=999,Min=8,Max=1000), WORKER: (Busy=1,Free=32766,Min=8,Max=32767), Local-CPU: n/a

Inner Message: SocketClosed on MY REDIS SERVER NAME/Interactive, origin: ProcessReadBytes, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 280531s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablished, in: 0, ar: 0, last-heartbeat: 0s ago, last-mbeat: 0s ago, global: 0s ago, mgr: Inactive, err: never

Not sure what went wrong, it is resolved once I restarted the app service.

Please guide me on What went wrong? How to debug Redis exceptions? How to interpret the exception messages?

I referred some already posted issues in stackexchange.Redis, but nothing providing good info on the root cause of this issue https://github.com/StackExchange/StackExchange.Redis/issues/559

My CPU percentage and Redis Server Load, looks fine no anomolies

need to find out the root cause what went wrong and



Solution 1:[1]

Under Azure portal check the connected clients while performing any operations on Redis. In my case, I was sending around 4k request without disposing the object which results in socket failure. After implementing IDisposable interface, I am able to process 10k/20k request without any issue

  1. Update your connection string

    • Set abortConnect to false
    • Adjust your syncTimeout and asyncTimeout as needed.
    • If you are using a secure TLS connection set the ssl=True,sslprotocols=tls12 in your configuration to force it to the latest version.
  2. Upgrade your StackExchange.Redis nuget package to latest if you can.

  3. Set the config option for ReconnectRetryPolicy in your C# code to be ExponentialRetry https://stackexchange.github.io/StackExchange.Redis/Configuration.html#reconnectretrypolicy

    private static readonly Lazy<ConfigurationOptions> configOptions  = new Lazy<ConfigurationOptions>(() =>
    {
        var connections = ConfigurationManager.ConnectionStrings["redis-connection"].ConnectionString;
        var configOptions = ConfigurationOptions.Parse(connections);
    
        configOptions.ClientName = "MyApp-RedisCacheProvider";
        //configOptions.SyncTimeout = 100000; // don`t do this in code, set it in your connection string
        //configOptions.AbortOnConnectFail = false; // don`t do this in code, set it in your connection string
    
        /*
         * The default is LinearRetry which can cause congestion at virtually the same time on multiple parallel threads.
         * Use ExponentialRetry so that a degree of randomness is used in the timing across multiple threads.
         */
        configOptions.ReconnectRetryPolicy = new ExponentialRetry(5000, 10000);
    
        return configOptions;
    });
    
     private static readonly Lazy<ConnectionMultiplexer> connection = new Lazy<ConnectionMultiplexer>(
             () => ConnectionMultiplexer.Connect(configOptions.Value));
    
  4. Implement IDisposable to dispose the object after its use

         public void Dispose()
         {
             try {
                 if (connection.IsValueCreated)
                 {
                     connection.Value.Dispose(); 
                 }
             } catch { }
         }
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sanju Kushwaha