title: Setting Max Idle connections with Redis in Spring Boot
body: <p>I have a Simple Spring Boot 1.5.x REST API application with Redis Caching. I haven't added any custom configuratators and Spring boot automatically gets configured with Jedis. Redis properties are as follows,</p>
  
  <pre><code>spring.cache.type = redis
  spring.redis.host = localhost
  spring.redis.port = 6379
  spring.redis.pool.max-active = 10000
  spring.redis.pool.max-idle = 9000
  spring.redis.pool.min-idle = 9000
  </code></pre>
  
  <p>Spring Boot Application Class</p>
  
  <pre><code>@SpringBootApplication 
  @EnableCaching 
  public class DemoApplication {
    public static void main(String[] args) {
      pringApplication.run(DemoApplication.class, args);  
    } 
  }
  </code></pre>
  
  <p>Usage</p>
  
  <pre><code>@RestController
  public class UserController {
  
      @Autowired
      private UserService userService;
  
      @RequestMapping(value = "/api/users/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
      @Cacheable(value = "UserDto", key = "#id")
      public UserDto getUser(@PathVariable("id") Integer id){
          User u = userService.getUser(id);
          UserDto dto = new UserDto(u.getId(), u.getFirstName(), u.getLastName());
          return dto;
      }
  }
  </code></pre>
  
  <p>If I load an API Endpoint with JMeter and monitor the number of connections maintained to Redis with a netstat continuously I can see the following output.</p>
  
  <p>Netstat command : </p>
  
  <pre><code>while [ true ] ; do  sudo netstat -anp | grep EST|grep "127.0.0.1:6379          ESTABLISHED"|wc -l; done
  </code></pre>
  
  <p>The Output</p>
  
  <pre><code>9000
  9000
  9006
  9016
  9031
  9019
  9000
  9015
  9005
  9027
  9046
  9011
  9005
  9013
  9011
  9054
  9010
  9094
  9061
  9091
  9036
  9010
  9004
  9003
  8935
  8826
  8387
  7910
  7122
  6631
  6524
  6420
  6300
  6190
  5776
  5482
  5475
  5293
  5187
  5118
  5081
  5020
  4992
  4817
  3996
  3485
  3582
  6504
  8964
  9031
  9136
  9000
  9025
  9035
  9000
  9000
  </code></pre>
  
  <p>As you can clearly see the number of connections is getting decreased at the middle and then again rises to 9000 which does not seem like the desirable result.</p>
  
  <p>Additionally I can see the following DEBUG logs. </p>
  
  <pre><code>2018-06-11 14:20:18.010 DEBUG 4151 --- [io-8080-exec-44] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.012 DEBUG 4151 --- [io-8080-exec-19] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.015 DEBUG 4151 --- [io-8080-exec-19] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.015 DEBUG 4151 --- [io-8080-exec-34] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.015 DEBUG 4151 --- [io-8080-exec-34] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.015 DEBUG 4151 --- [io-8080-exec-41] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.015 DEBUG 4151 --- [io-8080-exec-41] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.015 DEBUG 4151 --- [io-8080-exec-49] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [io-8080-exec-49] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [io-8080-exec-29] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [io-8080-exec-29] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [nio-8080-exec-3] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [nio-8080-exec-3] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [io-8080-exec-51] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.016 DEBUG 4151 --- [io-8080-exec-51] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  2018-06-11 14:20:18.017 DEBUG 4151 --- [io-8080-exec-45] o.s.d.redis.core.RedisConnectionUtils    : Closing Redis Connection
  2018-06-11 14:20:18.017 DEBUG 4151 --- [io-8080-exec-45] o.s.d.redis.core.RedisConnectionUtils    : Opening RedisConnection
  </code></pre>
  
  <p>Based on the <a href="https://github.com/spring-projects/spring-data-redis/blob/master/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java" rel="nofollow noreferrer">code in GitHub</a> this happens due to continuous calling of  doGetConnection and releaseConnection methods.  </p>
  
  <ol>
  <li><p>Is this desirable? If so what is the explanation for seeing a connection count less than 9000?</p></li>
  <li><p>If this is not accurate what are the things I can try? </p></li>
  </ol>