Search code examples
redistimeoutspring-data-redislettuce

Redis client Lettuce command timeout versus socket timeout


We have defined Lettuce client connection factory to be able to connect to Redis defining custom socket and command timeout:

@Bean
LettuceConnectionFactory lettuceConnectionFactory() {

   final SocketOptions socketOptions = SocketOptions.builder().connectTimeout(socketTimeout).build();
   final ClientOptions clientOptions =
           ClientOptions.builder().socketOptions(socketOptions).build();

   LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
           .commandTimeout(redisCommandTimeout)
           .clientOptions(clientOptions).build();
   RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(redisHost,
           redisPort);

   final LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(serverConfig,
           clientConfig);
   lettuceConnectionFactory.setValidateConnection(true);
   return new LettuceConnectionFactory(serverConfig, clientConfig);
}

enter image description here

Lettuce documentation define default values:

  • Default socket timeout is 10 seconds
  • Default command timeout is 60 seconds

If Redis service is down application must receive timeout in 300ms. Which value must be defined as the greatest value?

Github example project: https://github.com/cristianprofile/spring-data-redis-lettuce


Solution

  • In socket options you specify connect timeout. This is a maximum time allowed for Redis client (Lettuce) to try to establish a TCP/IP connection to a Redis Server. This value should be relatively small (e.g. up to 1 minute).

    If client could not establish connection to a server within 1 minute I guess it's safe to say server is not available (server is down, address/port is wrong, network security like firewalls prohibit connection etc).

    The command timeout is completely different. Once connection is established, client can send commands to the server. It expects server to respond to those command. The timeout configures for how long client will be waiting for a response to a command from the server.

    I think this timeout can be set to a bigger value (e.g a few minutes) in case client command sends a lot of data to the server and it takes time to transfer and store so much data.