Introduction to Redis Cluster
Redis Cluster is an assembly that provides data sharing among multiple Redis nodes.
Redis cluster does not support commands to process multiple keys, because this requires moving data between different nodes, thus failing to achieve the same performance as Redis, and may lead to unpredictability under high load conditions. Error.
Redis cluster provides a certain degree of availability through partitioning, and continues to process commands when a node is down or unreachable in the actual environment. Advantages of Redis cluster:
Automatically split data into different nodes.
Can continue to process commands even if some nodes in the entire cluster fail or are unreachable.
Data sharding of Redis cluster
Redis cluster does not use consistent hashing, but introduces the concept of hash slot.
Redis cluster There are 16384 hash slots. Each key is checked by CRC16 and modulo 16384 is used to determine which slot to place. Each node in the cluster is responsible for a part of the hash slot. For example, if the current cluster has 3 nodes, then:
Node A contains hash slots 0 to 5500.
Node B contains hash slots 5501 to 11000.
Node C contains hash slots 11001 to 16384.
This structure makes it easy to add or delete nodes. For example, if I want to add a new node D, I need to get some slots from nodes A, B, and C to D. If I want to remove node A , you need to move the slots in A to nodes B and C, and then remove the A node without any slots from the cluster. Since moving the hash slot from one node to another node does not stop the service, Therefore, no matter adding, deleting or changing the number of hash slots of a node, it will not cause the cluster to be unavailable.
Master-slave replication model of Redis cluster
In order to make the cluster still available when some nodes fail or most nodes cannot communicate, the cluster uses a master-slave replication model, and each node will have N-1 replicas.
In our example In a cluster with three nodes A, B, and C, without a replication model, if node B fails, the entire cluster will think that it lacks slots in the range of 5501-11000 and becomes unavailable.
However, if we add a slave node A1, B1, C1 to each node when the cluster is created (or after a period of time), then the entire cluster will consist of three master nodes and three slave nodes. In this way, after node B fails, The cluster will elect B1 as the new master node to continue serving, and the entire cluster will not be unavailable because the slot cannot be found.
However, when both B and B1 fail, the cluster will be unavailable.
Redis Consistency Guarantee
Redis does not guarantee strong consistency of data. This means that in practice, the cluster may lose write operations under certain conditions.
The first reason is because the cluster uses asynchronous replication. Write operation process:
The client writes a command to the master node B.
The master node B writes to the client Reply to the command status.
The master node copies the write operation to its slave nodes B1, B2 and B3.
The master node’s replication of the command occurs after the command reply is returned, because if each If each command request needs to wait for the replication operation to complete, the speed at which the master node processes command requests will be greatly reduced - we must make a trade-off between performance and consistency. Note: Redis Cluster may provide a synchronous write method in the future. Another situation where Redis cluster may lose commands is when the cluster has a network partition and a client is isolated from a small number of instances including at least one master node.
For example, assume that the cluster contains six nodes A, B, C, A1, B1, and C1, among which A, B, and C are the master nodes, and A1, B1, and C1 are the slaves of A, B, and C. node, and a client Z1. Assuming a network partition occurs in the cluster, the cluster may be divided into two parties. The majority party contains nodes A, C, A1, B1 and C1, and the small party contains node B and clients. End Z1.
Z1 can still write to master node B. If the network partition occurs for a short time, the cluster will continue to operate normally. If the partition time is long enough for most parties to elect B1 as New master, then the data written by Z1 into B will be lost.
The above is the detailed content of What is redis cluster. For more information, please follow other related articles on the PHP Chinese website!