Help Center/ GeminiDB/ GeminiDB Redis API/ Development Reference/ Configuring Parameters for a Client Connection Pool
Updated on 2024-08-06 GMT+08:00

Configuring Parameters for a Client Connection Pool

Setting proper parameters for a connection pool can effectively improve Redis performance of the client. Improper configuration (for example, the number of maximum connections is set to a small value) may cause applications not to be connected, affecting production services. This section uses JedisPool of the Redis client Jedis as an example to describe how to use JedisPool and its parameters, providing optimal configuration reference for service developers.

Usage Instructions

Take Jedis 4.3.1 as an example. The Maven dependency configuration is as follows:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.3.1</version>
    <scope>compile</scope>
</dependency>

Jedis uses Apache Commons-pool2 to manage its connection pool. When defining JedisPool, pay attention to the key parameter GenericObjectPoolConfig (Jedis). The following is an example for using this parameter:

GenericObjectPoolConfig<Jedis> config =  new GenericObjectPoolConfig<>();
config.setMaxTotal(100);
config.setMaxIdle(50);
config.setMinIdle(5);
config.setTestWhileIdle(true);
....

The initialization method of Jedis is as follows:

JedisPool pool = new JedisPool(config, host, port, timeout, password);// Create a connection pool.
try (Jedis jedis = pool.getResource()) {//Obtain a connection and automatically release the connection after the execution.
    //Run the following command on the Jedis client:
} catch (Exception e) {
    e.printStackTrace();
}
pool.close();//Close the connection pool.

Parameter Description

Jedis connections are resources managed by JedisPoo. JedisPool controls resources and protects threads. Proper GenericObjectPoolConfig configuration can improve Redis service performance and reduce resource overhead. The following table lists some important parameters and describes how to set the parameters.

Table 1 Common Jedis parameters

Jedis Parameter

Description

Default Value

Recommended Setting

maxTotal

Maximum number of concurrent connections in the current resource pool.

The number of Redis connections must be set based on the service volume. If the value is set to too large, resources are wasted. If the value is set to too small, connections cannot be obtained, affecting services.

8

The number of client nodes multiplied by the value of maxTotal cannot exceed the maximum number of Redis connections.

Assume that the QPS of a connection is about 1000 per second or millisecond and the expected QPS of a single Redis instance is 50,000. Theoretically, the required resource pool size (MaxTotal) is 50 (50000/1000).

maxIdle

Maximum number of idle connections in a resource pool.

After the number of idle connections reaches the value of maxIdle, the resource pool starts to revoke idle connections until the number of idle connections reaches the value of minIdle. This prevents empty connections from being occupied and resources from being wasted.

8

maxIdle indicates the maximum number of connections required by the service, and maxTotal indicates the margin. You are not advised to set maxIdle to a small value. Otherwise, new Jedis (new connection) overhead occurs.

minIdle

Minimum number of idle connections in a resource pool.

These connections are not revoked to prevent delayed connection creation when traffic increases.

0

10 to 20

maxWaitMillis

Maximum wait time (in milliseconds) of the invoker after the resource pool connections are used up

-1

You are advised to set a proper timeout interval to prevent application blocking after connection pools are used up.

testWhileIdle

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

false

true

testOnBorrow

Indicates whether to check the connection validity (by sending a ping request) each time a connection is obtained from a resource pool. Invalid connections will be released.

false

The preset value is recommended. If this parameter is set to true, a ping command is sent before each command is executed, which affects the performance of applications with a large number of concurrent requests. If high service availability is required, set this parameter to true to ensure that the connection is valid.

testOnReturn

Indicates whether to check the connection validity (by sending a ping request) each time a connection is returned to a connection pool. Invalid connections will be released.

false

The preset value is recommended. If this parameter is set to true, a ping command is sent after each command is executed, which affects the performance of applications with a large number of concurrent requests.

timeout

Socket timeout value of Jedis, in milliseconds

2000

200 to 1000