The principle Redis Cluster Cluster

A, Redis Cluster Architecture

Redis Redis Cluster was launched in version 3.0 distributed solutions. A plurality Redis Redis the Cluster nodes. Disjoint data between different nodes, each node corresponding to a plurality of data pieces. The node portion in backup node, by way of standby replication to ensure data consistency. A master node may have a plurality of slave nodes, the master node provides read and write services, providing a read from the service node.

1.1 internal communication mechanism

Redis Cluster nodes between the interactive information through the Cluster Bus Redis. Redis Cluster configuration information of each node in the cluster record: The version number Epoch, the cluster state data such State.

  Each node Redis Cluster are preserved cluster structure Node perspective. It describes how data is fragmented, in master and slave node, and the cluster configuration consistency of information as the version number by Epoch, while controlling the data migration process and failover.

1.2 Redis Cluster to the center
 in Redis Cluster, each master node Slave principle has one or more nodes. All Master cluster nodes can read and write data, misplaced priorities. Each master node internal communication, asynchronous replication from the inter-node protocol Goossip. However, asynchronous replication can lead to data loss under certain specific circumstances. So, Redis Cluster does not guarantee strong data consistency.

Redis Cluster design of the core idea: split the data, to the center.

Two, Redis Cluster data piece

2.1 Data fragmentation rules

In a distributed cluster, how to ensure that the same request falls on the same machine, and the back of the machine can be as clusters of sharing request, require the use of a reasonable strategy, there are two traditional methods:
1, Hash Algorithm: a fixed number of nodes when a node goes down, the reconstruction buffer.
2, consistent hashing algorithm: when a node goes down, only the data in the affected node. Pressure will be pressed into the database.
  When used Redis Cluster Algorithm hash slot partially affected by the number of nodes and fixed nodes can be configured to map to avoid modulo inflexibility and consistent hashing.
  Redis Cluster all the data distributed in accordance with the algorithm to hash slot 16384 [0-16383] The above slots hash, the hash grooves distributed on each node, each node maintains its own hash groove.

2.2 client routes

When the hash slot Key Client access is not in the current node, Redis Cluster command returns moved, and inform the proper routing information. When moved Client receives commands, requests Redis again and updates its internal routing cache information.

  When Redis Cluster redistributed in the data, Redis Cluster use the command to ask redirection. Because the data redistribution, the data may be a hash slots exist in both old and new nodes. So ask only redirects and will not update the routing information.

2.3 Migration data pieces

When there is a new master node joins the cluster, the node is removed from the cluster or because the uneven distribution of the data needed to re-distribution of data, it is necessary to migrate the data slice. Data migration is divided into three steps:
1, the status change command transmitted to the target node, corresponding to the hash of the slot status is set to the target node importing.
2, a state change command to the transmission source node, the source node corresponding to the hash slot status to migrating.
3, for all the hash key groove on the source node, the source node transmits a command to migrate, to inform the source node key corresponding to the destination node.
  When the state is set to the source node migrating. At this time differ under a normal state, and the service provided by the source node:
. 1, if the Client access key has not been moved out of the normal processing of the key;
2, if the key has been moved out or the key does not exist, it will return the ASK Client, allowed to jump to a target information node processing;
  when the target node state to importing. It indicates that the corresponding slot is moved to the target node. Differ under normal circumstances and the target node:
1, the slot for all non ask jump operations, the target node does not operate, but through moved so that Client jump to the source node to perform. This ensures that the same key is always executed at the source node before migration. After migration target node is always executed, thus eliminating double the conflict.
2, the migration process, the newly added key will be executed at the destination node, the source node will not add key. Making migration limits have to be at the end of a certain time.

A single key migration process can be done by atomization migrate command. For the node from the source node and the destination node, the master is prepared by copying, additions and deletions to achieve data. When all key migration is complete, Client fragment provided by the destination node's setslot Redis Cluster command, a move to contain the slot. The setup process will Epoch increment, and the latest value of Cluster. Then by sensing each other, spread to other nodes in Cluster.

Three, Redis Cluster Failover

3.1 node failure judgment
  First, Redis Cluster information in each node there all nodes in the cluster. You can connected to each other by the determination node ping-pong between them. If more than half of the nodes to ping a node when there is no response, it considers the cluster node goes down.

3.2 slave elections
  when the primary cluster node is recognized as a fail state, it will initiate a campaign from the node, if there are multiple slave nodes, the new node data is more likely to initiate the campaign. Other cluster master node returns a response.

3.3 structural changes
  when the campaign received more than half the primary node consent, will be the new primary node from the node. This time will be the latest Epoch message broadcast by PONG, so that other nodes Redis Cluster cluster update information as soon as possible. When the original primary node recovers from adding downgraded to a node.

Four, Redis Cluster High Availability

4.1 The master node protection
  when a node in the cluster from any instance of downtime, Redis Cluster will be non-unique instance replica from migrating from an instance, become this node to other nodes.
  Each active node in the cluster so that at least one slave, having such high availability Cluster. Cluster just need to keep 2 * master + 1 nodes, it can be maintained when any of a node goes down, after a failover continued availability.

4.2 cluster fail condition
  Redis Cluster guarantee the basic characteristics available only when certain conditions are identified as fail:
1, a master node and all hang from all the nodes, the cluster fail to enter the state.
2, if more than half of the primary node cluster hang up, regardless of whether the node into the cluster fail state.
3, if any of the primary cluster node hang, and the current state of the master node does not fail to enter the node from the cluster.

This switched: https://yq.aliyun.com/articles/711825

Published 88 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/99441680