tencent cloud

Connecting to Redis via Jedis
Last updated: 2025-10-11 14:26:39
Connecting to Redis via Jedis
Last updated: 2025-10-11 14:26:39
Jedis is a simple and easy-to-use Redis Java client. It provides synchronous blocking APIs to operate Redis and applies to single-thread or low-concurrency scenarios.

Recommended Version

Jedis 5.1.2 is recommended, and the connection mode should be set to standalone.
Note:
Tencent Cloud uses a VPC IP proxy layer to uniformly shield the complexity (sharding/high availability) of the underlying Redis architecture. When accessing a Tencent Cloud Redis instance, clients can directly connect to the unified entry (VPC IP) provided by Tencent Cloud, without needing to configure a cluster or sentinel mode, just like connecting to a single-node Redis.

Parameter Configuration

Parameter Name
Type
Default Value
Description
Recommended Value
connectTimeout
int
2000ms
Sets connection timeout.
3000ms
readTimeout
int
2000ms
Sets command timeout.
2000ms
poolMinIdle
int
None
Minimum number of idle connections in the connection pool.
50
poolMaxIdle
int
None
Maximum number of idle connections in the connection pool.
200
poolMaxTotal
int
None
Maximum number of connections in the connection pool.
200

application.properties Format

tencent.redis.host=XX.XX.XX.XX
tencent.redis.port=6379
tencent.redis.database=0
tencent.redis.password=XXXX
tencent.redis.connect.timeout=3000
tencent.redis.read.timeout=2000
tencent.redis.pool.minSize=50
tencent.redis.pool.maxSize=200

application.yml Format

tencent:
redis:
host: XX.XX.XX.XX
port: 6379
database: 0
password: XXXX
connect:
timeout: 3000
read:
timeout: 2000
pool:
minSize: 50
maxSize: 200

Sample Code

Pom Configuration

<!-- Introduce the spring-data-redis component. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!--After Spring Boot 2.0, the default client is Lettuce. When using Jedis, it is necessary to exclude the package.-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Introduce the Jedis dependency package. -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.2</version>
</dependency>

Reference Code

The Jedis connection pool defaults to Last-In-First-Out (LIFO) configuration. However, in actual operation, different connection response times may lead to an imbalance in connection establishment. To resolve this issue, it is recommended to change the LIFO configuration to First-In-First-Out (FIFO) to ensure a fair connection establishment sequence and enhance system stability and performance.
import java.time.Duration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisConfig {

@Value("${tencent.redis.host}")
private String redisHost;

@Value("${tencent.redis.port:6379}")
private Integer redisPort = 6379;

@Value("${tencent.redis.database:0}")
private Integer redisDatabase = 0;

@Value("${tencent.redis.password:}")
private String redisPassword;

@Value("${tencent.redis.connect.timeout:3000}")
private Integer redisConnectTimeout = 3000;

@Value("${tencent.redis.read.timeout:2000}")
private Integer redisReadTimeout = 2000;

@Value("${tencent.redis.pool.minSize:50}")
private Integer redisPoolMinSize = 50;

@Value("${tencent.redis.pool.maxSize:200}")
private Integer redisPoolMaxSize = 200;

@Bean
public RedisConnectionFactory redisConnectionFactory(JedisClientConfiguration clientConfiguration) {

RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(redisHost);
standaloneConfiguration.setPort(redisPort);
standaloneConfiguration.setDatabase(redisDatabase);
standaloneConfiguration.setPassword(redisPassword);

return new JedisConnectionFactory(standaloneConfiguration, clientConfiguration);
}

@Bean
public JedisClientConfiguration clientConfiguration() {

JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder()
.connectTimeout(Duration.ofMillis(redisConnectTimeout))
.readTimeout(Duration.ofMillis(redisReadTimeout))
.usePooling().poolConfig(redisPoolConfig())
.build();

return clientConfiguration;
}

private JedisPoolConfig redisPoolConfig() {

JedisPoolConfig poolConfig = new JedisPoolConfig();
//The minimum number of connections in the connection pool.
poolConfig.setMinIdle(redisPoolMinSize);
//The maximum number of idle connections in the connection pool.
poolConfig.setMaxIdle(redisPoolMaxSize);
//The maximum number of connections in the connection pool.
poolConfig.setMaxTotal(redisPoolMaxSize);
//Validate effectiveness (ping) when creating a connection. The default value is false.
poolConfig.setTestOnCreate(false);
//Validate effectiveness (ping) when obtaining a connection. The default value is false. It is recommended to set it to false during high business volume to reduce overhead.
poolConfig.setTestOnBorrow(true);
//Validate effectiveness (ping) when returning a connection. The default value is false. It is recommended to set it to false during high business volume to reduce overhead.
poolConfig.setTestOnReturn(false);
//Disable LIFO, and change the LIFO configuration to FIFO to ensure a fair connection establishment order.
poolConfig.setLifo(false);
return poolConfig;
}
}
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback