Redis client Lettuce command timeout versus socket timeout
Asked Answered
D

1

11

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

Density answered 14/8, 2018 at 7:53 Comment(0)
P
19

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.

Peaslee answered 10/4, 2019 at 19:52 Comment(5)
You're welcome. Please mark my answer as correct if it's not too much trouble.Peaslee
I marked it as correct answer on May 8. Thanks for your supportDensity
The description of the various timeout behaviors is correct, please take the suggested timeouts above with a grain of salt, they're almost certainly too high for any real world Redis usage scenario (one would expect a normal Redis command to complete in a few ms, waiting a few minutes rather than retrying would almost certainly lead to a production incident).Brandling
@CRISTIANROMEROMATESANZ you probably upvoted but you did not tick to accept answerBartonbartosch
Sorry marie. I forget to check your response as the accept one. Thank you again for your help resolving my dude.Density

© 2022 - 2024 — McMap. All rights reserved.