javaspringspring-bootspring-data-redis

How connection pooling works with RedisTemplate using spring boot application


I have the following code snippet which for getting the RedisTemplate.

@Bean
public JedisConnectionFactory getJedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(host);
    if (!StringUtils.isEmpty(password)) {
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
    }
    redisStandaloneConfiguration.setPort(port);
    return new JedisConnectionFactory(redisStandaloneConfiguration, getJedisClientConfiguration());
}

@Bean
public RedisTemplate redisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(getJedisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    return redisTemplate;
}

My question how sprint-boot will understand the Connection pooling because I have not provided any information in my factory about the connection pool. My application properties file has the following properties.

redis.host=<redis-host>
redis.port=<port>
redis.password=<password>
redi.jedis.pool.max.total=16
redi.jedis.pool.max.idle=8
redi.jedis.pool.min.idle=4

Solution

  • When you create JedisClientConfiguration using the builder

    JedisClientConfigurationBuilder builder = JedisClientConfiguration .builder()
    

    this will internally call default constructor on JedisClientConfiguration which looks like something like this.

    private DefaultJedisClientConfigurationBuilder() {
      this.poolConfig = new JedisPoolConfig();
      // other configs
    }
    

    JedisPoolConfig further extends GenericObjectPoolConfig which has default values as below. (which would be default values if not overridden manually)

    maxTotal = 8;
    maxIdle = 8;
    minIdle = 0;
    

    In your case, as you have overridden config with GenericObjectPoolConfig, it will pick values from there.

    GenericObjectPoolConfig.setMaxTotal(maxConnection); 
    GenericObjectPoolConfig.setMaxIdle(maxConnectionIdle); 
    GenericObjectPoolConfig.setMinIdle(minConnectionIdle);
    

    As you are specifying usePooling() and poolConfig(genericObjectPoolConfig), your app will use these config for connection pooling.

    I hope this helps.