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

Configuring Parameters for a Client Connection Pool

Setting Proper parameters for a connection pool can effectively improve the Redis performance of the client. Improper configuration (for example, the number of connections is too small) may cause applications to fail to obtain connections, affecting production services. This document uses the connection pool 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 connection pools. When defining JedisPool, pay attention to the key parameter GenericObjectPoolConfig (Jedis). The following is an example of 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 the 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 JedisPool in the connection pool. JedisPool ensures that resources are under control and thread security is ensured. Proper GenericObjectPoolConfig configuration can improve Redis service performance and reduce resource overhead. The following table shows some important parameters and describes how to set the parameters.

Table 1 Common Jedis parameters

Jedis Parameters

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 there are too many Redis connections, resources are wasted. If there are too few Redis connections, 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 (1s/1 ms) 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. Therefore, 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 the traffic increases.

0

10~20

maxWaitMillis

Maximum waiting 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~1000