Redis cluster mode high availability practice

Posted by fI_Ux on Mon, 14 Feb 2022 10:40:56 +0100

1, Preface

This paper builds Redis cluster service.

<img src="" alt="image-20211108171613912" style="zoom:33%;" />

2, Single machine simulation

Single machine simulation refers to simulating operation on a single physical machine or virtual machine to maximize the restoration of the intermediate process of this scheme. It is suitable for use in the learning or development stage.

In order to simplify the operation, the Redis service makes the following conventions: data is not persistent to disk; The service instance runs in the previous process mode; The node's configuration file takes the default configuration file as the template; No password authentication.

(1) Cluster planning



Main service port

Slave service port





Physical machine




Physical machine




Physical machine

(2) Service configuration

1. Random master-slave initialization cluster

The initial configuration file of the node takes the default configuration file as the template. In order to quickly build the cluster environment, the master-slave relationship scheme of random allocation is adopted here.

/usr/local/redis/bin/redis-cli --cluster create  \  \  \   --cluster-replicas 1
2. Customize master-slave initialization cluster

Manually specify the master-slave relationship. In the production environment, manually specify the location of the master-slave service to ensure that the master-slave of the same group of instances are not distributed on the same physical host.

(3) Service management

When testing or learning, it is recommended to use the foreground process management service to simulate a single point of failure, view the log and observe the master-slave switching.

Under production conditions, it is recommended to use Supervisor management service, which is not only easy to manage, but also can realize automatic restart after abnormal termination of service. Three physical machines are used in the high availability scenario.

1. Redis instance
/usr/local/redis/bin/redis-server /usr/local/redis/conf/cluster/redis6390.conf --port 6390 --save '' --daemonize no
/usr/local/redis/bin/redis-server /usr/local/redis/conf/cluster/redis6391.conf --port 6391 --save '' --daemonize no
/usr/local/redis/bin/redis-server /usr/local/redis/conf/cluster/redis6392.conf --port 6392 --save '' --daemonize no
/usr/local/redis/bin/redis-server /usr/local/redis/conf/cluster/redis6393.conf --port 6393 --save '' --daemonize no
/usr/local/redis/bin/redis-server /usr/local/redis/conf/cluster/redis6394.conf --port 6394 --save '' --daemonize no
/usr/local/redis/bin/redis-server /usr/local/redis/conf/cluster/redis6395.conf --port 6395 --save '' --daemonize no

3, Client integration

The implementation of spring boot based integration is divided into two steps: one is to complete the integration based on the client; The second is to supplement new features according to the needs of production.

(1) Basic integration

The content of basic integration is to connect the highly available sentinel mode Redis service with Java client to meet the requirements of normal operation of single node fault service.

1. Global profile

The configuration information added to the global configuration file includes: the master parameter is the sentinel service name, which is the default value here; The nodes parameter is the sentinel service list (not the Redis instance service list); The database parameter is the database.

      max-redirects: 3
2. Configuration class

Integrated into the SpringBoot system, the core is to create a lettueconnectionfactory connection factory.

public class RedisClusterConfig {    
    private RedisProperties redisProperties;
    public RedisConnectionFactory lettuceConnectionFactory() {
        RedisProperties.Cluster cluster = redisProperties.getCluster();
        List<String> nodes = cluster.getNodes();
        RedisClusterConfiguration configuration = new RedisClusterConfiguration(nodes);
        return new LettuceConnectionFactory(configuration);

4, Read write separation

Basic integration is only the process of realizing highly available Redis services. Other configurations still need to be added in the production environment: modify the user-defined connection database serial number; Authorized connection; Connection pool configuration; Read write separation.

On the premise of high availability, the feature of read-write separation is derived, and the main database completes the write request; Complete the read request from the library (write is not allowed from the Library).

public LettuceClientConfigurationBuilderCustomizer lettuceClientCustomizer() {
    // Configure read write separation
    return builder -> builder.readFrom(ReadFrom.REPLICA);

Original address

Topics: Redis