Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

io.lettuce.core.RedisCommandTimeoutException: Command timed out

io.lettuce.core.RedisCommandTimeoutException: Command timed out after 10 second(s)
    at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51) ~[lettuce-core-5.1.6.RELEASE.jar:?]
    at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114) ~[lettuce-core-5.1.6.RELEASE.jar:?]
    at io.lettuce.core.cluster.ClusterFutureSyncInvocationHandler.handleInvocation(ClusterFutureSyncInvocationHandler.java:123) ~[lettuce-core-5.1.6.RELEASE.jar:?]
    at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.1.6.RELEASE.jar:?]
    at com.sun.proxy.$Proxy72.get(Unknown Source) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor138.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
    at io.lettuce.core.support.ConnectionWrapping$DelegateCloseToConnectionInvocationHandler.handleInvocation(ConnectionWrapping.java:191) ~[lettuce-core-5.1.6.RELEASE.jar:?]
    at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.1.6.RELEASE.jar:?]
    at com.sun.proxy.$Proxy72.get(Unknown Source) ~[?:?]

I'm using the following sample code to get the connection through connection pool, Please check and suggest what am I doing wrong

public StatefulRedisClusterConnection<String, String> getRedisClient() {
        log.debug("getRedisClient()");
        if (pool == null) {
            synchronized (this) {
                pool = initializeConnectionPool();
            }
        }
        try {
            return pool.borrowObject();
        } catch (Exception e) {
            log.error("getting pool connection failed",e);
        }
        return null;
    }

    public GenericObjectPool<StatefulRedisClusterConnection<String, String>> initializeConnectionPool() {
        RedisClusterClient clusterClient = RedisClusterClient.create(new RedisURI(clusterUrl, clusterPort,Duration.ofMillis(10000)));
        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
                  .enableAllAdaptiveRefreshTriggers()
                  .refreshTriggersReconnectAttempts(30)
                  .build();
        clusterClient.setOptions(ClusterClientOptions.builder()
                .topologyRefreshOptions(topologyRefreshOptions)
                .build());
        pool = ConnectionPoolSupport.createGenericObjectPool(() -> clusterClient.connect(), getLettucePoolConfig());
        return pool;
    }

private GenericObjectPoolConfig getLettucePoolConfig() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(256);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(10000);
        poolConfig.setMaxIdle(100);
        poolConfig.setMinIdle(50);
        poolConfig.setTestOnBorrow(false);
        poolConfig.setTestWhileIdle(true);
        return poolConfig;
    }
like image 740
Lakshmi Kanth Avatar asked Jul 29 '19 08:07

Lakshmi Kanth


1 Answers

Redis should respond very fast like within milliseconds, you can also try to configure the connection timeout from application.yml.

spring.redis.timeout: 5000

PS: This value is for the test, usually 5sec is a huge number for a Redis connection, Also check if your application can access Redis in the first place.

Note: you really don't need those java connection methods. Spring Boot provide it out of the box with almost no codding, Its just a matter of configuring your application.yml

Look at: Spring Boot Configs for Redis

like image 77
Jay Avatar answered Nov 01 '22 17:11

Jay