This is the 4th day of my participation in the August More Text Challenge
Set up Redis Cluster to realize Redis Cluster storage and high availability. Why build a Redis Cluster?
Redis Cluster can realize Redis Cluster storage and high availability. QPS on a single master/slave replication machine cannot meet the increasing service requirements. If the memory of a single server cannot meet service data requirements, you need to distribute data to different servers in a cluster.
Redis Cluster partitioning scheme
Virtual slot partitioning is the partitioning scheme used by Redis Cluster. The default virtual slot ranges from 0 to 16383.
(Data is broken up to ensure even data distribution)
Data allocation steps:
1. Evenly allocate 16383 to slot nodes. 2. Hash each key according to CRC16 rules 3. Mod the hash value to 16383 and send the result to node 4. The node receives the data and verifies that it is in the range of the slot number that it manages and if it is in the range of the slot number that it manages, it saves the data to the data slot and then returns the execution result and if it is not in the range of the slot number that it manages, it will send back to the client moved redirection exception if the client receives the Moved redirection, It gets information about the target node, and the client gets information about the target node.Copy the code
Messages are shared between nodes in the Redis Cluster, and each node knows which node is responsible for which slot.
To expand or reduce the capacity of a cluster, migrate slots and data in slots. If the cluster is expanding or empting, when the client sends a command to the correct node, the slot and the data in the slot have been migrated to another node, it will return Ask. This is the Ask redirection mechanism.
Fault found
Redis Cluster uses ping/ Pong messages to discover faults.
Ping/Pong can not only transmit the corresponding message of the node and slot, but also transmit other states, such as: node master/slave status, node failure, etc.
1. Node 1 periodically sends a ping message to node 2. 2. If the ping message is successfully sent, it indicates that node 2 is running normally. If the ping message fails to be sent, the communication between node 1 and node 2 is abnormal. In the next scheduled task period, the ping message 4 is sent to node 2. If the last communication between node 1 and node 2 exceeds cluster-node-timeout, node 2 is in the PFAIL state, that is, node 2 is offline.Copy the code
Nodes in the cluster will communicate through ping/pong. If more than half of the primary nodes holding slots in the cluster mark a node with pFAIL status, the node will be offline objectively. The cluster broadcasts messages about the offline node and elects a secondary node to replace the primary node.
Node election
If a master node has no slave nodes, when it fails, the entire cluster becomes completely unavailable.
As the suspended master may have multiple slaves, there is a process in which multiple slaves compete to become master nodes. The process is as follows:
1. The slave finds that its master changes to FAIL. 2. The other nodes receive the message, and only the master responds, judges the validity of the requester, and sends the result 4. The slave that tries to vote collects the result returned by the master, and the number of masters that exceeds the number of master nodes /2+1 becomes the new master 5. Broadcast the Pong message to inform other cluster nodes.Copy the code