NoSQL Redis cluster mode achieves high availability

Table of Contents

1 Redis cluster mode

1.1 The role of clusters can be summarized into two points

1.2 Data sharding of Redis cluster

2 Build Redis cluster mode

2.1 Turn on the cluster function

2.2 Start redis node

2.3 Start the cluster

2.4 Test cluster

1 Redis cluster mode

Cluster, namely Redis Cluster, is a distributed storage solution introduced in Redis 3.0.

The cluster is composed of multiple groups of nodes (Node), and Redis data is distributed among these nodes. The nodes in the cluster are divided into master nodes and slave nodes: only the master node is responsible for read and write requests and maintenance of cluster information; the slave node only replicates the master node data and status information.

1.1 The role of clusters can be summarized into two points

(1) Data partitioning: Data partitioning (or data sharding) is the core function of the cluster. The cluster disperses data to multiple nodes. On the one hand, it breaks through the memory size limit of Redis single machine and greatly increases the storage capacity. On the other hand, each master node can provide external read services and write services, which greatly improves the responsiveness of the cluster. The problem of limited Redis stand-alone memory size is mentioned when introducing persistence and master-slave replication; for example, if the stand-alone memory is too large, the fork operations of bgsave and bgrewriteaof may cause the main process to be blocked, which may cause host switching in a master-slave environment. As a result, the slave node cannot provide services for a long time, and the replication buffer of the master node may overflow during the full replication phase.

(2) High availability: The cluster supports master-slave replication and automatic failover of the master node (similar to Sentinel); when any node fails, the cluster can still provide external services.

1.2 Data sharding of Redis cluster

Redis cluster introduces the concept of hash slots. Redis cluster has 16384 hash slots (numbered 0-16383). Each group of nodes in the cluster is responsible for a part of the hash slots. Each key passes the CRC16 check and takes the remainder of 16384 to determine which hash slot to place. Slot, use this value to find the node corresponding to the corresponding slot, and then automatically jump to the corresponding node for access operations.

#Take a cluster composed of 3 nodes as an example:

Node A contains hash slots 0 to 5460

Node B contains numbers 5461 to 10922

Hash slot Node C contains hash slots 10923 to 16383

#Master-slave replication model of Redis cluster

There are three nodes A, B, and C in the cluster. If node B fails, the entire cluster will be unavailable due to the lack of slots in the range 5461-10922. Add a slave node A1, B1, C1 to each node, and the entire cluster will consist of three Master nodes and three slave nodes. After node B fails, the cluster will elect the master node with bit B1 to continue serving. When both B and B1 fail, the cluster will be unavailable.

2 Build Redis cluster mode

A redis cluster generally requires 6 nodes, 3 masters and 3 slaves. For convenience, all nodes here are simulated on the same server: Distinguished by port numbers: 3 master node port numbers: 6001/6002/6003, corresponding slave node port numbers: 6004/6005/6006.

cd /usr/local/redis/
mkdir -p redis-cluster/redis600{1..6}

for i in {1..6}
do
cp /opt/redis-7.0.9/redis.conf /usr/local/redis/redis-cluster/redis600$i
cp /opt/redis-7.0.9/src/redis-cli /opt/redis-7.0.9/src/redis-server /usr/local/redis/redis-cluster/redis600$i
done

97485ed171a84a9e87118e964570f318.png

2.1 Turn on the cluster function

The configuration files of the other five folders are modified in the same way. Note that the six ports are all different.

cd /usr/local/redis/redis-cluster/redis6001
vim redis.conf
#bind 127.0.0.1 #87 line, comment out the bind item, monitor all network cards by default
protected-mode no #111 line, turn off protected mode
port 6001 #138 line, modify the redis listening port
daemonize yes #309 line, set as daemon process, start in the background
pidfile /usr/local/redis/log/redis_6001.pid #341 line, specify the PID file
logfile "/usr/local/redis/log/redis_6001.log" #354 line, specify the log file
dir ./ #504 line, specifies the directory where the persistent file is located
appendonly yes #1379 line, enable AOF
cluster-enabled yes #1576 line, uncomment, enable cluster function
cluster-config-file nodes-6001.conf #1584 line, uncomment, cluster name file settings
cluster-node-timeout 15000 #1590 line, uncomment the cluster timeout setting

605e6184723f44c981140d55f7636290.png

3f109b4c58b247c099ac200302db9fd7.png

dc28dae7d6f843b6bcb5d924e6e4ee25.png

c7d679fe431e4e9a95f219a10c738334.png

d26a9ceaa0694833984537e24eb25543.png

b98b43e4e4dc4004af4eda97783c036f.png

The configuration files of the other 5 folders are modified in the same way. Note that the 6 ports are all different

b0155f41e1ac4f378b23a9114d28e320.png

2.2 Start redis node

Enter the six folders respectively and execute the command: redis-server redis.conf to start the redis node

cd /usr/local/redis/redis-cluster/redis6001
redis-server redis.conf

for d in {1..6}
do
cd /usr/local/redis/redis-cluster/redis600$d
./redis-server redis.conf
done

ps -ef | grep redis

8ee077e749fa4240895d1c41084a5788.png

2.3 Start the cluster

redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1

#The six instances are divided into three groups, each group has one master and one slave. The former is the master node and the latter is the slave node. When interacting below, you need to enter yes before you can create it. –replicas 1 means there is 1 slave node per master node.

f433488e22df4f5d912ee631a3c1554d.png

58b447ea62424250b50dd90430f7f90d.png

2.4 Test Cluster

93ad07ab4c1245c4bf9f7a09fd7b2a89.png

127.0.0.1:6001> set name zhangsan
-> Redirected to slot [5798] located at 127.0.0.1:6003
OK

127.0.0.1:6001> cluster keyslot name #View the slot number of the name key

    redis-cli -p 6004 -c
127.0.0.1:6004> keys * #The corresponding slave node also has this data, but other nodes do not
1) "name"


redis-cli -p 6001 -c cluster nodes

07820d8fb9e04482a425536ce3a3ec83.png

80f416f02a5049179d547da5ed4f08a7.png

e75561e46a3c4b0bb205294d351780a0.png