The principle of redis cluster

Because the previous work involves the application of redis three masters and three slaves, so simply record the principle behind the cluster, because the current work does not require the construction of a cluster, so it will not be implemented.

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

The concepts that need to be clarified here are nodes and slots. There are three nodes a, b, and c in redis cluster A. In the cluster, a, b, and c do not all record the complete set of data, but they each record the complete set of data. In part, when the data in a, b, and c are combined, it is the sum of all data. Then what rules are a, b, c based on to divide the segments of recorded data? It is divided according to the slot.

A Redis cluster is composed of multiple slots, which can be passed when linking each node

cluster meet ip port

command to make a cluster link

The cluster allow option in the configuration file must be turned on before linking

cluster-enable yes

write picture description here

When a cluster is created, its internal structure will also change, mainly because each node no longer only records its own state, but also records the state of other nodes in the entire cluster.

Slot assignment: Nodes in the cluster use slots to determine which node records data. There are a total of 16384 slots in the cluster. When all the slots are allocated by the nodes, the cluster is officially online. When a record is stored, redis will use an algorithm to determine that the key of the record will hit. Which slot, and then determine which node the slot is currently replicated by, and finally let this node store the record.

=算法(key)
槽->结点

command to allocate slots

cluster addslots <slot> [slot ....]

When the slots are allocated, each node in the cluster will publish its own information, and the final result is that each node can know the slot data copied by other nodes in the cluster.
write picture description here

The failover in the cluster is also achieved through replication (ie master-slave). If the master node does not have a corresponding slave node, then when he goes offline, he will not be able to access the records corresponding to the slot he copied.

The slave node can be set by the following command

cluster replicate <node_id>

When a master node goes offline, other master nodes in the cluster will elect a slave node to replace the original master node from the slave nodes of the offline master node by voting, and change other slave nodes slave nodes The primary node is the new primary node.

The election process here is very similar to the sentinel election. In fact, the election algorithm is the same as the sentinel mode election algorithm, except that other master servers in the cluster are sentinels and can decide which slave server can inherit the host.

After that, the restoration of the original host is the same as the sentinel mode, which becomes a slave server to copy the contents of the new master server.

After understanding replication, sentinel, and clustering, we can draw such a conclusion. For multi-node redis,
if you only need to do hot backup or read-write separation, you can use master-slave
. If you need to do failover, you need to use Sentinel mode
If the amount of data is too large and you want to reduce the pressure on a certain server, then you need to use the cluster mode
. Above, the learning of redis comes to an end, recommend two good books
"redis combat": mainly introduction and development
"redis design and implementation" : Mainly to deeply understand the working principle of redis, it is worth reading

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326030088&siteId=291194637