In the scenario of high concurrency of big data, Sentinel has some problems. All write requests fall on the Master node, and the Master node has only one instance. There are bottlenecks in storage capacity, CPU, memory and IO. At this time, Redis cluster scheme emerges.

1. How to fragment data?

The Redis Cluster uses virtual slot partitions. There are 16,384 hash slots in a Cluster. The Redis Cluster automatically distributes these slots evenly across Cluster instances. For example, if there are N instances in the cluster, the number of slots on each instance is 16384/N.

The Redis Cluster hashes the key to get an integer value, and then modulates 16384 to get the slot.

2. Capacity expansion mechanism

As shown in the figure, there are three primary nodes: 6379, 6380, and 6381. 6382 is the secondary node of 6379, 6383 is the secondary node of 6380, and 6384 is the secondary node of 6381.

Step 1: Specify the slot migration plan for the new node, that is, migrate the slots of the nodes to the new node. In addition, the migration plan ensures that each node is responsible for a similar number of slots to ensure data uniformity among nodes. After the slot migration plan is determined, data in the slots is migrated from the source node to the destination node one by one.


As shown in the figure above, 6379 plans to migrate its slot 4097-5460 to the new node. 6385, 6380 plans to migrate its slot 9558-10921 to the new node. 6385, 6381 plans to migrate its slot 15019-16383 to the new node 6385.

Step 2: Migrate data Data is migrated one slot at a time. The migration process for each slot is described as follows:

① Run the cluster setslot {slot} importing {sourceNodeId} command on the target node to prepare to import slot data.

② Run the cluster setslot {slot} migrating {targetNodeId} command on the source node to migrate data from the slot.

③ Run cluster getKeysinslot {slot} {count} to obtain the keys of count slots {slot} on the source node.

④ Run migrate {targetIp} {targetPort} key 0 {timeout} on the source node to migrate the specified keys

Note: After Redis3.2.8, use pipeline transfer

⑤ Repeat Step 3 and step 4 until all key data in the slot is migrated to the target node.

⑥ Send the cluster setslot {slot} node {targetNodeId} command to all primary nodes in the cluster to inform them of slot allocation to the target node.

3. Capacity reduction mechanism

There are three cases of shrinkage:

① Offline migration trough

② Forget node

③ Shut down the node

Slot migration is the same as expansion

4.MOVED

When a client issues an instruction to the wrong node, the node realizes that the instruction’s key slot is not its own, and it sends a MOVED instruction to the client with the address of the node for the target operation, telling the client to connect to the node for the data.

After the client receives the MOVED command, it must immediately correct the local slot mapping table. All subsequent keys will use the new slot mapping table.

Case study:

First compute the hash value, then take the module 16384 to get the slot

Calculate the hash value command:

127.0.0.1:6379 > cluster keyslot"hello world"
Copy the code

hit

Not hit

The MOVED command contains two pieces of information, the location of the slot and the address of the destination node.

5.Asking

During cluster scaling (expansion/reduction), when we visit the source node and find that the key has been migrated to the target node, we will reply ask to turn to the exception. After receiving the exception, we first execute the Asking command, and then send the command to the target node again, and then the result will be returned.

If we execute A get key command, this key was originally in node A and now migrated to node B, then it will return to us the ASK turn exception. When we receive the ASK turn exception, we need to execute an Asking command to the target node, and then send the GET command.

Similarities between ASK and MOVED: Both are redirects. Differences between ASK and MOVED:

  • MOVED: The slot has been determined
  • Asking: The slot is in the migration process, and the key may be in the source node or the target node

6. Detect slot migration

If a slot in the cluster is being migrated or has been migrated, how can the client feel the slot change?

The client saved the mapping table between slots and nodes. When the client receives the moved command, it will refresh the slot mapping table to obtain the latest mapping. When an ASK turn exception is received, the slot mapping table is not refreshed because it is a temporary correction.

7. Fault tolerance

A Redis Cluster can set several secondary nodes for each primary node. If a single primary node fails, the Cluster automatically promotes one of the secondary nodes to the primary node. If a master node has no slave nodes, the cluster will be completely unavailable when it fails. However, Redis also provides a parameter cluster-require-full-coverage that allows some nodes to fail and other nodes to continue to provide external access.