Help Center/ Distributed Cache Service/ FAQs/ Client and Network Connection/ Connection Pool Selection and Recommended Jedis Parameter Settings
Updated on 2022-09-15 GMT+08:00

Connection Pool Selection and Recommended Jedis Parameter Settings

Advantages of the Jedis Connection Pool

The comparison between Lettuce and Jedis is as follows:

  • Lettuce
    • Lettuce does not perform connection keepalive detection. If an abnormal connection exists in the connection pool, an error is reported when requests time out.
    • Lettuce does not implement connection pool validation such as testOnBorrow. As a result, connections cannot be validated before being used.
  • Jedis
    • Jedis implements connection pool validation using testOnBorrow, testWhileIdle, and testOnReturn.

      If testOnBorrow is enabled, connection validation is performed when connections are being borrowed, which has the highest reliability but affects the performance (detection is performed before each Redis request).

    • testWhileIdle can be used to detect idle connections. If the threshold is set properly, abnormal connections in the connection pool can be removed in time to prevent service errors caused by abnormal connections.
    • If a connection becomes abnormal before the idle connection check, the service that uses the connection may report an error. You can specify the timeBetweenEvictionRunsMillis parameter to control the check interval.

Therefore, Jedis has better exception handling and detection capabilities and is more reliable than Lettuce in scenarios where there are connection exceptions and network jitters.

Recommended Jedis Connection Pool Parameter Settings

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

Set this parameter to the value of 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 timeout 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. Generally, you are advised to set this timeout to longer than 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.