title: StackExchange.Redis timeout and "No connection is available to service this operation"
body: <p>I have the following issues in our production environment (Web-Farm - 4 nodes, on top of it Load balancer):</p>
  
  <p>1) <code>Timeout performing HGET key, inst: 3, queue: 29, qu=0, qs=29, qc=0, wr=0/0
  at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor``1 processor, ServerEndPoint server) in ConnectionMultiplexer.cs:line 1699</code> This happens 3-10 times in a minute</p>
  
  <p>2) <code>No connection is available to service this operation: HGET key at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor``1 processor, ServerEndPoint server) in ConnectionMultiplexer.cs:line 1666</code></p>
  
  <p>I tried to implement as Marc suggested (Maybe I interpreted it incorrectly) - better to have fewer connections to Redis than multiple.
  I made the following implementation:</p>
  
  <pre><code>public class SeRedisConnection
  {
      private static ConnectionMultiplexer _redis;
  
      private static readonly object SyncLock = new object();
  
      public static IDatabase GetDatabase()
      {
          if (_redis == null || !_redis.IsConnected || !_redis.GetDatabase().IsConnected(default(RedisKey)))
          {
              lock (SyncLock)
              {
                  try
                  {
                      var configurationOptions = new ConfigurationOptions
                      {
                          AbortOnConnectFail = false
                      };
                      configurationOptions.EndPoints.Add(new DnsEndPoint(ConfigurationHelper.CacheServerHost,
                          ConfigurationHelper.CacheServerHostPort));
  
                      _redis = ConnectionMultiplexer.Connect(configurationOptions);
                  }
                  catch (Exception ex)
                  {
                     IoC.Container.Resolve&lt;IErrorLog&gt;().Error(ex);
                      return null;
                  }
              }
          }
          return _redis.GetDatabase();
      }
  
      public static void Dispose()
      {
          _redis.Dispose();
      }
  }
  </code></pre>
  
  <p>Actually dispose is not being used right now. Also I have some specifics of the implementation which could cause such behavior (I'm only using hashes):
  1. Add, Remove hashes - async
  2. Get -sync</p>
  
  <p>Could somebody help me how to avoid this behavior?</p>
  
  <p>Thanks a lot in advance!</p>
  
  <p><strong>SOLVED - Increasing Client connection timeout after evaluating network capabilities.</strong></p>
  
  <p><strong>UPDATE 2</strong>: Actually it didn't solve the problem. When cache volume starting to get increased e.g. from 2GB.
  Then I saw the same pattern actually these timeouts were happend about every 5 minutes.
  And our sites were frozen for some period of time every 5 minutes until fork operation was finished.
  Then I found out that there is an option to make a fork (save to disk) every x seconds:</p>
  
  <pre><code>save 900 1
  save 300 10
  save 60 10000
  </code></pre>
  
  <p>In my case it was "save 300 10" - save in every 5 minutes if at least 10 updates were happened. Also I found out that "fork" could be very expensive. Commented "save" section resolved the problem at all. We can commented "save" section as we are using only Redis as "cache in memory" - we don't need any persistance.
  Here is configuration of our cache servers "Redis 2.4.6" windows port: <a href="https://github.com/rgl/redis/downloads" rel="noreferrer">https://github.com/rgl/redis/downloads</a></p>
  
  <p>Maybe it has been solved in recent versions of Redis windows port in MSOpentech: <a href="http://msopentech.com/blog/2013/04/22/redis-on-windows-stable-and-reliable/" rel="noreferrer">http://msopentech.com/blog/2013/04/22/redis-on-windows-stable-and-reliable/</a>
  but I haven't tested yet.</p>
  
  <p>Anyway StackExchange.Redis has nothing to do with this issue and it works pretty stable in our production environment, thanks to Marc Gravell.</p>
  
  <p><strong>FINAL UPDATE:</strong>
  Redis is single-threaded solution - it is ultimately fast but when it comes to the point of releasing the memory (Removing items that are stale or expired) the problems are emerged due to one thread should reclaim the memory (that is not fast operation - whatever algorithm is used) and the same thread should handle GET, SET operations. Of course it happens when we are talking about medium-loaded production environment. Even if you use a cluster with slaves when the memory barrier is reached it will have the same behavior.</p>