title: Consistent Timeouts in Redis Cache in Azure
body: <p>I'm using Redis Cache in an azure website. The Cache is hosted in Azure. I was noticing some timeouts when setting values to the cache coming through our monitoring. So I ran some load tests that I'd run before I'd moved from the local server cache to using redis and the results were pretty bad compared to the previous test runs mostly caused by timeouts to redis cache. </p>
  
  <p>I'm using the StackExchange.Redis library version 1.0.333 strong name version.</p>
  
  <p>I was careful not to create a new connection each time I access the cache.</p>
  
  <p>The load test is not actually loading the server up that much and results were 100% successful previously and now get about 50% error rate caused by timeouts.</p>
  
  <p>Code being used to access the cache.</p>
  
  <pre><code> public static class RedisCacheProvider
  {
      private static ConnectionMultiplexer connection;
      private static ConnectionMultiplexer Connection
      {
          get
          {
              if (connection == null || !connection.IsConnected)
              {
                  connection = ConnectionMultiplexer.Connect(ConfigurationManager.ConnectionStrings["RedisCache"].ToString());
              }
              return connection;
          }
      }
  
      private static IDatabase Cache
      {
          get
          {
              return Connection.GetDatabase();
          }
      }
  
  
      public static T Get&lt;T&gt;(string key)
      {
          return Deserialize&lt;T&gt;(Cache.StringGet(key));
      }
  
      public static object Get(string key)
      {
          return Deserialize&lt;object&gt;(Cache.StringGet(key));
      }
  
      public static void Set(string key, object value)
      {
          Cache.StringSet(key, Serialize(value));
      }
  
      public static void Remove(string key)
      {
          Cache.KeyDelete(key);
      }
  
      public static void RemoveContains(string contains)
      {
          var endpoints = Connection.GetEndPoints();
          var server = Connection.GetServer(endpoints.First());
          var keys = server.Keys();
          foreach (var key in keys)
          {
              if (key.ToString().Contains(contains))
                  Cache.KeyDelete(key);
          }
      }
  
      public static void RemoveAll()
      {
          var endpoints = Connection.GetEndPoints();
          var server = Connection.GetServer(endpoints.First());
          server.FlushAllDatabases();
      }
  
      static byte[] Serialize(object o)
      {
          if (o == null)
          {
              return null;
          }
  
          BinaryFormatter binaryFormatter = new BinaryFormatter();
          using (MemoryStream memoryStream = new MemoryStream())
          {
              binaryFormatter.Serialize(memoryStream, o);
              byte[] objectDataAsStream = memoryStream.ToArray();
              return objectDataAsStream;
          }
      }
  
      static T Deserialize&lt;T&gt;(byte[] stream)
      {
          if (stream == null)
          {
              return default(T);
          }
  
          BinaryFormatter binaryFormatter = new BinaryFormatter();
          using (MemoryStream memoryStream = new MemoryStream(stream))
          {
              T result = (T)binaryFormatter.Deserialize(memoryStream);
              return result;
          }
      }
  
  }
  </code></pre>