Help Center/ GeminiDB/ GeminiDB Redis API/ Best Practices/ GeminiDB Redis API Connection Test and Client Reconnection Retry Suggestions
Updated on 2025-09-04 GMT+08:00

GeminiDB Redis API Connection Test and Client Reconnection Retry Suggestions

Connection Test

GeminiDB Redis API does not disconnect a client if the network remains stable and the client stays connected. Similar to the connection mechanism of open-source Redis, GeminiDB Redis API has TCP Keepalive enabled to identify and clear abnormal connections caused by network interruptions, client breakdown, or NAT timeout. This prevents exhausting server connection resources and ensures service stability and availability.

Parameter Description

Table 1 Parameter description

Parameter

Description

ClientTimeout

  • Timeout interval at which a server terminates idle connections
  • The default value is 0, indicating that GeminiDB does not proactively terminate idle connections.
  • This parameter is only available to normal client connections. It is unavailable to Pub/Sub connections or blocking commands (such as BRPOP).

tcp-keepalive

Interval at which GeminiDB sends a TCP keepalive packet to idle connections. The default value is 120s.

TCP_KEEPINTVL

Interval (in seconds) at which test packets are sent. The default value is 40s.

TCP_KEEPCNT

Number of times that a keepalive packet is sent. The default value is 3.

Test Mechanism

1. When a connection is idle for a period longer than the value of tcp-keepalive, GeminiDB sends a keepalive packet to check whether the connection is still valid.

2. If no response is received, GeminiDB retries the test at the interval set for TCP_KEEPINTVL.

3. If no response is received after the keepalive packet has been sent for consecutive times set for TCP_KEEPCNT, the connection is considered invalid. GeminiDB terminates the connection to free up server connection resources, ensuring service stability and availability.

If the minor kernel version of a GeminiDB Redis instance is earlier than 5.0.6.4, take care when configuring tcp-keepalive. It cannot be dynamically adjusted. You are advised to upgrade the instance to the latest version which features an improved keepalive mechanism and service reliability. This update minimizes connection disruptions due to network fluctuations while offering greater flexibility and stability in managing connections.

Client Reconnection and Command Retry

The connection between the client and server can be impacted by several factors, including packet loss, jitter, or temporary faults. To improve system robustness and user experience, you are advised to introduce the connection reconnection and command retry mechanisms to reduce the impact of network jitter.

The client and GeminiDB instance may be disconnected if packets are lost, jitter occurs, node specifications are changed, or a version is upgraded. To ensure successful operations, the client must have an automated reconnection mechanism. You are advised to use mainstream Redis clients, such as Jedis, to test connections before obtaining them or periodically check the status of idle connections to prevent operation timeout or failures caused by connection issues. In this way, system connectivity and reliability can be effectively improved.

  • Set testonBorrow to check validity when obtaining connections.
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    //Sets the validity check item when obtaining connections from the connection pool.
    poolConfig.setTestOnBorrow(true);
    //Other configurations
     
    //Uses the configurations when creating a connection pool.
    JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379);

When testOnBorrow is set to true, the system checks connection validity by executing a ping command each time a connection is obtained from the connection pool. The system will discard an invalid connection and attempt to obtain another available connection. This configuration ensures that each obtained connection is valid but consumes more resources each time ping is executed. During traffic spikes, you can enable idle connection test (for example, setting testWhileIdle to true) to check connection validity before connections are used. This prevents extra resource consumption each time a connection is obtained, improving the overall performance.

  • An idle connection test is performed by an asynchronous thread to check connection validity. Compared with testonBorrow, this option consumes fewer resources.
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setTestWhileIdle(true);
//Interval for testing idle connections. The value ranges from 30s to 60s.
poolConfig.setTimeBetweenEvictionRunsMillis(30000);
//Number of connections checked each time. A negative value is recommended, indicating that all idle connections are checked.
poolConfig.setNumTestsPerEvictionRun(-1);
//Minimum idle period of a connection. The test starts when the idle period exceeds this value.
poolConfig.setMinEvictableIdleTimeMillis(60000); //60s
JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379);
Table 2 Parameter description

Parameter

Description

Default Value

Suggestion

testWhileIdle

Whether validity of idle connections is tested using ping. Invalid connections will be destroyed.

false

true

timeBetweenEvictionRunsMillis

Interval for testing idle resources, in milliseconds

-1 (no test)

You can specify a value, for example, 30000.

minEvictableIdleTimeMillis

Minimum idle duration in a resource pool, in milliseconds. Resources that remain idle for longer than this period will be removed.

1,800,000 (30 minutes)

The default value is recommended.

numTestsPerEvictionRun

Number of idle resources to be tested each time

3

You can adjust the value based on the number of application connections. The recommended value is –1, which means all idle connections will be tested.

If a Redis command fails to be executed, for example, due to timeout caused by network jitter, you can retry the command, such as SET key value, which is idempotent, to avoid the impact of temporary network problems. Retrying non-idempotent operations, such as LPUSH and LPOP may cause data to be inserted or deleted multiple times, leading to service logic errors. To ensure successful operations, you need to analyze whether a retry policy is suitable in specific scenarios and properly set the retry times and interval.

public <T> T executeWithRetry(RedisCommand<T> command, int maxAttempts) {
    JedisException lastException = null;
    
    for (int attempt = 1; attempt <= maxAttempts; attempt++) {
        try (Jedis jedis = jedisPool.getResource()) {
            return command.execute(jedis);
        } catch (JedisConnectionException e) {
            lastException = e;
            log.warn("Redis connection failed (attempt {}/{}): {}", 
                attempt, maxAttempts, e.getMessage());
           
                //Uses the sleep method or exponential backoff to retry a command after a period.
               Thread.sleep(waitTime);
        }
    }
    throw new RedisOperationException("Max retry attempts reached", lastException);
}