2

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

enter image description here

Below is the graph of memory used by redis for above scenarios

enter image description here

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?

Manas Saxena
  • 2,171
  • 6
  • 39
  • 58
  • Are you using persistance in Redis ? If so, Redis could take 2 times the memory required by data stored in it. See more here : https://stackoverflow.com/questions/52993019/redis-out-of-memory-exceptions-but-still-have-plenty-of-memory/52998436#52998436 – rainhacker Jan 17 '19 at 20:17
  • @rainhacker redis persistence is off i cross checked – Manas Saxena Jan 21 '19 at 09:01

1 Answers1

1

I think the answer is that once peak memory usage is reached by Redis , it never frees it until restarted.This is the nature of the memory allocator it uses.

Reference: https://groups.google.com/forum/#!topic/redis-db/ibhYDLT_n68

Manas Saxena
  • 2,171
  • 6
  • 39
  • 58