In this paper, let’s learn about Redis Cluster Cluster and sharding, Cluster Cluster of three master three slave server, the most behind will also integrate with SpringBoot content.

What is a Cluster?

In the previous chapter, Sentinel was mainly used to solve the problem of automatic failover of Master and slave architectures, but it was still limited by the write and storage capacity of the Master node. The use of Cluster Cluster is to solve the problem of limited capacity of single Redis and distribute data to multiple machines according to certain rules. Cluster A Cluster is a group of independent computers that connect to each other through a network. The computers form a group and are managed in a single system mode.

The difference between distributed and cluster: Distributed is to divide a system into multiple small systems, each small system is only responsible for its own services. Clustering is a business that can be responsible for multiple systems.

For example: the user goes to the bank to deposit money, little sister Anna is the staff of windowsill 1, and her work includes opening an account, saving money, reporting loss and so on. Anna’s business is inefficient because of too much work. Therefore, all banks employ more people to take charge of different things and let customers make choices according to their needs. This is called distribution. Lao Wang’s job is to deposit money for customers, but there is only one window, which leads to a long queue. At this time, more Windows can be opened to serve customers at the same time, which is called cluster. In general, distribution and clustering are used together.

“Introduction to Redis Cluster Mode”

Cluster mode is introduced from Redis3.0. Cluster is a centrless structure, each node saves data and changes the Cluster state, and each node is connected to other nodes. The official website requires six nodes to ensure high availability, namely, three master nodes and three slave nodes, with strong scalability and better high availability. In the Cluster, each node communicates with each other. For example, Master1 communicates with Slave1, Slave2 and Slave3, using gossip protocol to exchange node metadata information. The data is distributed to each node.

Cluster Data Sharding and Virtual Hash Slot

Demand analysis

In Redis, the write and read capacity of the master node is limited, and a single machine cannot meet the actual demand, so the data needs to be scattered to each machine, similar to the sub-database sub-table.

As shown in the figure below, Redis can decide which section to store the record according to the defined rules, and then go to the section to retrieve the data according to the rules.

“Common Data Partitioning Algorithms”

Hash mode: Calculate the hash value of a selected Partitioning key, and then allocate storage areas based on the hash value.

Range sharding: Partitioning is performed by determining whether the partitioning key is in range. For example, if the partitioning key is 1-1000, data from 1-1000 will be stored in range 1.

Consistent Hash partitioning: Redis Cluster does not use a consistent Hash scheme. It uses Hash slots in “data shards” to store and read data.

“What is a Redis hash slot”

The Redis cluster will classify 16384 buckets in advance. When a key-value needs to be placed in the Redis cluster, the corresponding value can be obtained according to the CRC16(Key)mod algorithm, and then the key can be placed in the bucket.

Hash slot general flow

Assume that the number of primary nodes is 3, allocate the 16,384 slots of the Redis cluster to these three nodes according to ** “user-defined rules” **, and copy one slot for each primary node.

Node 1 Slots range from 0 to 5460

Slot number of node 2:5461-10922

Node 3 Slots range: 10923-16383

Note: Slave nodes do not have slots, only master nodes have slots

Store to find

CRC16 hash is performed on the key to store lookup to obtain a value, and modulo 16384 is taken to determine which node the value is in range

If CRC16(“test_key”)%16384=3000, determine the value stored on node 1

CRC16 algorithm is a checksum algorithm

The advantage of using hash slots is that you can easily add or remove nodes. When you need to add nodes, you only need to reduce the range of some hash slots of other nodes and give the remaining hash slots to new nodes. For example, storage area 3, originally 10923-16383, now set to 10923-13000, then the remaining 13001-16383 will be given to the new node. The same is true for deleting a node. Simply move the hash slot of the removed node to another node in the hash slot.

Cluster Cluster Environment Preparation

First of all, the old version of Redis needs to use Ruby language to build, the new version of 5+ directly use Redis- CLI, six nodes, three master and three slave, the master and slave nodes will be automatically assigned, not manually specified. If the primary node is faulty, the secondary node is replaced by the primary node.

The nodes are 6381, 6382, 6383, 6384, 6385, 6386. We do not know which node is master or slave because it is automatically assigned (remember to open the network security group).

If you have started the sentry process in the previous chapter, stop it and delete persistent files in /user/local/redis/data/, as well as log files in the log directory

Conf and edit redis1.conf. The configuration information is as follows. Redis2. conf and the following information are also as follows. Notice information (port port, logfile logfile, appendfilename, cluster-config-file, cluster-announce-port, cluster-announce-bus-port) needs to be modified.

