title: Disable redis when many timeouts using spring boot
body: <p>I have an application deployed in AWS EC2, some time (rare), I can't connect and execute any command in redis, I am investigating the root cause of this problem.</p>
  
  <p>I am using Spring boot + Redis (Elasticcache).</p>
  
  <p>I am using a wrapper to catch any exception to continue the request process.</p>
  
  <p>My wrapper:</p>
  
  <pre><code>class RedisCacheWrapper implements Cache {
  
  private final Cache delegate;
  
  public RedisCacheWrapper(Cache redisCache) {
      Assert.notNull(redisCache, "delegate cache must not be null");
      this.delegate = redisCache;
  }
  
  @Override
  public String getName() {
      try {
          return delegate.getName();
      } catch (Exception e) {
          return handleException(e);
      }
  }
  
  @Override
  public Object getNativeCache() {
      try {
          return delegate.getNativeCache();
      } catch (Exception e) {
          return handleException(e);
      }
  }
  
  @Override
  public ValueWrapper get(Object key) {
      try {
          return delegate.get(key);
      } catch (Exception e) {
          return handleException(e);
      }
  }
  
  @Override
  public &lt;T&gt; T get(Object o, Class&lt;T&gt; type) {
      try {
          return delegate.get(o, type);
      } catch (Exception e) {
          return handleException(e);
      }
  }
  
  @Override
  public &lt;T&gt; T get(Object o, Callable&lt;T&gt; callable) {
      try {
          return delegate.get(o, callable);
      } catch (Exception e) {
          return handleException(e);
      }
  }
  
  @Override
  public void put(Object key, Object value) {
      try {
          delegate.put(key, value);
      } catch (Exception e) {
          handleException(e);
      }
  }
  
  @Override
  public ValueWrapper putIfAbsent(Object o, Object o1) {
      try {
          return delegate.putIfAbsent(o, o1);
      } catch (Exception e) {
          return handleException(e);
      }
  }
  
  @Override
  public void evict(Object o) {
      try {
          delegate.evict(o);
      } catch (Exception e) {
          handleException(e);
      }
  }
  
  @Override
  public void clear() {
      try {
          delegate.clear();
      } catch (Exception e) {
          handleException(e);
      }
  }
  
  private &lt;T&gt; T handleException(Exception e) {
      log.error("handleException", e);
      return null;
  }}
  </code></pre>
  
  <p>In my redis configuration I set the timeout to 1s. So, when connect/command is not perform after 1s, the redis throws an Exception like this:</p>
  
  <pre><code>Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out
  </code></pre>
  
  <p>My doubt:
  There is a good way to disable cache temporary (without any deploy), while redis is not good? For example: Using a circuit break?</p>
  
  <p>I am think do it:</p>
  
  <pre><code>    @Cacheable()
      myMethodCached(){
         myRealMethod();
      }
  
      myRealMethod(){}
  </code></pre>
  
  <p>Put "myMethodCached" in HystrixCommand, if timeout is throwed, then fallback method is performed without using redis.</p>
  
  <p>The problem with this approach is that I need to create a "fallback" for all methods that use cache, I would like to "disable" globally (all cache will be skipped).</p>
  
  <p>Is there a good solution to "disable" redis for a period?</p>