title: Redis health check for spring boot 2 hangs
body: <p>I have implemented some redis stuff in my spring boot 2.1.5 application. It works fine.
  I also want the health check for redis. If I switch off the redis server the health check (actuator/health) hangs forever.
  <strong>How can I configure a sensible timeout?</strong></p>
  
  <p>I have created a little demo of this problem here:
  <a href="https://github.com/markuskruse/demo-redis-health-bug" rel="nofollow noreferrer">https://github.com/markuskruse/demo-redis-health-bug</a></p>
  
  <p>Clone, run, stop redis, check health (wait forever), start redis (health returns).</p>
  
  <p>This is my gradle for redis:</p>
  
  <pre><code>implementation 'org.springframework.boot:spring-boot-starter-data-redis'
  </code></pre>
  
  <p>This is my application.yaml:</p>
  
  <pre><code>spring:
     redis:
        timeout: 5000
        host: localhost
  </code></pre>
  
  <p>This is my RedisConfig.java</p>
  
  <pre><code>@Configuration
  @EnableConfigurationProperties(RedisProperties.class)
  public class RedisConfig {
  
    @Bean
    public LettuceConnectionFactory redisConnectionFactory(
        @Value("${spring.redis.host:localhost}") String redisHost) {
      RedisStandaloneConfiguration redisStandaloneConfiguration =
          new RedisStandaloneConfiguration(redisHost);
      return new LettuceConnectionFactory(redisStandaloneConfiguration);
    }
  
    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory jedisConnectionFactory) {
      final StringRedisTemplate template = new StringRedisTemplate();
      template.setConnectionFactory(jedisConnectionFactory);
      template.afterPropertiesSet();
      return template;
    }
  }
  </code></pre>
  
  <p>According to this issue on github, it is a mere configuration issue:
  <a href="https://github.com/spring-projects/spring-boot/issues/15542" rel="nofollow noreferrer">https://github.com/spring-projects/spring-boot/issues/15542</a></p>
  
  <p>According to this jira ticket, it should be fixed in spring boot 2.1.4 (I'm on 2.1.5).
  <a href="https://jira.spring.io/browse/DATAREDIS-918" rel="nofollow noreferrer">https://jira.spring.io/browse/DATAREDIS-918</a></p>
  
  <p>They mention a workaround that I have tried:</p>
  
  <pre><code>  @Bean
    public ClientOptions clientOptions() {
      return ClientOptions.builder()
          .timeoutOptions(TimeoutOptions.enabled())
          .build();
    }
  </code></pre>
  
  <p>By itself, it had no effect. I have to inject it somewhere. Googling gave this:</p>
  
  <pre><code>  @Bean
    LettucePoolingClientConfiguration lettucePoolConfig(ClientOptions options, ClientResources dcr){
      return LettucePoolingClientConfiguration.builder()
          .clientOptions(options)
          .clientResources(dcr)
          .build();
    }
  </code></pre>
  
  <p>Then I get this:</p>
  
  <pre><code>Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration]: Factory method 'lettucePoolConfig' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
      ... 50 more
  Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
      at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration$LettucePoolingClientConfigurationBuilder.&lt;init&gt;(LettucePoolingClientConfiguration.java:91)
      at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.builder(LettucePoolingClientConfiguration.java:50)
      at com.ikea.cps.mhs.config.RedisConfig.lettucePoolConfig(RedisConfig.java:50)
      at com.ikea.cps.mhs.config.RedisConfig$$EnhancerBySpringCGLIB$$3804d114.CGLIB$lettucePoolConfig$3(&lt;generated&gt;)
      at com.ikea.cps.mhs.config.RedisConfig$$EnhancerBySpringCGLIB$$3804d114$$FastClassBySpringCGLIB$$ccabed80.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
      at com.ikea.cps.mhs.config.RedisConfig$$EnhancerBySpringCGLIB$$3804d114.lettucePoolConfig(&lt;generated&gt;)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
      ... 51 more
  Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
      at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
      at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
      ... 64 more
  </code></pre>
  
  <p>I can maybe work around this. But I am thinking that I am doing something (fundamentally) wrong. It should already be fixed.</p>
  
  <p>Edit: I added the commons pool and the error goes away, but health check still hangs forever.</p>
  
  <p>I also tried this below, to no effect.</p>
  
  <pre><code>@Component
  public class RedisConfigurer implements LettuceClientConfigurationBuilderCustomizer {
  
    @Override
    public void customize(LettuceClientConfigurationBuilder builder) {
      builder.clientOptions(ClientOptions.builder()
          .timeoutOptions(TimeoutOptions.enabled(Duration.of(5, SECONDS))).build());
    }
  
  }
  </code></pre>