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.
I am using Spring boot + Redis (Elasticcache).
I am using a wrapper to catch any exception to continue the request process.
My wrapper:
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 <T> T get(Object o, Class<T> type) {
try {
return delegate.get(o, type);
} catch (Exception e) {
return handleException(e);
}
}
@Override
public <T> T get(Object o, Callable<T> 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 <T> T handleException(Exception e) {
log.error("handleException", e);
return null;
}}
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:
Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out
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?
I am think do it:
@Cacheable()
myMethodCached(){
myRealMethod();
}
myRealMethod(){}
Put "myMethodCached" in HystrixCommand, if timeout is throwed, then fallback method is performed without using redis.
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).
Is there a good solution to "disable" redis for a period?