An overview of the

Redis cluster is a distributed database solution provided by Redis. The cluster shares data through sharding and provides replication and failover functions

A node

A Redis cluster consists of multiple nodes. Initially, they are all independent of each other, and they are all in a cluster containing only themselves, but to form a truly usable cluster, the nodes must be linked together to form a cluster containing multiple nodes

Connect nodes using the CLUSTER MEET command

After the connection is successful, the peer node can be pulled to its own cluster. The connection page can be called handshake. Node states are stored in Redis through clusterNode objects

Two slots assigned

Redis cluster stores key-value pairs in the database by sharding: the whole database in the cluster is divided into 16384 slots, each key in the database belongs to one of the 16384 slots, and each node in the cluster can process 0 or up to 16384 slots

If all 16384 slots in the database have nodes processing, the cluster is online; otherwise, the cluster is offline

Using the CLUSTER ADDSLOTS command, you can assign slots to nodes

The slots information is recorded on the Slots and NumSlot attributes of the clusterNode object.

When setting an existing data, such as set MSG “hello world”, the cluster calculates which slot the MSG belongs to, and then determines which node the slot belongs to. If the current node is not the node where the slot belongs, a ‘Moved’ error is returned, directing the node to the node that is handling the slot

Iii Re-sharding

Redis resharding allows any number of slots assigned to one node to be assigned to another node, and the key-value pairs of the associated nodes to be moved to another node

Resharding can take place online, the cluster does not go offline, and both source and target nodes can continue to execute command requests

Four ASK error

An error may occur during migration when the source node is accessed and the data is actually migrated to the target node, so an ASK error is issued and the request is directed to the target node

Replication and failover

The nodes in the cluster are divided into master node and slave node. The master node has a complex processing slot, and the slave node is used to copy a master node. For example, 7000,7001,7002 are three master nodes, and 7004,7005 are set to the cluster and added as slave node.

The flags in clusterNode record the status of each node, which is divided into three states: online, suspected offline and offline. When more than half of the primary nodes responsible for processing the slot report that a primary node is suspected offline, The primary node is marked as offline.

6 Select a new primary node

When a primary node goes offline, the remaining primary nodes vote for the secondary node and choose a new primary node from the secondary node. When the number of votes obtained from a secondary node is greater than N/2+1, the secondary node is promoted to the primary node. It should be noted that deploying 4 master nodes is the same as deploying 5 master nodes when voting, because they both require 3 votes to choose the master node.