As we know, Redis is divided into four architectures: single-machine, master-slave, sentry, and cluster. This article focuses on cluster architecture.

Here’s a redis-Cluster architecture diagram:



Steps for cluster construction:

Start a node: Start the node as a cluster.

Node handshake: Connects independent nodes to a network.

Slot assignment: 16,384 slots are allocated to the master node to achieve the effect of storing database key-value pairs in fragments.

Primary/secondary replication: Specifies the primary node for the secondary node.



As can be seen from the figure, the cluster contains multiple small clusters, and each small cluster is a master/slave architecture. When the master node fails, itsSlave nodes automatically fail over, upgrade the primary node; (A large cluster must contain at least three small clusters)



How do I determine which primary node to save data to in a cluster?

The figure above shows that in each small clusterThe master nodeThey’re all assigned a rangeslot, indicates that only data in this range can be saved, for example:set hello world, the Key value needs to be calculatedhelloIs used to determine which primary node to store in. Redis specifies that the total number of slots in the cluster must be16384;

In the figure above,set hello world.helloThe value of is 866. The slot number of the first primary node in the cluster ranges from 0 to 5461. Therefore, it is directly saved to the first primary node.



The setup process is shown below (four small clusters, one master and one slave, combined into one large cluster) :

Start by creating eight folders, each of which is an instance of Redis, with a redis.conf configuration file in each instance:



Redis.conf needs to be modified:

Basic configuration: port, pidFile, logfile, dir

Cluster configuration: Cluster-enabled, cluster-config-file, cluster-node-timeout, cluster-replica-validity-factor, cluster-migration-barrier, and CLUST Er – the require – full – coverage, luster – up – no – failover

Finally, remember:wqsave

Each folder now has its own redis.conf file:



Let’s start it upsixRedis instances:

Data cannot be written to any redis at this point because the redis instances are not yet associated or assignedslot:

Next, use a command to make each node know each other, assign master and slave nodes, and configure slots:

Start by remembering one command: redis-cli –cluster help



As can be seen from the figure, create cluster command:



There are six cluster-replicas 1 nodes, and the ratio is 3 to 3. If you write 2, the ratio is 2 to 4 (an error will be reported, requiring at least three small clusters). By default, the preceding commands evenly allocate slots. To connect to any redis, run Cluster Nodes:



When writing data, an error will be reported:

Because the slot value of K1 is not on the primary node 7000;



When connecting redis, add -c to the tail (cluster connection), save data successfully again, and automatically switch to 7002 node;



Start 7006 and 7007:



Add these two nodes to the cluster, primary node:



From the node:



Currently 7006 does not have slots, slots need to be allocated:



Slots to be allocated:

At this point, slot allocation is complete:

Note: When assigning slots, data will also be migrated.



Next, we will explain the cluster capacity reduction. First, we will relocate slots:

Delete point again :(delete the secondary node first and then the primary node, otherwise failover will occur)



During a failover, a node in a cluster is unavailable for a period of time, but other nodes are not affected. In sentinel mode, the entire cluster is unavailable during a failover.