Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Connecting to Redis on Redisson (Java)

Updated on 2024-11-26 GMT+08:00

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.

NOTE:
  • 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 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 number of the DCS Redis instance to be accessed. For details, see Distributed Cache Service User Guide > "Getting Started" > "Viewing Details of a DCS Instance".

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 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

Table 1 Config parameters

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.

Table 2 SingleServerConfig parameters (single-node, or Proxy Cluster)

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.

Table 3 MasterSlaveServersConfig parameters (master/standby)

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.

Table 4 ClusterServersConfig parameters (Redis Cluster)

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.

  • subscriptionMode

    Similar to readMode, MASTER is the recommended value.

  • Connection pool configuration
    NOTE:

    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) x 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
NOTE:

In Redisson, some APIs are implemented through LUA, and the performance is low. You are advised to use Jedis instead of Redisson.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback