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="https://www.altitude.xin/typora/image-20211108171613912.png" 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

node

host

Main service port

Slave service port

remarks

node01

127.0.0.1

7010

7011

Physical machine

node02

127.0.0.1

7020

7021

Physical machine

node03

127.0.0.1

7030

7031

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  \
127.0.0.1:6390 127.0.0.1:6391  \
127.0.0.1:6392 127.0.0.1:6393  \
127.0.0.1:6394 127.0.0.1:6395   --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.

spring:
  redis:
    cluster:
      max-redirects: 3
      nodes:
        - 192.168.181.171:6390
        - 192.168.181.171:6391
        - 192.168.181.171:6392
        - 192.168.181.171:6393
        - 192.168.181.171:6394
        - 192.168.181.171:6395
2. Configuration class

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

@Configuration
public class RedisClusterConfig {    
    @Autowired
    private RedisProperties redisProperties;
    
    @Bean
    public RedisConnectionFactory lettuceConnectionFactory() {
        RedisProperties.Cluster cluster = redisProperties.getCluster();
        List<String> nodes = cluster.getNodes();
        RedisClusterConfiguration configuration = new RedisClusterConfiguration(nodes);
        configuration.setMaxRedirects(cluster.getMaxRedirects());
        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).

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

Original address

Topics: Redis