Updated on 2024-11-26 GMT+08:00

Configuring Redis Client Retry

Importance of Retry

Both the client and server may encounter temporary faults (such as transient network or disk jitter, service unavailability, or invoking timeout, due to infrastructure or running environment reasons). As a result, Redis operations may fail. You can design automated retry mechanisms to reduce the impact of such faults and ensure successful execution.

Scenarios Where Redis Operations Fail

Scenario

Description

Master/standby switchover triggered by a fault

If the master node is faulty due to Redis underlying hardware or other reasons, a master/standby switchover is triggered to ensure that the instance is still available. A master/standby switchover causes instance disconnection for 15 to 30s:

Read-only during specification modification

During specification modification, the instance may be disconnected for seconds and read-only for minutes.

Request blockage caused by slow queries

Operations whose time complexity is O(N) cause slow queries and request blockage. In this case, other client requests may temporarily fail.

Complex network environment

Due to the complex network environment between the client and the Redis server, network jitter, packet loss, and data retransmission may occur occasionally. In this case, client requests may temporarily fail.

Complex hardware issues

Client requests may temporarily fail due to occasional hardware faults, such as VM HA and disk latency jitter.

Recommended Retry Rules

Retry Rule

Description

Retry only idempotent operations.

Timeout may occur in any of the following phases:

  • A command is successfully sent by the client but has not reached Redis.
  • The command has reached Redis, but the execution times out.
  • Redis has executed the command, but the result returned to the client times out.

A retried operation may be repeatedly executed in Redis. Therefore, not all operations are suitable to be retried. You are advised to retry only idempotent operations, such as running the SET command. For example, if you run the SET a b command multiple times, the value of a can only be b or the execution fails. If you run LPUSH mylist a, which is not idempotent, mylist may contain multiple a elements.

Configure proper retry times and interval.

Configure the retry times and interval based on service requirements in actual scenarios to prevent the following problems:

  • If the number of retries is insufficient or the interval is too long, the application may fail to complete operations.
  • If the number of retries is too large or the interval is too short, the application may occupy too many system resources and the server may be blocked due to too many requests.

Common retry interval policies include immediate retry, fixed-interval retry, exponential backoff retry, and random backoff retry.

Avoid retry nesting.

Retry nesting may cause the retry interval to be exponentially amplified.

Record retry exceptions and print failure reports.

During retry, you can print retry error logs at the WARN level.

Jedis Client Retry Configurations

  • Retries are not supported in native JedisPool mode (for single-node, master/standby, and Proxy Cluster instances). However, you can implement retries by referring to JedisClusterCommand.
  • Retries are supported in JedisCluster mode. You can set the maxAttempts parameter to define the number of retry times when a failure occurs. The default value is 5. By default, all JedisCluster operations invoke the retry method.

    Example code:

    @Bean
    JedisCluster jedisCluster() {
        Set<HostAndPort> hostAndPortsSet = new HashSet<>();
        hostAndPortsSet.add(new HostAndPort("{dcs_instance_address}", 6379));
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(100);
        jedisPoolConfig.setMinIdle(1);
        jedisPoolConfig.setMaxTotal(1000);
        jedisPoolConfig.setMaxWaitMilis(2000);
        jedisPoolConfig.setMaxAttempts(5);
        return new JedisCluster(hostAndPortsSet, jedisPoolConfig);
    }
Table 1 Recommended Jedis connection pool parameter settings

Parameter

Description

Recommended Setting

maxTotal

Maximum number of connections

Set this parameter based on the number of HTTP threads of the web container and reserved connections. Assume that the maxConnections parameter of the Tomcat Connector is set to 150 and each HTTP request may concurrently send two requests to Redis, you are advised to set this parameter to at least 400 (150 x 2 + 100).

Limit: The value of maxTotal multiplied by the number of client nodes (CCE containers or service VMs) must be less than the maximum number of connections allowed for a single DCS Redis instance.

For example, if maxClients of a master/standby DCS Redis instance is 10,000 and maxTotal of a single client is 500, the maximum number of clients is 20.

maxIdle

Maximum number of idle connections

Use the same configuration as maxTotal.

minIdle

Minimum number of idle connections

Generally, you are advised to set this parameter to 1/X of maxTotal. For example, the recommended value is 100.

In performance-sensitive scenarios, you can set this parameter to the value of maxIdle to prevent the impact caused by frequent connection quantity changes. For example, set this parameter to 400.

maxWaitMillis

Maximum waiting time for obtaining a connection, in milliseconds

The recommended maximum waiting time for obtaining a connection from the connection pool is the maximum tolerable timeout of a single service minus the timeout for command execution. For example, if the maximum tolerable HTTP failure is 15s and the timeout of Redis requests is 10s, set this parameter to 5s.

timeout

Command execution timeout, in milliseconds

This parameter indicates the maximum timeout for running a Redis command. Set this parameter based on the service logic. You are advised to set this timeout to least 210 ms to ensure network fault tolerance. For special detection logic or environment exception detection, you can adjust this timeout to seconds.

minEvictableIdleTimeMillis

Idle connection eviction time, in milliseconds. If a connection is not used for a period longer than this, it will be released.

If you do not want the system to frequently re-establish disconnected connections, set this parameter to a large value (xx minutes) or set this parameter to –1 and check idle connections periodically.

timeBetweenEvictionRunsMillis

Interval for detecting idle connections, in milliseconds

The value is estimated based on the number of idle connections in the system. For example, if this interval is set to 30s, the system detects connections every 30s. If an abnormal connection is detected within 30s, it will be removed. Set this parameter based on the number of connections. If the number of connections is too large and this interval is too short, request resources will be wasted. If there are hundreds of connections, you are advised to set this parameter to 30s. The value can be dynamically adjusted based on system requirements.

testOnBorrow

Indicates whether to check the connection validity using the ping command when borrowing connections from the resource pool. Invalid connections will be removed.

If your service is extremely sensitive to connections and the performance is acceptable, you can set this parameter to True. Generally, you are advised to set this parameter to False to enable idle connection detection.

testWhileIdle

Indicates whether to use the ping command to monitor the connection validity during idle resource monitoring. Invalid connections will be destroyed.

True

testOnReturn

Indicates whether to check the connection validity using the ping command when returning connections to the resource pool. Invalid connections will be removed.

False

maxAttempts

Number of connection retries when JedisCluster is used

Recommended value: 3–5. Default value: 5.

Set this parameter based on the maximum timeout intervals of service APIs and a single request. The maximum value is 10. If the value exceeds 10, the processing time of a single request is too long, blocking other requests.