Redis/Jedis 服务不正常

Redis/Jedis not serving properly

我们在我们的 Spring Java 应用程序中使用 Redis 缓存和 Jedis,在高负载期间缓存卡住并且没有及时响应。

我们已经配置了Jedis连接如下:

val clientConfiguration = JedisClientConfiguration
                .builder()
                .readTimeout(Duration.ofSeconds(2L))
                .connectTimeout(Duration.ofSeconds(10L))
                .usePooling()
                .build()

        val configuration = RedisStandaloneConfiguration(redisHost, Integer.parseInt(redisPort))
        configuration.password = RedisPassword.of(redisPassword)

val jedisConnectionFactory = JedisConnectionFactory(configuration, clientConfiguration)
        jedisConnectionFactory.poolConfig.apply {
            maxTotal = 512
            maxIdle = 256
            minIdle = 16
            maxWaitMillis = 2000L
            blockWhenExhausted = true
        }

Redis 服务器似乎没有负载,也没有显示任何错误。我们唯一能看到的问题是在使用 jstack 时有数百个线程卡在 RedisCache.get:

 java.lang.Thread.State: BLOCKED (on object monitor)
    at org.springframework.data.redis.cache.RedisCache.get(RedisCache.java:117)
    - waiting to lock <0x00000000c00bd5f8> (a org.springframework.data.redis.cache.RedisCache)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:381)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)

如果服务器没有正确响应,我希望看到一些异常,但绝对 none。我们唯一能看到的是超级慢的响应。

您遇到的错误不是因为 redis-server 负载过大,而是 spring-server 耗尽。在配置中:

jedisConnectionFactory.poolConfig.apply {
            maxTotal = 512
            ...
            ...
            blockWhenExhausted = true
        }

似乎 jedis-pool 已耗尽并阻止新请求。为了尽量减少这种情况,您可以增加池大小并检查是否减少 maxWaitMillis = 2000LreadTimeout(Duration.ofSeconds(2L))connectTimeout(Duration.ofSeconds(10L)) 有任何改进。

我们发现这实际上是由 Jedis 实现的一个已知问题引起的:

https://jira.spring.io/projects/DATAREDIS/issues/DATAREDIS-678?filter=allopenissues