Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 681
CRISTIAN ROMERO MATESANZ Avatar asked Aug 14 '18 07:08

CRISTIAN ROMERO MATESANZ


People also ask

What is socket timeout Redis?

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).

What is default Redis connection timeout?

The default is 1000 milliseconds. You can modify this by adding a "syncTimeout=2000" (which would give a timeout of 2 seconds) to your configuration string or by using the ConfigurationOptions when you connect to the cache.

How do I set timeout in Redis?

To create a Redis with an expiration time, use the SET command and the EX option to set the expiration time. The EX option takes a number in seconds and sets the number of seconds the key is valid until expiration. You can also use PX to specify the expiration time in Milliseconds.

What is LettuceConnectionFactory?

public class LettuceConnectionFactory extends Object implements InitializingBean, DisposableBean, RedisConnectionFactory, ReactiveRedisConnectionFactory. Connection factory creating Lettuce-based connections. This factory creates a new LettuceConnection on each call to getConnection() .


1 Answers

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.

like image 169
mvmn Avatar answered Sep 21 '22 11:09

mvmn