11

I am using Jedis in a tomcat web app to connect to an Elascticache Redis node. The app is used by hundreds of users in day time. I am not sure of this is normal or not, but whenever I check the current connections count with cloudwatch metrics, I see the current connections increasing without falling down.

This is my Jedis pool configuration:

public static JedisPool getPool(){
        if(pool == null){
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMinIdle(5);
            config.setMaxIdle(35);
            config.setMaxTotal(1500);
            config.setMaxWaitMillis(3000);
            config.setTestOnBorrow(true);
            config.setTestWhileIdle(true);
            pool = new JedisPool(config, PropertiesManager.getInstance().getRedisServer());
        }
        return pool;    
    }

and this is how I always use the pool connections to execute redis commands:

        Jedis jedis = JedisUtil.getPool().getResource();
        try{
            //Redis commands
            }
        catch(JedisException e){
            e.printStackTrace();
            throw e;
        }finally{
            if (jedis != null) JedisUtil.getPool().returnResource(jedis);
        }

With this configuration, the count is currently over 200. Am I missing something that is supposed to discard or kill unsused connections ? I set maxIdle to 35 and I expected that the count falls down to 35 when the traffic is very low but this never happened.

3 Answers 3

18

we had the same problem. After investigating a little bit more further we came across with this (from redis official documentation - http://redis.io/topics/clients) :

By default recent versions of Redis don't close the connection with the client if the client is idle for many seconds: the connection will remain open forever.

By default, aws provides a timeout value of 0. Therefore, any connection that has been initialised with your redis instance will be kept by redis even if the connection initialised by your client is down.

Create a new cache parameter policy in aws with a timeout different of 0 and then you should be good

1
  • 2
    Wow, thanks so much. I would've spent hours looking into this. I just checked my first ElastiCache instance's monitoring and was shocked to see a near linear increase in connection over the past week. I'll be updating the settings for sure.
    – Sam
    Dec 17, 2015 at 2:10
6

In the cache parameter group you can edit timeout. It defaults to 0 which leaves idle connection in redis. If you set it to 100 it will remove connections idle for 100 seconds.

1

You can check the pool size using JMX. Activating the idle evictor thread is a good idea. You can do so by setting the timeBetweenEvictionRunsMillis parameter on the JedisPoolConfig.

If you don't use transactions (EXEC) or blocking operations (BLPOP, BRPOP), you could stick to one connection if connection count is a concern for you. The lettuce client is thread-safe with one connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.