In the previous article, we talked about sentinels’ inability to solve the problem of single-node concurrency and physical memory and disk outage, so let’s start with Redis clustering.

introduce

Redis started to support Cluster mode in 3.0. At present, Redis Cluster supports automatic node discovery (dynamically adding a Redis can be integrated into the Cluster), and supports slave-master election (after the master fails, the remaining slave nodes can be elected. New master) and fault tolerance (when the master goes down, a new master will come on top), support online sharding shard, reshard and other features.

Architecture diagram

details

1. All Redis nodes are connected to each other (pingpong mechanism -> optimized heartbeat mechanism) and use binary protocol internally to optimize transmission speed and bandwidth. 2. The fail of a node takes effect only when more than half of the nodes in the cluster fail. Therefore, the number of nodes in the cluster should be odd. 3. The client is directly connected to the Redis node, without the intermediate proxy layer. The client does not need to connect to all nodes of the cluster, but to connect to any available node in the cluster. 4. Redis-cluster maps all physical nodes to solt [0-16383]. The cluster is responsible for maintaining node <-> solt <-> valueCopy the code

A Node is a master Node that provides external services. It is also called a physical Node

Set up process

Judge whether one is the nodes in the cluster is available, used the master node in the cluster is the election process, if more than half of the node that hang up the current node, then the current node is to hang out, so set up advice when redis cluster node number is an odd number of best, need at least three main building cluster nodes, three from the node, will take at least six nodes.

# yum install -y ruby rubygems -gem install redis-xxx.gemCopy the code

# 2. Create 7 directories on one machineCopy the code

[root@localhost ~]# cp redis-4.0.10/redis.conf 7000/ [root@localhost ~]# cp redis-4.0.10/redis.conf 7001/ [root@localhost ~]# cp redis-4.0.10/redis.conf 7002/ [root@localhost ~]# cp redis-4.0.10/redis.conf 7003/ [root@localhost ~]# cp redis-4.0.10/redis.conf 7004/ [root@localhost ~]# cp redis-4.0.10/redis.conf 7005/ [root@localhost ~]# cp redis-4.0.10/redis.conf 7006/Copy the code

# 4. Modify configuration files for different directories - port 6379..... -bind 0.0.0.0 // Enable remote connection -cluster-enabled yes // enable cluster mode -cluster-config -file nodes-port.conf // Cluster node configuration file - Cluster-node-timeout 5000 // Cluster node timeout - appendonly yes // Enable AOF persistence - daemonize yes // Enable daemonize. Start seven nodes with different directory configuration files - [root@localhost bin]#./redis-server /root/7000/redis.conf - [root@localhost bin]#./redis-server /root/7001/redis.conf - [root@localhost bin]# ./redis-server /root/7002/redis.conf - [root@localhost bin]# ./redis-server /root/7003/redis.conf - [root@localhost bin]# ./redis-server /root/7004/redis.conf - [root@localhost bin]# ./redis-server /root/7005/redis.conf - [root@localhost bin]# ./redis-server /root/7006/redis.confCopy the code

# 6. Check the process - [root @ localhost bin] # ps aux | grep redisCopy the code

Create the cluster

Copy the cluster operation script to the bin directory - [root@localhost bin]# cp /root/redis-4.0.10/src/redis-trib.rb. Rb create --replicas 1 192.168.202.205:7000 192.168.202.205:7001 192.168.202.205:7002 192.168.202.205 192.168.202.205:7003 192.168.202.205:7004:7005Copy the code

# 3. The following message is displayed indicating that the cluster is successfully createdCopy the code

Viewing Cluster Status

Check [any node in original cluster] [None] -./redis-trib.rb check 192.168.202.205:7000 # 2. Cluster node status Description - Primary node The primary node has Hash slots, and the Hash slots of the primary node are not crossed. The primary node cannot be deleted. A primary node can have multiple secondary nodes The secondary node can be deleted. The secondary node does not write data, but only synchronizes dataCopy the code

Adding a Primary Node

Add primary node add-node [new node] [any node in the original cluster] -./redis-trib.rb add-node 192.168.1.158:7006 192.168.1.158:7005 - Note: 1. The node must be started in cluster mode. 2. By default, the node is added as a master nodeCopy the code

Adding slave Nodes

Add slave node add-node --slave [newly added node] [Any node in the cluster] --./redis-trib.rb Add-node --slave 192.168.1.158:7006 192.168.1.158:7000 - Note: When the primary node is not specified, Redis will randomly add the current replica node # 2 to the primary node with fewer replica nodes. Add the master node to the specified master node. Add-node --slave --master-id Master node ID [newly added node] [Any node in the cluster] -./redis-trib.rb add-node --slave - master - id 3 c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 127.0.0.1:127.0.0.1 7006:7000Copy the code

Deleting replica nodes

Del - # 1. Delete the node node [any node in the cluster] [delete node id]. - / redis - trib. Rb del -node 127.0.0.1:7002 0 ca3f102ecf0c888fc7a7ce43a13e9be9f6d3dd1 - Note: 1. The nodes to be removed must be from nodes or nodes not assigned Hash SlotsCopy the code

Cluster Online Sharding

Reshard [any node in the cluster] [None] -./redis-trib.rb reshard 192.168.1.158:7000Copy the code

Spring connects to redis Cluster

Spring. Redis. Cluster nodes = 192.168.2.2:7000192168 2.2:8888192168 2.2:3564Copy the code

Of course you can write one to all the nodes in the cluster, but it’s possible that the one you write to just dies