Background:
My service caches data in redis standalone setup in prod environment, using spring-data-redis RedisTemplate
using @Cacheable
annotation. I cache data for 3 mins , however I saw that my redis memory was gradually increasing (this observation was done for 1-2 weeks). I suspected that my redis keys were not getting evicted, as number of keys constantly increased (this could be due to constant load too). So I disconnected my service from redis for 3 min and observed redis memory . All the keys got expired and memory usage dropped.
However, when I restarted my service to cache data in redis , within 1-2 mins of doing the same I got the same number of keys as before( this was expected as load was high on my service) but the memory usage of redis was significantly less.
Below is a graph of number of keys in redis before, during redis not being used and after reconnecting my service to cache
Below is the graph of memory used by redis for above scenarios
As you can see , for the same number of keys , redis is consuming very high memory when it was run for a long duration (1-2 weeks). When I disconnected my service from redis to empty all keys and then again restarted to use redis cache , my memory usage was very low, for same number of keys
What could explain this behaviour? Could it be memory leak , for connection I have a class which extends CachingConfigurerSupport
. The connection bean and redis template bean are as follows:
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
jedisConnFactory.setUsePool(true);
jedisConnFactory.setHostName(redisMasterUrl);
return jedisConnFactory;
}
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
Anything I am missing out pertaining to connections , do I need to close connections anywhere when using RedisTemplate
?