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
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
The configuration files of the other 5 folders are modified in the same way. Note that the 6 ports are all different
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
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.
2.4 Test Cluster
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