Distributed Cache Service (DCS) 2.3.0.1 Usage Guide (for Huawei Cloud Stack 8.5.1) 01
Java
Connecting to Redis on Jedis (Java)
This section describes how to access a Redis instance on Jedis. For more information about how to use other Redis clients, visit the Redis official website.
Spring Data Redis is already integrated with Jedis and Lettuce for Spring Boot projects. Spring Boot 1.x is integrated with Jedis, and Spring Boot 2.x is integrated with Lettuce. To use Jedis in Spring Boot 2.x and later, you need to solve Lettuce dependency conflicts.
Notes and Constraints
Springboot 2.3.12.RELEASE or later is required. Jedis 3.10.0 or later is required.
Prerequisites
- A Redis instance is created, and is in the Running state.
- View the IP address/domain name and port of the DCS Redis instance to be accessed. For details, see "Getting Started" > "Viewing Details of a DCS Instance" in Distributed Cache Service (DCS) 2.3.0.1 User Guide (for Huawei Cloud Stack 8.5.1).
Pom Configuration
<!-- import spring-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!--In Spring Boot 2.0, Lettuce is used by default. To use Jedis, solve dependency conflicts.--> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!--Jedis dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}<version> </dependency>
application.properties Configuration
- Single-node, master/standby, read/write splitting, and Proxy Cluster
#Redis host spring.redis.host=<host> #Redis port spring.redis.port=<port> #Redis database number spring.redis.database=0 #Redis password spring.redis.password=<password> #Redis read/write timeout spring.redis.timeout=2000 #Whether to enable connection pooling spring.redis.jedis.pool.enabled=true #Minimum connections in the pool spring.redis.jedis.pool.min-idle=50 #Maximum idle connections in the pool spring.redis.jedis.pool.max-idle=200 #Maximum connections in the pool spring.redis.jedis.pool.max-active=200 #Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. The default value -1 indicates to wait indefinitely. spring.redis.jedis.pool.max-wait=3000 #Interval for checking and evicting idle connection. Default: 60s. spring.redis.jedis.pool.time-between-eviction-runs=60S
- Redis Cluster
#Redis Cluster node connection information spring.redis.cluster.nodes=<ip:port>,<ip:port>,<ip:port> #Redis Cluster password spring.redis.password=<password> #Redis Cluster max. redirecting times spring.redis.cluster.max-redirects=3 #Redis read/write timeout spring.redis.timeout=2000 #Whether to enable connection pooling spring.redis.jedis.pool.enabled=true #Minimum connections in the pool spring.redis.jedis.pool.min-idle=50 #Maximum idle connections in the pool spring.redis.jedis.pool.max-idle=200 #Maximum connections in the pool spring.redis.jedis.pool.max-active=200 #Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. The default value -1 indicates to wait indefinitely. spring.redis.jedis.pool.max-wait=3000 #Interval for checking and evicting idle connections. Default: 60s. spring.redis.jedis.pool.time-between-eviction-runs=60S
Bean Configuration
- Single-node, master/standby, read/write splitting, and Proxy Cluster
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 RedisConfiguration { @Value("${redis.host}") private String redisHost; @Value("${redis.port:6379}") private Integer redisPort = 6379; @Value("${redis.database:0}") private Integer redisDatabase = 0; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:3000}") private Integer redisConnectTimeout = 3000; @Value("${redis.read.timeout:2000}") private Integer redisReadTimeout = 2000; @Value("${redis.pool.minSize:50}") private Integer redisPoolMinSize = 50; @Value("${redis.pool.maxSize:200}") private Integer redisPoolMaxSize = 200; @Value("${redis.pool.maxWaitMillis:3000}") private Integer redisPoolMaxWaitMillis = 3000; @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}") private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 * 1000; @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}") private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000; @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(); //Minimum connections in the pool poolConfig.setMinIdle(redisPoolMinSize); //Maximum idle connections in the pool poolConfig.setMaxIdle(redisPoolMaxSize); //Maximum total connections in the pool poolConfig.setMaxTotal(redisPoolMaxSize); //Wait when pool is exhausted? Set to true to wait. To validate setMaxWait, it has to be true. poolConfig.setBlockWhenExhausted(true); //Longest time to wait for connection after pool is exhausted. The default value -1 indicates to wait indefinitely. poolConfig.setMaxWaitMillis(redisPoolMaxWaitMillis); //Set to true to enable connectivity test on creating connections. Default: false. poolConfig.setTestOnCreate(false); //Set to true to enable connectivity test on borrowing connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnBorrow(true); //Set to true to enable connectivity test on returning connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnReturn(false); //Indicates whether to check for idle connections. If this is set to false, idle connections are not evicted. poolConfig.setTestWhileIdle(true); //Duration after which idle connections are evicted. If the idle duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted. poolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolSoftMinEvictableIdleTimeMillis); //Disable MinEvictableIdleTimeMillis(). poolConfig.setMinEvictableIdleTimeMillis(-1); //Interval for checking and evicting idle connections. Default: 60s. poolConfig.setTimeBetweenEvictionRunsMillis(redisPoolBetweenEvictionRunsMillis); return poolConfig; } }
- Redis Cluster
import java.time.Duration; import java.util.ArrayList; import java.util.List; 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.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisConfiguration { @Value("${redis.cluster.nodes}") private String redisClusterNodes; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:3000}") private Integer redisConnectTimeout = 3000; @Value("${redis.read.timeout:2000}") private Integer redisReadTimeout = 2000; @Value("${redis.pool.minSize:50}") private Integer redisPoolMinSize = 50; @Value("${redis.pool.maxSize:200}") private Integer redisPoolMaxSize = 200; @Value("${redis.pool.maxWaitMillis:3000}") private Integer redisPoolMaxWaitMillis = 3000; @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}") private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 * 1000; @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}") private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000; @Bean public RedisConnectionFactory redisConnectionFactory(JedisClientConfiguration clientConfiguration) { RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(); List<RedisNode> clusterNodes = new ArrayList<>(); for (String clusterNodeStr : redisClusterNodes.split(",")) { String[] nodeInfo = clusterNodeStr.split(":"); clusterNodes.add(new RedisNode(nodeInfo[0], Integer.valueOf(nodeInfo[1]))); } clusterConfiguration.setClusterNodes(clusterNodes); clusterConfiguration.setPassword(redisPassword); clusterConfiguration.setMaxRedirects(3); return new JedisConnectionFactory(clusterConfiguration, 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(); //Minimum connections in the pool poolConfig.setMinIdle(redisPoolMinSize); //Maximum idle connections in the pool poolConfig.setMaxIdle(redisPoolMaxSize); //Maximum total connections in the pool poolConfig.setMaxTotal(redisPoolMaxSize); //Wait when pool is exhausted? Set to true to wait. To validate setMaxWait, it has to be true. poolConfig.setBlockWhenExhausted(true); //Longest time to wait for connection after pool is exhausted. The default value -1 indicates to wait indefinitely. poolConfig.setMaxWaitMillis(redisPoolMaxWaitMillis); //Set to true to enable connectivity test on creating connections. Default: false. poolConfig.setTestOnCreate(false); //Set to true to enable connectivity test on borrowing connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnBorrow(true); //Set to true to enable connectivity test on returning connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnReturn(false); //Indicates whether to check for idle connections. If this is set to false, idle connections are not evicted. poolConfig.setTestWhileIdle(true); //Duration after which idle connections are evicted. If the idle duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted. poolConfig.setSoftMinEvictableIdleTimeMillis(redisPoolSoftMinEvictableIdleTimeMillis); //Disable MinEvictableIdleTimeMillis(). poolConfig.setMinEvictableIdleTimeMillis(-1); //Interval for checking and evicting idle connections. Default: 60s. poolConfig.setTimeBetweenEvictionRunsMillis(redisPoolBetweenEvictionRunsMillis); return poolConfig; } }
Parameter Description
Parameter |
Default Value |
Description |
---|---|---|
hostName |
localhost |
IP address/domain name for connecting to a DCS Redis instance |
port |
6379 |
Port number |
database |
0 |
Database number. Default: 0. |
password |
- |
Redis instance password |
Parameter |
Description |
---|---|
clusterNodes |
Cluster node connection information, including the node IP address and port number |
maxRedirects |
Maximum redirecting times |
password |
Password |
Parameter |
Default Value |
Description |
---|---|---|
minIdle |
- |
Minimum connections in the connection pool |
maxIdle |
- |
Maximum idle connections in the connection pool |
maxTotal |
- |
Maximum total connections in the connection pool |
blockWhenExhausted |
true |
Indicates whether to wait after the connection pool is exhausted. true: Wait. false: Do not wait. To validate maxWaitMillis, this parameter must be set to true. |
maxWaitMillis |
-1 |
Maximum amount of time (in milliseconds) to wait for connection after the connection pool is exhausted. The default value -1 indicates to wait indefinitely. |
testOnCreate |
false |
Indicates whether to enable connectivity test on creating connections. false: Disable. true: Enable. |
testOnBorrow |
false |
Indicates whether to enable connectivity test on obtaining connections. false: Disable. true: Enable. For heavy-traffic services, set this parameter to false to reduce overhead. |
testOnReturn |
false |
Indicates whether to enable connectivity test on returning connections. false: Disable. true: Enable. For heavy-traffic services, set this parameter to false to reduce overhead. |
testWhileIdle |
false |
Indicates whether to check for idle connections. If this parameter is set to false, idle connections are not evicted. Recommended value: true. |
softMinEvictableIdleTimeMillis |
1800000 |
Duration (in milliseconds) after which idle connections are evicted. If the idle duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted. |
minEvictableIdleTimeMillis |
60000 |
Minimum amount of time (in milliseconds) a connection may remain idle in the pool before it is eligible for eviction. The recommended value is -1, indicating that softMinEvictableIdleTimeMillis is used instead. |
timeBetweenEvictionRunsMillis |
60000 |
Interval (in milliseconds) for checking and evicting idle connections. |
Parameter |
Default Value |
Description |
---|---|---|
connectTimeout |
2000 |
Connection timeout interval, in milliseconds. |
readTimeout |
2000 |
Timeout interval for waiting for a response, in milliseconds. |
poolConfig |
- |
Pool configurations. For details, see JedisPoolConfig. |
Suggestion for Configuring DCS Instances
- Connection pool configuration
The following calculation is applicable only to common service scenarios. You can customize it based on your service requirements.
There is no standard connection pool size. You can configure one based on your service traffic. The following formulas are for reference:
- Minimum number of connections = (QPS of a single node accessing Redis)/(1000 ms/Average time spent on a single command)
- Maximum number of connections = (QPS of a single node accessing Redis)/(1000 ms/Average time spent on a single command) x 150%
For example, if the QPS of a service application is about 10,000, each request needs to access Redis 10 times (that is, 100,000 accesses to Redis every second), and the service application has 10 hosts, the calculation is as follows:
QPS of a single node accessing Redis = 100,000/10 = 10,000
Average time spent on a single command = 20 ms (Redis takes 5 ms to 10 ms to process a single command under normal conditions. If network jitter occurs, it takes 15 ms to 20 ms.)
Minimum number of connections = 10,000/(1000 ms/20 ms) = 200
Maximum number of connections = 10,000/(1000 ms/20 ms) × 150% = 300
Connecting to Redis on Lettuce (Java)
This section describes how to access a Redis instance on Lettuce. For more information about how to use other Redis clients, visit the Redis official website.
Spring Data Redis is already integrated with Jedis and Lettuce for Spring Boot projects. In addition, Spring Boot 1.x is integrated with Jedis, and Spring Boot 2.x with Lettuce. Therefore, you do not need to import Lettuce in Spring Boot 2.x and later projects.
Notes and Constraints
Springboot 2.3.12.RELEASE or later is required. Lettuce 6.3.0.RELEASE or later is required. Netty 4.1.100.Final or later is required.
Prerequisites
- A Redis instance is created, and is in the Running state.
- View the IP address/domain name and port of the DCS Redis instance to be accessed. For details, see "Getting Started" > "Viewing Details of a DCS Instance" in Distributed Cache Service (DCS) 2.3.0.1 User Guide (for Huawei Cloud Stack 8.5.1).
Pom Configuration
<!-- Enable Spring Data Redis. Lettuce-supported SDK is integrated by default. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.3.0.RELEASE</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-transport-native-epoll</artifactId> <version>4.1.100.Final</version> <classifier>linux-x86_64</classifier> </dependency>
application.properties Configuration
- Single-node, master/standby, read/write splitting, and Proxy Cluster
#Redis host spring.redis.host=<host> #Redis port spring.redis.port=<port> #Redis database number spring.redis.database=0 #Redis password spring.redis.password=<password> #Redis read/write timeout spring.redis.timeout=2000
- Redis Cluster
#Redis Cluster node information spring.redis.cluster.nodes=<ip:port>,<ip:port>,<ip:port> #Redis Cluster max redirecting times spring.redis.cluster.max-redirects=3 #Redis Cluster node password spring.redis.password=<password> #Redis Cluster timeout spring.redis.timeout=2000 #Enable adaptive topology refresh spring.redis.lettuce.cluster.refresh.adaptive=true #Enable topology refresh every 10 seconds spring.redis.lettuce.cluster.refresh.period=10S
Bean Configuration
- Single-node, master/standby, read/write splitting, and Proxy Cluster
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.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import io.lettuce.core.ClientOptions; import io.lettuce.core.SocketOptions; /** * Lettuce non-pooling configuration (use either this or the application.properties configuration) */ @Configuration public class RedisConfiguration { @Value("${redis.host}") private String redisHost; @Value("${redis.port:6379}") private Integer redisPort = 6379; @Value("${redis.database:0}") private Integer redisDatabase = 0; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:2000}") private Integer redisConnectTimeout = 2000; @Value("${redis.read.timeout:2000}") private Integer redisReadTimeout = 2000; /** * TCP_KEEPALIVE configuration parameters: * A keepalive interval = TCP_KEEPALIVE_TIME = 30 * Idle duration before keepalive = TCP_KEEPALIVE_TIME/3 = 10 * keepalive xx times before disconnect = TCP_KEEPALIVE_COUNT = 3 */ private static final int TCP_KEEPALIVE_TIME = 30; /** * TCP_USER_TIMEOUT Idle duration limit, to address Lettuce timeout. * refer: https://github.com/lettuce-io/lettuce-core/issues/2082 */ private static final int TCP_USER_TIMEOUT = 30; @Bean public RedisConnectionFactory redisConnectionFactory(LettuceClientConfiguration clientConfiguration) { RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration(); standaloneConfiguration.setHostName(redisHost); standaloneConfiguration.setPort(redisPort); standaloneConfiguration.setDatabase(redisDatabase); standaloneConfiguration.setPassword(redisPassword); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(standaloneConfiguration, clientConfiguration); connectionFactory.setDatabase(redisDatabase); return connectionFactory; } @Bean public LettuceClientConfiguration clientConfiguration() { SocketOptions socketOptions = SocketOptions.builder() .keepAlive(SocketOptions.KeepAliveOptions.builder() // A keepalive interval .idle(Duration.ofSeconds(TCP_KEEPALIVE_TIME)) // Idle duration before keepalive .interval(Duration.ofSeconds(TCP_KEEPALIVE_TIME/3)) // keepalive xx times before disconnect .count(3) // Whether to keep connections alive. .enable() .build()) .tcpUserTimeout(SocketOptions.TcpUserTimeoutOptions.builder() // Addressing timeouts caused by RST on the server .tcpUserTimeout(Duration.ofSeconds(TCP_USER_TIMEOUT)) .enable() .build()) // TCP connection timeout setting .connectTimeout(Duration.ofMillis(redisConnectTimeout)) .build(); ClientOptions clientOptions = ClientOptions.builder() .autoReconnect(true) .pingBeforeActivateConnection(true) .cancelCommandsOnReconnectFailure(false) .disconnectedBehavior(ClientOptions.DisconnectedBehavior.ACCEPT_COMMANDS) .socketOptions(socketOptions) .build(); LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder() .commandTimeout(Duration.ofMillis(redisReadTimeout)) .readFrom(ReadFrom.MASTER) .clientOptions(clientOptions) .build(); return clientConfiguration; } }
- Pooling configuration for single-node, master/standby, read/write splitting, and Proxy Cluster instancesEnable the pooling component
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.11.1</version> </dependency>
Code
import java.time.Duration; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 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.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import io.lettuce.core.ClientOptions; import io.lettuce.core.SocketOptions; /** * Lettuce pooling configuration */ @Configuration public class RedisPoolConfiguration { @Value("${redis.host}") private String redisHost; @Value("${redis.port:6379}") private Integer redisPort = 6379; @Value("${redis.database:0}") private Integer redisDatabase = 0; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:2000}") private Integer redisConnectTimeout = 2000; @Value("${redis.read.timeout:2000}") private Integer redisReadTimeout = 2000; @Value("${redis.pool.minSize:50}") private Integer redisPoolMinSize = 50; @Value("${redis.pool.maxSize:200}") private Integer redisPoolMaxSize = 200; @Value("${redis.pool.maxWaitMillis:2000}") private Integer redisPoolMaxWaitMillis = 2000; @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}") private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 * 1000; @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}") private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000; /** * TCP_KEEPALIVE configuration parameters: * A keepalive interval = TCP_KEEPALIVE_TIME = 30 * Idle duration before keepalive = TCP_KEEPALIVE_TIME/3 = 10 * keepalive xx times before disconnect = TCP_KEEPALIVE_COUNT = 3 */ private static final int TCP_KEEPALIVE_TIME = 30; /** * TCP_USER_TIMEOUT Idle duration limit, to address Lettuce timeout. * refer: https://github.com/lettuce-io/lettuce-core/issues/2082 */ private static final int TCP_USER_TIMEOUT = 30; @Bean public RedisConnectionFactory redisConnectionFactory(LettuceClientConfiguration clientConfiguration) { RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration(); standaloneConfiguration.setHostName(redisHost); standaloneConfiguration.setPort(redisPort); standaloneConfiguration.setDatabase(redisDatabase); standaloneConfiguration.setPassword(redisPassword); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(standaloneConfiguration, clientConfiguration); connectionFactory.setDatabase(redisDatabase); //Disable sharing native connection before enabling pooling connectionFactory.setShareNativeConnection(false); return connectionFactory; } @Bean public LettuceClientConfiguration clientConfiguration() { SocketOptions socketOptions = SocketOptions.builder() .keepAlive(SocketOptions.KeepAliveOptions.builder() // A keepalive interval .idle(Duration.ofSeconds(TCP_KEEPALIVE_TIME)) // Idle duration before keepalive .interval(Duration.ofSeconds(TCP_KEEPALIVE_TIME/3)) // keepalive xx times before disconnect .count(3) // Whether to keep connections alive. .enable() .build()) .tcpUserTimeout(SocketOptions.TcpUserTimeoutOptions.builder() // Addressing timeouts caused by RST on the server .tcpUserTimeout(Duration.ofSeconds(TCP_USER_TIMEOUT)) .enable() .build()) // TCP connection timeout setting .connectTimeout(Duration.ofMillis(redisConnectTimeout)) .build(); ClientOptions clientOptions = ClientOptions.builder() .autoReconnect(true) .pingBeforeActivateConnection(true) .cancelCommandsOnReconnectFailure(false) .disconnectedBehavior(ClientOptions.DisconnectedBehavior.ACCEPT_COMMANDS) .socketOptions(socketOptions) .build(); LettucePoolingClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder() .poolConfig(poolConfig()) .commandTimeout(Duration.ofMillis(redisReadTimeout)) .clientOptions(clientOptions) .readFrom(ReadFrom.MASTER) .build(); return poolingClientConfiguration; } private GenericObjectPoolConfig redisPoolConfig() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); //Minimum idle connections in the pool poolConfig.setMinIdle(redisPoolMinSize); //Maximum idle connections in the pool poolConfig.setMaxIdle(redisPoolMaxSize); //Maximum total connections in the pool poolConfig.setMaxTotal(redisPoolMaxSize); //Wait when pool is exhausted? Set to true to wait. To validate setMaxWait, it has to be true. poolConfig.setBlockWhenExhausted(true); //Max allowed time to wait for connection after pool is exhausted. The default value -1 indicates to wait indefinitely. poolConfig.setMaxWait(Duration.ofMillis(redisPoolMaxWaitMillis)); //Set to true to enable connectivity test on creating connections. Default: false. poolConfig.setTestOnCreate(false); //Set to true to enable connectivity test on borrowing connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnBorrow(true); //Set to true to enable connectivity test on returning connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnReturn(false); //Indicates whether to check for idle connections. If this is set to false, idle connections are not evicted. poolConfig.setTestWhileIdle(true); //Idle duration after which a connection is evicted. If the actual duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted. poolConfig.setSoftMinEvictableIdleTime(Duration.ofMillis(redisPoolSoftMinEvictableIdleTimeMillis)); //Disable eviction policy MinEvictableIdleTimeMillis(). poolConfig.setMinEvictableIdleTime(Duration.ofMillis(-1)); //Interval for checking and evicting idle connections. Default: 60s. poolConfig.setTimeBetweenEvictionRuns(Duration.ofMillis(redisPoolBetweenEvictionRunsMillis)); return poolConfig; } }
- Configuration for Redis Cluster instances
import java.time.Duration; import java.util.ArrayList; import java.util.List; 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.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import io.lettuce.core.ClientOptions; import io.lettuce.core.SocketOptions; import io.lettuce.core.cluster.ClusterClientOptions; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; /** * Lettuce Cluster non-pooling configuration (use either this or the application.properties configuration) */ @Configuration public class RedisConfiguration { @Value("${redis.cluster.nodes}") private String redisClusterNodes; @Value("${redis.cluster.maxDirects:3}") private Integer redisClusterMaxDirects; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:2000}") private Integer redisConnectTimeout = 2000; @Value("${redis.read.timeout:2000}") private Integer redisReadTimeout = 2000; @Value("${redis.cluster.topology.refresh.period.millis:10000}") private Integer redisClusterTopologyRefreshPeriodMillis = 10000; /** * TCP_KEEPALIVE configuration parameters: * A keepalive interval = TCP_KEEPALIVE_TIME = 30 * Idle duration before keepalive = TCP_KEEPALIVE_TIME/3 = 10 * keepalive xx times before disconnect = TCP_KEEPALIVE_COUNT = 3 */ private static final int TCP_KEEPALIVE_TIME = 30; /** * TCP_USER_TIMEOUT Idle duration limit, to address Lettuce timeout. * refer: https://github.com/lettuce-io/lettuce-core/issues/2082 */ private static final int TCP_USER_TIMEOUT = 30; @Bean public RedisConnectionFactory redisConnectionFactory(LettuceClientConfiguration clientConfiguration) { RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(); List<RedisNode> clusterNodes = new ArrayList<>(); for (String clusterNodeStr : redisClusterNodes.split(",")) { String[] nodeInfo = clusterNodeStr.split(":"); clusterNodes.add(new RedisNode(nodeInfo[0], Integer.valueOf(nodeInfo[1]))); } clusterConfiguration.setClusterNodes(clusterNodes); clusterConfiguration.setPassword(redisPassword); clusterConfiguration.setMaxRedirects(redisClusterMaxDirects); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfiguration, clientConfiguration); return connectionFactory; } @Bean public LettuceClientConfiguration clientConfiguration() { SocketOptions socketOptions = SocketOptions.builder() .keepAlive(SocketOptions.KeepAliveOptions.builder() // A keepalive interval .idle(Duration.ofSeconds(TCP_KEEPALIVE_TIME)) // Idle duration before keepalive .interval(Duration.ofSeconds(TCP_KEEPALIVE_TIME/3)) // keepalive xx times before disconnect .count(3) // Whether to keep connections alive. .enable() .build()) .tcpUserTimeout(SocketOptions.TcpUserTimeoutOptions.builder() // Addressing timeouts caused by RST on the server .tcpUserTimeout(Duration.ofSeconds(TCP_USER_TIMEOUT)) .enable() .build()) // TCP connection timeout setting .connectTimeout(Duration.ofMillis(redisConnectTimeout)) .build(); ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder() .enableAllAdaptiveRefreshTriggers() .enablePeriodicRefresh(Duration.ofMillis(redisClusterTopologyRefreshPeriodMillis)) .build(); ClusterClientOptions clientOptions = ClusterClientOptions.builder() .autoReconnect(true) .pingBeforeActivateConnection(true) .cancelCommandsOnReconnectFailure(false) .disconnectedBehavior(ClientOptions.DisconnectedBehavior.ACCEPT_COMMANDS) .socketOptions(socketOptions) .topologyRefreshOptions(topologyRefreshOptions) .build(); LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder() .commandTimeout(Duration.ofMillis(redisReadTimeout)) .readFrom(ReadFrom.MASTER) .clientOptions(clientOptions) .build(); return clientConfiguration; } }
- Pooling configuration for Redis Cluster instancesEnable the pooling component
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.11.1</version> </dependency>
Code
import java.time.Duration; import java.util.ArrayList; import java.util.List; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 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.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import io.lettuce.core.ClientOptions; import io.lettuce.core.SocketOptions; import io.lettuce.core.cluster.ClusterClientOptions; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; /** * Lettuce pooling configuration */ @Configuration public class RedisPoolConfiguration { @Value("${redis.cluster.nodes}") private String redisClusterNodes; @Value("${redis.cluster.maxDirects:3}") private Integer redisClusterMaxDirects; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:2000}") private Integer redisConnectTimeout = 2000; @Value("${redis.read.timeout:2000}") private Integer redisReadTimeout = 2000; @Value("${redis.cluster.topology.refresh.period.millis:10000}") private Integer redisClusterTopologyRefreshPeriodMillis = 10000; @Value("${redis.pool.minSize:50}") private Integer redisPoolMinSize = 50; @Value("${redis.pool.maxSize:200}") private Integer redisPoolMaxSize = 200; @Value("${redis.pool.maxWaitMillis:2000}") private Integer redisPoolMaxWaitMillis = 2000; @Value("${redis.pool.softMinEvictableIdleTimeMillis:1800000}") private Integer redisPoolSoftMinEvictableIdleTimeMillis = 30 * 60 * 1000; @Value("${redis.pool.timeBetweenEvictionRunsMillis:60000}") private Integer redisPoolBetweenEvictionRunsMillis = 60 * 1000; /** * TCP_KEEPALIVE configuration parameters: * A keepalive interval = TCP_KEEPALIVE_TIME = 30 * Idle duration before keepalive = TCP_KEEPALIVE_TIME/3 = 10 * keepalive xx times before disconnect = TCP_KEEPALIVE_COUNT = 3 */ private static final int TCP_KEEPALIVE_TIME = 30; /** * TCP_USER_TIMEOUT Idle duration limit, to address Lettuce timeout. * refer: https://github.com/lettuce-io/lettuce-core/issues/2082 */ private static final int TCP_USER_TIMEOUT = 30; @Bean public RedisConnectionFactory redisConnectionFactory(LettuceClientConfiguration clientConfiguration) { RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(); List<RedisNode> clusterNodes = new ArrayList<>(); for (String clusterNodeStr : redisClusterNodes.split(",")) { String[] nodeInfo = clusterNodeStr.split(":"); clusterNodes.add(new RedisNode(nodeInfo[0], Integer.valueOf(nodeInfo[1]))); } clusterConfiguration.setClusterNodes(clusterNodes); clusterConfiguration.setPassword(redisPassword); clusterConfiguration.setMaxRedirects(redisClusterMaxDirects); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfiguration, clientConfiguration); //Disable native connection sharing before validating connection pool connectionFactory.setShareNativeConnection(false); return connectionFactory; } @Bean public LettuceClientConfiguration clientConfiguration() { SocketOptions socketOptions = SocketOptions.builder() .keepAlive(SocketOptions.KeepAliveOptions.builder() // A keepalive interval .idle(Duration.ofSeconds(TCP_KEEPALIVE_TIME)) // Idle duration before keepalive .interval(Duration.ofSeconds(TCP_KEEPALIVE_TIME/3)) // keepalive xx times before disconnect .count(3) // Whether to keep connections alive. .enable() .build()) .tcpUserTimeout(SocketOptions.TcpUserTimeoutOptions.builder() // Addressing timeouts caused by RST on the server .tcpUserTimeout(Duration.ofSeconds(TCP_USER_TIMEOUT)) .enable() .build()) // TCP connection timeout setting .connectTimeout(Duration.ofMillis(redisConnectTimeout)) .build(); ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder() .enableAllAdaptiveRefreshTriggers() .enablePeriodicRefresh(Duration.ofMillis(redisClusterTopologyRefreshPeriodMillis)) .build(); ClusterClientOptions clientOptions = ClusterClientOptions.builder() .autoReconnect(true) .pingBeforeActivateConnection(true) .cancelCommandsOnReconnectFailure(false) .disconnectedBehavior(ClientOptions.DisconnectedBehavior.ACCEPT_COMMANDS) .socketOptions(socketOptions) .topologyRefreshOptions(topologyRefreshOptions) .build(); LettucePoolingClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder() .poolConfig(poolConfig()) .commandTimeout(Duration.ofMillis(redisReadTimeout)) .clientOptions(clientOptions) .readFrom(ReadFrom.MASTER) .build(); return clientConfiguration; } private GenericObjectPoolConfig poolConfig() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); //Minimum connections in the pool poolConfig.setMinIdle(redisPoolMinSize); //Maximum idle connections in the pool poolConfig.setMaxIdle(redisPoolMaxSize); //Maximum total connections in the pool poolConfig.setMaxTotal(redisPoolMaxSize); //Wait when pool is exhausted? Set to true to wait. To validate setMaxWait, it has to be true. poolConfig.setBlockWhenExhausted(true); //Max allowed time to wait for connection after pool is exhausted. The default value -1 indicates to wait indefinitely. poolConfig.setMaxWait(Duration.ofMillis(redisPoolMaxWaitMillis)); //Set to true to enable connectivity test on creating connections. Default: false. poolConfig.setTestOnCreate(false); //Set to true to enable connectivity test on borrowing connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnBorrow(true); //Set to true to enable connectivity test on returning connections. Default: false. Set to false for heavy-traffic services to reduce overhead. poolConfig.setTestOnReturn(false); //Indicates whether to check for idle connections. If this is set to false, idle connections are not evicted. poolConfig.setTestWhileIdle(true); //Disable connection closure when the minimum idle time is reached. poolConfig.setMinEvictableIdleTime(Duration.ofMillis(-1)); //Idle duration before a connection being evicted. If the actual duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted. MinEvictableIdleTimeMillis (default eviction policy) is no longer used. poolConfig.setSoftMinEvictableIdleTime(Duration.ofMillis(redisPoolSoftMinEvictableIdleTimeMillis)); //Interval for checking and evicting idle connections. Default: 60s. poolConfig.setTimeBetweenEvictionRuns(Duration.ofMillis(redisPoolBetweenEvictionRunsMillis)); return poolConfig; } }
Parameter Description
Parameter |
Type |
Default Value |
Description |
---|---|---|---|
configuration |
RedisConfiguration |
- |
Redis connection configuration. Two subsclasses:
|
clientConfiguration |
LettuceClientConfiguration |
- |
Client configuration parameter. Common subclass: LettucePoolingClientConfiguration |
shareNativeConnection |
boolean |
true |
Indicates whether to share native connections. Set to true to share. Set to false to enable connection pooling. |
Parameter |
Default Value |
Description |
---|---|---|
hostName |
localhost |
IP address/domain name for connecting to a DCS Redis instance |
port |
6379 |
Port number |
database |
0 |
Database subscript |
password |
- |
Password |
Parameter |
Description |
---|---|
clusterNodes |
Cluster node connection information, including the node IP address and port number |
maxRedirects |
Maximum redirecting times. Recommended value: 3. |
password |
Password |
Parameter |
Type |
Default Value |
Description |
---|---|---|---|
timeout |
Duration |
60s |
Command timeout: Recommended: 2s. |
clientOptions |
ClientOptions |
- |
Configuration options. |
readFrom |
readFrom |
MASTER |
Read mode. Recommended: MASTER. Other values may cause access failures in failover scenarios. |
Parameter |
Type |
Default Value |
Description |
---|---|---|---|
timeout |
Duration |
60s |
Command timeout: Recommended: 2s. |
clientOptions |
ClientOptions |
- |
Configuration options. |
poolConfig |
GenericObjectPoolConfig |
- |
Connection pool configuration. |
readFrom |
readFrom |
MASTER |
Read mode. Recommended: MASTER. Other values may cause access failures in failover scenarios. |
Parameter |
Type |
Default Value |
Description |
---|---|---|---|
autoReconnect |
boolean |
true |
Indicates whether to automatically reconnect after disconnection. Recommended: true. |
pingBeforeActivateConnection |
boolean |
true |
Indicates whether to test connectivity on established connections. Recommended: true. |
cancelCommandsOnReconnectFailure |
boolean |
true |
Indicates whether to cancel commands after a failed reconnection attempt. Recommended: false. |
disconnectedBehavior |
DisconnectedBehavior |
DisconnectedBehavior.DEFAULT |
Indicates what to do when a connection drops. Recommended: ACCEPT_COMMANDS.
|
socketOptions |
SocketOptions |
- |
Socket configuration. |
Parameter |
Default Value |
Description |
---|---|---|
connectTimeout |
10s |
Connection timeout. Recommended: 2s. |
Parameter |
Default Value |
Description |
---|---|---|
minIdle |
- |
Minimum connections in the pool. |
maxIdle |
- |
Maximum idle connections in the connection pool. |
maxTotal |
- |
Maximum total connections in the connection pool. |
blockWhenExhausted |
true |
Indicates whether to wait after the connection pool is exhausted. true: Wait. false: Do not wait. To validate maxWaitMillis, this parameter must be set to true. |
maxWaitMillis |
-1 |
Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. The default value –1 indicates to wait indefinitely. |
testOnCreate |
false |
Set to true to enable connectivity test on creating connections. Default: false. |
testOnBorrow |
false |
Set to true to enable connectivity test on borrowing connections. Default: false. Set to false for heavy-traffic services to reduce overhead. |
testOnReturn |
false |
Set to true to enable connectivity test on returning connections. Default: false. Set to false for heavy-traffic services to reduce overhead. |
testWhileIdle |
false |
Indicates whether to check for idle connections. If this parameter is set to false, idle connections are not evicted. Recommended value: true. |
softMinEvictableIdleTimeMillis |
-1 |
Duration (in milliseconds) after which idle connections are evicted. If the idle duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted. Recommended value: 1800000. |
minEvictableIdleTimeMillis |
1800000 |
An eviction policy, set to –1 (suggested) to disable it. Use softminEvictableIdleTimeMillis instead. |
timeBetweenEvictionRunsMillis |
-1 |
Eviction interval, in milliseconds. Recommended value: 60000 |
Suggestion for Configuring DCS Instances
- Pooling connection
Different from Jedis's BIO, the bottom layer of Lettuce communicates with Redis Server based on Netty's NIO. Combining persistent connections and queues, Lettuce sends and receives multiple requests and responses spontaneously with sequential sending and receiving features of TCP. A single connection supports 3000 to 5000 QPS, but you are not advised to allow more than 3000 QPS in production systems. Pooling is not supported by Lettuce, and is disabled by default in Spring Boot. To enable pooling, validate the commons-pool2 dependency and disable native connection sharing.
By default, each Lettuce connection needs two thread pools, I/O thread pool and computation thread pool, to support I/O event reading and asynchronous event processing. If you configure connection pooling, each connection creates two thread pools, consuming high memory resources. Lettuce is strong at processing single connections based on its bottom-layer implementation, so you are not advised to use Lettuce with pooling.
- Topology refresh
When connecting to a Redis Cluster instance, Lettuce randomly sends cluster nodes to the node list during initialization to obtain the distribution of cluster slots. Cluster topology structure changes when the cluster capacity is increased or decreased or a master/standby switchover occurs. Lettuce does not detect such changes by default. You can enable detection with the following configurations:
- application.properties configuration
#Enable adaptive topology refresh. spring.redis.lettuce.cluster.refresh.adaptive=true #Enable topology refresh every 10 seconds. spring.redis.lettuce.cluster.refresh.period=10S
- API configuration
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder() .enableAllAdaptiveRefreshTriggers() .enablePeriodicRefresh(Duration.ofMillis(redisClusterTopologyRefreshPeriodMillis)) .build(); ClusterClientOptions clientOptions = ClusterClientOptions.builder() ... ... .topologyRefreshOptions(topologyRefreshOptions) .build();
- application.properties configuration
- Blast radius
The bottom layer of Lettuce uses a combination of single persistent connection and request queue. Once network jitter or intermittent disconnection occurs or connection times out, all requests are affected. Especially when connection times out, an attempt is made to resend TCP pockets until timeout and connection drops. Requests do not recover until connections are reestablished. Requests accumulate during resending attempts. If upper-layer services time out in batches, or the resending timeout is too long in some OSs' kernels, the service system remains unavailable for a long time. Therefore, you are advised to use Jedis instead of Lettuce.
Connecting to Redis on Redisson (Java)
This section describes how to access a Redis instance on Redisson. For more information about how to use other Redis clients, visit the Redis official website.
For Spring Boot projects, Spring Data Redis is already integrated with Jedis and Lettuce, but does not support Redisson. Redisson provides the redisson-spring-boot-starter component (https://mvnrepository.com/artifact/org.redisson/redisson) that can be used with Spring Boot.
Spring Boot 1.x is integrated with Jedis, and Spring Boot 2.x is integrated with Lettuce.
Notes and Constraints
- If a password was set during DCS Redis instance creation, configure the password for connecting to Redis using Redisson. Do not hard code the plaintext password.
- To connect to a single-node, read/write splitting, or Proxy Cluster instance, use the useSingleServer method of the SingleServerConfig object of Redisson. To connect to a master/standby instance, use the useMasterSlaveServers method of the MasterSlaveServersConfig object of Redisson. To connect to a Redis Cluster instance, use the useClusterServers method of the ClusterServersConfig object.
- Springboot 2.3.12.RELEASE or later is required. Redisson 3.37.0 or later is required.
Prerequisites
- A Redis instance is created, and is in the Running state.
- View the IP address/domain name and port of the DCS Redis instance to be accessed. For details, see "Getting Started" > "Viewing Details of a DCS Instance" in Distributed Cache Service (DCS) 2.3.0.1 User Guide (for Huawei Cloud Stack 8.5.1).
Pom Configuration
<!-- spring-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <!--Lettuce is integrated in Spring Boot 2.x by default. This dependency needs to be deleted. --> <exclusion> <artifactId>lettuce-core</artifactId> <groupId>io.lettuce</groupId> </exclusion> </exclusions> </dependency> <!--Redisson's adaptation package for Spring Boot--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>${redisson.version}</version> </dependency>
Bean Configuration
Spring Boot does not provide Redisson adaptation, and the application.properties configuration file does not have the corresponding configuration item. Therefore, you can only use Bean configuration.
- Single-node, read/write splitting, and Proxy Cluster
import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.codec.JsonJacksonCodec; import org.redisson.config.Config; import org.redisson.config.SingleServerConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SingleConfig { @Value("${redis.address:}") private String redisAddress; @Value("${redis.password:}") private String redisPassword; @Value("${redis.database:0}") private Integer redisDatabase = 0; @Value("${redis.connect.timeout:3000}") private Integer redisConnectTimeout = 3000; @Value("${redis.connection.idle.timeout:10000}") private Integer redisConnectionIdleTimeout = 10000; @Value("${redis.connection.ping.interval:1000}") private Integer redisConnectionPingInterval = 1000; @Value("${redis.timeout:2000}") private Integer timeout = 2000; @Value("${redis.connection.pool.min.size:50}") private Integer redisConnectionPoolMinSize; @Value("${redis.connection.pool.max.size:200}") private Integer redisConnectionPoolMaxSize; @Value("${redis.retry.attempts:3}") private Integer redisRetryAttempts = 3; @Value("${redis.retry.interval:200}") private Integer redisRetryInterval = 200; @Bean public RedissonClient redissonClient(){ Config redissonConfig = new Config(); SingleServerConfig serverConfig = redissonConfig.useSingleServer(); serverConfig.setAddress(redisAddress); serverConfig.setConnectionMinimumIdleSize(redisConnectionPoolMinSize); serverConfig.setConnectionPoolSize(redisConnectionPoolMaxSize); serverConfig.setDatabase(redisDatabase); serverConfig.setPassword(redisPassword); serverConfig.setConnectTimeout(redisConnectTimeout); serverConfig.setIdleConnectionTimeout(redisConnectionIdleTimeout); serverConfig.setPingConnectionInterval(redisConnectionPingInterval); serverConfig.setTimeout(timeout); serverConfig.setRetryAttempts(redisRetryAttempts); serverConfig.setRetryInterval(redisRetryInterval); redissonConfig.setCodec(new JsonJacksonCodec()); return Redisson.create(redissonConfig); } }
- Master/Standby
import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.codec.JsonJacksonCodec; import org.redisson.config.Config; import org.redisson.config.MasterSlaveServersConfig; import org.redisson.config.ReadMode; import org.redisson.config.SubscriptionMode; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashSet; @Configuration public class MasterStandbyConfig { @Value("${redis.master.address}") private String redisMasterAddress; @Value("${redis.slave.address}") private String redisSlaveAddress; @Value("${redis.database:0}") private Integer redisDatabase = 0; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:3000}") private Integer redisConnectTimeout = 3000; @Value("${redis.connection.idle.timeout:10000}") private Integer redisConnectionIdleTimeout = 10000; @Value("${redis.connection.ping.interval:1000}") private Integer redisConnectionPingInterval = 1000; @Value("${redis.timeout:2000}") private Integer timeout = 2000; @Value("${redis.master.connection.pool.min.size:50}") private Integer redisMasterConnectionPoolMinSize = 50; @Value("${redis.master.connection.pool.max.size:200}") private Integer redisMasterConnectionPoolMaxSize = 200; @Value("${redis.retry.attempts:3}") private Integer redisRetryAttempts = 3; @Value("${redis.retry.interval:200}") private Integer redisRetryInterval = 200; @Bean public RedissonClient redissonClient() { Config redissonConfig = new Config(); MasterSlaveServersConfig serverConfig = redissonConfig.useMasterSlaveServers(); serverConfig.setMasterAddress(redisMasterAddress); HashSet<String> slaveSet = new HashSet<>(); slaveSet.add(redisSlaveAddress); serverConfig.setSlaveAddresses(slaveSet); serverConfig.setDatabase(redisDatabase); serverConfig.setPassword(redisPassword); serverConfig.setMasterConnectionMinimumIdleSize(redisMasterConnectionPoolMinSize); serverConfig.setMasterConnectionPoolSize(redisMasterConnectionPoolMaxSize); serverConfig.setReadMode(ReadMode.MASTER); serverConfig.setSubscriptionMode(SubscriptionMode.MASTER); serverConfig.setConnectTimeout(redisConnectTimeout); serverConfig.setIdleConnectionTimeout(redisConnectionIdleTimeout); serverConfig.setPingConnectionInterval(redisConnectionPingInterval); serverConfig.setTimeout(timeout); serverConfig.setRetryAttempts(redisRetryAttempts); serverConfig.setRetryInterval(redisRetryInterval); redissonConfig.setCodec(new JsonJacksonCodec()); return Redisson.create(redissonConfig); } }
- Redis Cluster
import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.codec.JsonJacksonCodec; import org.redisson.config.ClusterServersConfig; import org.redisson.config.Config; import org.redisson.config.ReadMode; import org.redisson.config.SubscriptionMode; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.List; @Configuration public class ClusterConfig { @Value("${redis.cluster.address}") private List<String> redisClusterAddress; @Value("${redis.cluster.scan.interval:5000}") private Integer redisClusterScanInterval = 5000; @Value("${redis.password:}") private String redisPassword; @Value("${redis.connect.timeout:3000}") private Integer redisConnectTimeout = 3000; @Value("${redis.connection.idle.timeout:10000}") private Integer redisConnectionIdleTimeout = 10000; @Value("${redis.connection.ping.interval:1000}") private Integer redisConnectionPingInterval = 1000; @Value("${redis.timeout:2000}") private Integer timeout = 2000; @Value("${redis.retry.attempts:3}") private Integer redisRetryAttempts = 3; @Value("${redis.retry.interval:200}") private Integer redisRetryInterval = 200; @Value("${redis.master.connection.pool.min.size:50}") private Integer redisMasterConnectionPoolMinSize = 50; @Value("${redis.master.connection.pool.max.size:200}") private Integer redisMasterConnectionPoolMaxSize = 200; @Bean public RedissonClient redissonClient() { Config redissonConfig = new Config(); ClusterServersConfig serverConfig = redissonConfig.useClusterServers(); serverConfig.setNodeAddresses(redisClusterAddress); serverConfig.setScanInterval(redisClusterScanInterval); serverConfig.setPassword(redisPassword); serverConfig.setMasterConnectionMinimumIdleSize(redisMasterConnectionPoolMinSize); serverConfig.setMasterConnectionPoolSize(redisMasterConnectionPoolMaxSize); serverConfig.setReadMode(ReadMode.MASTER); serverConfig.setSubscriptionMode(SubscriptionMode.MASTER); serverConfig.setConnectTimeout(redisConnectTimeout); serverConfig.setIdleConnectionTimeout(redisConnectionIdleTimeout); serverConfig.setPingConnectionInterval(redisConnectionPingInterval); serverConfig.setTimeout(timeout); serverConfig.setRetryAttempts(redisRetryAttempts); serverConfig.setRetryInterval(redisRetryInterval); redissonConfig.setCodec(new JsonJacksonCodec()); return Redisson.create(redissonConfig); } }
Parameter Description
Parameter |
Default Value |
Description |
---|---|---|
codec |
org.redisson.codec.JsonJacksonCodec |
Encoding format, including JSON, Avro, Smile, CBOR, and MsgPack. |
threads |
Number of CPU cores x 2 |
Thread pool used for executing RTopic Listener, RRemoteService, and RExecutorService. |
executor |
null |
The function is the same as threads. If this parameter is not set, a thread pool is initialized based on threads. |
nettyThreads |
Number of CPU cores x 2 |
Thread pool used by the TCP channel that connects to the redis-server. All channels share this connection pool and are mapped to Netty's Bootstrap.group(...). |
eventLoopGroup |
null |
The function is the same as nettyThreads. If this parameter is not set, an EventLoopGroup is initialized based on the nettyThreads parameter for the bottom-layer TCP channel to use. |
transportMode |
TransportMode.NIO |
Transmission mode. The options are NIO, EPOLL (additional package required), and KQUEUE (additional package required). |
lockWatchdogTimeout |
30000 |
Timeout interval (in milliseconds) of the lock-monitoring watchdog. In the distributed lock scenario, if the leaseTimeout parameter is not specified, the default value of this parameter is used. |
keepPubSubOrder |
true |
Indicates whether to receive messages in the publish sequence. If messages can be processed concurrently, you are advised to set this parameter to false. |
Parameter |
Default Value |
Description |
---|---|---|
address |
- |
Node connection information, in redis://ip:port format. |
database |
0 |
ID of the database to be used. |
connectionMinimumIdleSize |
32 |
Minimum number of connections to the master node of each shard. |
connectionPoolSize |
64 |
Maximum number of connections to the master node of each shard. |
subscriptionConnectionMinimumIdleSize |
1 |
Minimum number of connections to the target node for pub/sub. |
subscriptionConnectionPoolSize |
50 |
Maximum number of connections to the target node for pub/sub. |
subcriptionPerConnection |
5 |
Maximum number of subscriptions on each subscription connection. |
connectionTimeout |
10000 |
Connection timeout interval, in milliseconds. |
idleConnectionTimeout |
10000 |
Maximum time (in milliseconds) for reclaiming idle connections. |
pingConnectionInterval |
30000 |
Heartbeat for detecting available connections, in milliseconds. Recommended: 3000 ms. |
timeout |
3000 |
Timeout interval for waiting for a response, in milliseconds. |
retryAttempts |
3 |
Maximum number of retries upon send failures. |
retryInterval |
1500 |
Retry interval, in milliseconds. Recommended: 200 ms. |
clientName |
null |
Client name. |
Parameter |
Default Value |
Description |
---|---|---|
masterAddress |
- |
Master node connection information, in redis://ip:port format. |
slaveAddresses |
- |
Standby node connection information, in Set<redis://ip:port> format. |
readMode |
SLAVE |
Read mode. By default, read traffic is distributed to replica nodes. The value can be MASTER (recommended), SLAVE, or MASTER_SLAVE. Other values may cause access failures in failover scenarios. |
loadBalancer |
RoundRobinLoadBalancer |
Load balancing algorithm. This parameter is valid only when readMode is set to SLAVE or MASTER_SLAVE. Read traffic is distributed evenly. |
masterConnectionMinimumIdleSize |
32 |
Minimum number of connections to the master node of each shard. |
masterConnectionPoolSize |
64 |
Maximum number of connections to the master node of each shard. |
slaveConnectionMinimumIdleSize |
32 |
Minimum number of connections to each replica node of each shard. If readMode is set to MASTER, the value of this parameter is invalid. |
slaveConnectionPoolSize |
64 |
Maximum number of connections to each replica node of each shard. If readMode is set to MASTER, the value of this parameter is invalid. |
subscriptionMode |
SLAVE |
Subscription mode. By default, only replica nodes handle subscription. The value can be SLAVE or MASTER (recommended). |
subscriptionConnectionMinimumIdleSize |
1 |
Minimum number of connections to the target node for pub/sub. |
subscriptionConnectionPoolSize |
50 |
Maximum number of connections to the target node for pub/sub. |
subcriptionPerConnection |
5 |
Maximum number of subscriptions on each subscription connection. |
connectionTimeout |
10000 |
Connection timeout interval, in milliseconds. |
idleConnectionTimeout |
10000 |
Maximum time (in milliseconds) for reclaiming idle connections. |
pingConnectionInterval |
30000 |
Heartbeat for detecting available connections, in milliseconds. Recommended: 3000 ms. |
timeout |
3000 |
Timeout interval for waiting for a response, in milliseconds. |
retryAttempts |
3 |
Maximum number of retries upon send failures. |
retryInterval |
1500 |
Retry interval, in milliseconds. Recommended: 200 ms. |
clientName |
null |
Client name. |
Parameter |
Default Value |
Description |
---|---|---|
nodeAddress |
- |
Connection addresses of cluster nodes. Each address uses the redis://ip:port format. Use commas (,) to separate connection addresses of different nodes. |
password |
null |
Password for logging in to the cluster. |
scanInterval |
1000 |
Interval for periodically checking the cluster node status, in milliseconds. |
readMode |
SLAVE |
Read mode. By default, read traffic is distributed to replica nodes. The value can be MASTER (recommended), SLAVE, or MASTER_SLAVE. Other values may cause access failures in failover scenarios. |
loadBalancer |
RoundRobinLoadBalancer |
Load balancing algorithm. This parameter is valid only when readMode is set to SLAVE or MASTER_SLAVE. Read traffic is distributed evenly. |
masterConnectionMinimumIdleSize |
32 |
Minimum number of connections to the master node of each shard. |
masterConnectionPoolSize |
64 |
Maximum number of connections to the master node of each shard. |
slaveConnectionMinimumIdleSize |
32 |
Minimum number of connections to each replica node of each shard. If readMode is set to MASTER, the value of this parameter is invalid. |
slaveConnectionPoolSize |
64 |
Maximum number of connections to each replica node of each shard. If readMode is set to MASTER, the value of this parameter is invalid. |
subscriptionMode |
SLAVE |
Subscription mode. By default, only replica nodes handle subscription. The value can be SLAVE or MASTER (recommended). |
subscriptionConnectionMinimumIdleSize |
1 |
Minimum number of connections to the target node for pub/sub. |
subscriptionConnectionPoolSize |
50 |
Maximum number of connections to the target node for pub/sub. |
subcriptionPerConnection |
5 |
Maximum number of subscriptions on each subscription connection. |
connectionTimeout |
10000 |
Connection timeout interval, in milliseconds. |
idleConnectionTimeout |
10000 |
Maximum time (in milliseconds) for reclaiming idle connections. |
pingConnectionInterval |
30000 |
Heartbeat for detecting available connections, in milliseconds. Recommended: 3000. |
timeout |
3000 |
Timeout interval for waiting for a response, in milliseconds. |
retryAttempts |
3 |
Maximum number of retries upon send failures. |
retryInterval |
1500 |
Retry interval, in milliseconds. Recommended: 200. |
clientName |
null |
Client name. |
Suggestion for Configuring DCS Instances
- readMode
MASTER is the recommended value, that is, the master node bears all read and write traffic. This is to avoid data inconsistency caused by master/replica synchronization latency. If the value is SLAVE, all read requests will trigger errors when replicas are faulty. If the value is MASTER_SLAVE, some read requests will trigger errors. Read errors last for the period specified by failedSlaveCheckInterval (180s by default) until the faulty nodes are removed from the available node list.
If read traffic and write traffic need to be separated, you can use read/write splitting DCS instances. Proxy nodes are deployed in the middle to distribute read and write traffic. When a replica node is faulty, traffic is automatically switched to the master node. The switchover does not interrupt service applications, and the fault detection time window is far shorter than Redisson's window.
- subscriptionMode
Similar to readMode, MASTER is the recommended value.
- Connection pool configuration
The following calculation is applicable only to common service scenarios. You can customize it based on your service requirements.
There is no standard connection pool size. You can configure one based on your service traffic. The following formulas are for reference:
- Minimum number of connections = (QPS of a single node accessing Redis)/(1000 ms/Average time spent on a single command)
- Maximum number of connections = (QPS of a single node accessing Redis)/(1000 ms/Average time spent on a single command) x 150%
For example, if the QPS of a service application is about 10,000, each request needs to access Redis 10 times (that is, 100,000 accesses to Redis every second), and the service application has 10 hosts, the calculation is as follows:
QPS of a single node accessing Redis = 100,000/10 = 10,000
Average time spent on a single command = 20 ms (Redis takes 5 ms to 10 ms to process a single command under normal conditions. If network jitter occurs, it takes 15 ms to 20 ms.)
Minimum number of connections = 10,000/(1000 ms/20 ms) = 200
Maximum number of connections = 10,000/(1000 ms/20 ms) × 150% = 300
- Retry configuration
Redisson supports retries. You can set the following parameters based on service requirements. Generally, configure three retries, and set the retry interval to about 200 ms.
- retryAttempts: number of retry times
- retryInterval: retry interval
In Redisson, some APIs are implemented through LUA, and the performance is low. You are advised to use Jedis instead of Redisson.