Bind 0.0.0.0 port 6381 daemonize yes requirepass "123456" logfile "/ usr/local/redis/log/redis1 log" dbfilename "Xdclass1. RDB" dir "/usr/local/redis/data" appendonly yes appendfilename "appendonly1.aof" masterauth "123456" # Whether to start the cluster Cluster-enabled yes # A node file is generated to record cluster node information. The default value is Nodes.conf to prevent conflicts. Conf cluster-config-file nodes-6381.conf # Node connection timeout cluster-node-timeout 20000 # NODE IP address IP cluster-announce-ip 172.18.172.109 # cluster node mapping port cluster-announce-port 6381 # Cluster node bus port for communicating with each other, Common port + 10 thousand cluster-announce-bus-port 16381Copy the code
Bind 0.0.0.0 port 6386 daemonize yes requirepass "123456" logfile "/ usr/local/redis/log/redis6 log" dbfilename "xdclass6.rdb" dir "/usr/local/redis/data" appendonly yes appendfilename "appendonly6.aof" masterauth "123456" Cluster-enabled yes cluster-config-file nodes-6386.conf cluster-node-timeout 20000 cluster-announce- IP 172.18.172.109 cluster-announce-port 6386 cluster-announce-bus-port 16386Copy the code

With all the configuration done above, we can then practice Cluster.

Start six nodes

./redis-server .. /conf/cluster/redis1.conf ./redis-server .. /conf/cluster/redis2.conf ./redis-server .. /conf/cluster/redis3.conf ./redis-server .. /conf/cluster/redis4.conf ./redis-server .. /conf/cluster/redis5.conf ./redis-server .. /conf/cluster/redis6.confCopy the code

Add to a cluster. – Cluster indicates the information about all nodes in the cluster. – Cluster-replicas 1 Indicates the ratio of primary and secondary nodes

/redis-cli -a 123456 --cluster create 172.18.172.109:6381 172.18.172.109:6382 172.18.172.109:6383 172.18.172.109:6384 172.18.172.109 172.18.172.109:6385:6386 - cluster - replicas of 1Copy the code

Check the status information (just one of the nodes)

/redis-cli -a 123456 --cluster check 172.18.172.109:6381Copy the code

After the command is successfully executed, slots are automatically allocated

Cluster Read/write commands

Above we have set up the Redis Cluster, now we start to test the Cluster read and write

/redis-cli -c -a 123456 -p 6381 # cluster info # cluster nodesCopy the code

Test cluster command set/get, when using the set command to add a Key, it is found that the Key is automatically allocated to the hash slot after calculation, get will calculate the value, and get the data in the corresponding hash slot (automatic steering), all operations are the master node, secondary nodes only backup.

Process analysis:

(1) Start the application

(2) Join the cluster

(3) The secondary node requests the replication from the primary node (the same as that from the primary node)

“Integrating a Cluster with SpringBoot”

Add a configuration to the SpringBoot project’s application.yml (remember to comment out the sentinel configuration)

Cluster: # maximum number of named forwarding times max-redirects: 3 nodes: 8.129.113.233:6381,8.129. 113.233:6382,8.129. 113.233:6383,8.129. 113.233:6384,8.129. 113.233:6385,8.129. 113.233:6386Copy the code

Add the dependent

<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-pool2</artifactId>
 </dependency>
Copy the code

The cluster is on different networks, so the cluster can be accessed only by changing the public IP address of Ali Cloud. The same network will be used for deployment in the company

Conf -redis6.conf file modification

# external IP cluster-announce-ip 8.129.113.233 # external port cluster-announce-port # cluster bridge port cluster-announce-bus-porCopy the code

It can also be modified using dynamic commands

The config set cluster - announce - IP 8.129.113.233Copy the code

“Cluster Automatic Failover Combat”

If the cluster inside send fault how to do, then we will come to the actual operation again!

Process: Kill the master node first, and the slave node becomes the new master node

We can kill a primary node manually, and then we can look at the log, and we can see that we are trying to connect to the primary node, and then we can start to change a secondary node to the primary node.

The command is as follows:

/redis-cli -c -a 123456 -p 6381 # cluster info # Cluster nodesCopy the code

High availability architecture summary

Master/slave mode: Read/write separation and backup. A master node can have multiple slave nodes

Sentinel: Monitoring, automatic failover, when the Sentinel detects that the primary server is down, it selects one of the nodes to be the primary node

Cluster: In order to solve the problem of limited Redis capacity in a single machine, data is allocated to multiple machines according to certain rules, memory /QPS is not limited to single machine, improve the concurrency.

“Summary:” from the most basic master-slave architecture to Cluster, learn how Redis Cluster mode, configuration related Cluster configuration, how to partition, the last we use SpringBoot Cluster Cluster integration.