Works 7.redis cluster model can say about it? In cluster mode, redis addressing the key is how? Distributed algorithms which are addressed? Consistency hash algorithm to understand it?

Author: Chinese Shi Shan

Interview questions

Works redis cluster model can say about it? In cluster mode, redis addressing the key is how? Distributed algorithms which are addressed? Consistency hash algorithm to understand it?

Interviewer psychological analysis

In previous years, Redis engage if several nodes, each node storing a portion of data, obtained by means of a number of middleware implemented with, say  codis, or  twemproxy, both. There are some redis middleware, you write redis middleware, middleware is responsible for redis redis your distributed data stored on multiple machines instance.

This year, redis constantly in development, have also been redis new version, now redis cluster model, can be done on multiple machines, redis deploy multiple instances, each instance storage part of the data, and each redis examples can be hung from the main redis example, automatically ensure that if the primary instance redis hung up, will automatically switch to the instance redis up.

Now the new version redis, we are using redis cluster, which is the native support of redis redis cluster mode, the interviewer will certainly come to you on redis cluster even a few guns. If you have not used redis cluster, normal, before a lot of people use the client codis like to support cluster, but at least you have to look at it redis cluster.

If you small amount of data, mainly carry high concurrency performance scene, such as your cache generally on several G, stand-alone is sufficient, it may be used replication, a master plurality of slaves, slave to a few requirements with you read throughput related, then set up a sentinel cluster themselves to ensure redis master-slave architecture for high availability.

redis cluster, mainly for mass data concurrency + + high availability scenarios. redis cluster supporting the N redis master node, each of the master node can mount a plurality of slave node. So that the entire expansion of the lateral redis can. If you want to support a larger amount of data cache, then the lateral expansion more master nodes, each master node will be able to store more of the data.

Face questions analysis

redis cluster Introduction

  • The automatic data slice, the data put on a portion of each master
  • Provides built-in support for high availability, when part of the master is unavailable, you can still continue to work

In redis Cluster architecture, each redis to release the two port numbers, such as 6379 is a, the other one is 1w plus port number, such as 16,379.

16379 is the port number used for communication between nodes, the cluster bus thing is, the cluster bus communication, used for fault detection, configuration updates the failover authorization. another cluster bus with a binary protocol gossip for efficient exchange of data between the protocol, a node, takes up less network bandwidth and processing time.

Internal communication mechanism between the nodes

The basic communication principle

Maintain the cluster metadata in two ways: centralized, Gossip protocol. Redis cluster communication between nodes using the gossip protocol.

It is a Centralized Cluster metadata on a node (the node information, fault, etc.) in several stores. A typical centralized meta data centrally stored, the data field is large  storm. It is a real-time distributed large data calculation engine, a configuration of the centralized storage of metadata, based on the underlying ZooKeeper (distributed coordination middleware) stores all the metadata maintained.

zookeeper-centralized-storage

redis maintain the cluster metadata is another way,  gossip protocol, all nodes are in possession of a piece of metadata, the different metadata node if the change occurred, it will continue to send metadata to other nodes, so that other nodes also yuan change data.

redis-gossip

Centralized advantage is that read and update metadata, timeliness is very good, there was once a metadata change, updates to the centralized storage immediately, other nodes time to read a good feeling; that is not good All the pressure to update the metadata of all concentrated in one place, may lead to metadata stored pressure.

gossip advantage that update metadata scattered, not concentrated in one place after another update request will hit update all nodes up and reduce the pressure; bad that there is a delay in updating the metadata may result in a cluster Some operations will be some lag.

  • Port 10000: Each node has a port for communication between nodes dedicated to, is the port number +10000 provide services themselves, such as 7001, the inter-node communications port is 17001. Each node intervals are transmitted to several other nodes  ping message, while several other nodes received  ping after returning  pong.

  • Information exchange: Information including increased fault information, and delete nodes, hash slot information, and so on.

gossip protocol

gossip protocol comprising a plurality of messages, comprising  ping, pong, meet, fail and the like.

  • meet: a sending node to meet the new node is added, so that the new node joins the cluster, the new node will then begin to communicate with other nodes.
redis-trib.rb add-node

In fact, the interior is to send a message to gossip meet new node, notice that node to join the cluster.

  • ping: each node to other nodes frequently sending of ping, which contains its own state in the cluster as well as the maintenance of their own metadata, metadata is exchanged by ping.
  • pong: Returns ping and meeet, contains its own status and other information, and also updates the information for the broadcast.
  • fail: a node to another node fail after the determination, transmits to other nodes fail to notify the other nodes that a node is down it.

ping-depth news

To carry the ping some of the metadata, if it is frequent, may increase the network burden.

Each node executes 10 times per second ping, each will select the five longest no other nodes in communication. Of course, if we find a node communication delays reached  cluster_node_timeout / 2, then send a ping immediately to avoid data exchange delay is too long, too long behind. For example, between two nodes 10 minutes there is no exchange of data, then the entire cluster in an inconsistent metadata serious situation, there will be problems. It  cluster_node_timeout can be adjusted if the adjustment comparatively large, it will reduce the frequency of ping.

Every ping, will bring its own node information, there is information 1/10 to bring other nodes send out the exchange. Comprising at least  3 one information of other nodes, comprising up  总节点数减 2 information one other nodes.

Addressing Distributed Algorithms

  • hash algorithm (a lot of caching reconstruction)
  • Consistency hash algorithm (automatic cache migration) + virtual node (automatic load balancing)
  • redis cluster algorithm of hash slot

hash algorithm

It is a key, a hash value is first calculated, and then modulo the number of nodes. Then play on a different master node. Once a certain master node goes down, all requests over, are based on the latest master nodes to get a remaining mold, attempts to fetch data. This causes most of the requests come, not to get all valid cache, resulting in a lot of traffic influx database.

hash

Consistency hash algorithm

Consistency hash algorithm hash value of the entire space into a virtual ring, the entire space organization in a clockwise direction, the next step will each master node (using the server's ip or host name) hash. This will determine the location of each node on the ring thereof hash.

To a key, the hash value is first calculated, and determines the position of the data on the ring, from the position of "walking" the ring in a clockwise direction, the first node encountered is a master key location.

In consistent hashing algorithms, if a node linked to the affected data is only the data between this node to one node (the first node encountered in traveling in the counterclockwise direction) before the ring space, no other Affected. Add a node also empathy.

燃鹅,一致性哈希算法在节点太少时,容易因为节点分布不均匀而造成缓存热点的问题。为了解决这种热点问题,一致性 hash 算法引入了虚拟节点机制,即对每一个节点计算多个 hash,每个计算结果位置都放置一个虚拟节点。这样就实现了数据的均匀分布,负载均衡。

consistent-hashing-algorithm

redis cluster 的 hash slot 算法

redis cluster 有固定的 16384 个 hash slot,对每个 key 计算 CRC16 值,然后对 16384 取模,可以获取 key 对应的 hash slot。

redis cluster 中每个 master 都会持有部分 slot,比如有 3 个 master,那么可能每个 master 持有 5000 多个 hash slot。hash slot 让 node 的增加和移除很简单,增加一个 master,就将其他 master 的 hash slot 移动部分过去,减少一个 master,就将它的 hash slot 移动到其他 master 上去。移动 hash slot 的成本是非常低的。客户端的 api,可以对指定的数据,让他们走同一个 hash slot,通过 hash tag 来实现。

任何一台机器宕机,另外两个节点,不影响的。因为 key 找的是 hash slot,不是机器。

hash-slot

redis cluster 的高可用与主备切换原理

redis cluster 的高可用的原理,几乎跟哨兵是类似的。

判断节点宕机

如果一个节点认为另外一个节点宕机,那么就是 pfail,主观宕机。如果多个节点都认为另外一个节点宕机了,那么就是 fail,客观宕机,跟哨兵的原理几乎一样,sdown,odown。

In the  cluster-node-timeout inside, a node has not returned  pong, then it is considered  pfail.

If a node considers a node  pfail , then will  gossip ping message ping to other nodes, if more than half of the nodes are considered  pfail , then it will become  fail.

Filtered from the node

Downtime of the master node, from all the slave node, the switch to select a master node.

Time to check each slave node and the master node disconnected, if exceeded  cluster-node-timeout * cluster-slave-validity-factor, it will not be eligible to switch to  master.

From node election

Each slave node, according to offset their own copy all the data on the master, to set an election time, offset the greater (more copying data) from the node, the more time the election front, priority election.

All master node start slave poll, to slave to conduct elections to vote, if most of the master node (N/2 + 1)are voted for from a node, then the election by, that can be switched from node to master.

Performs the switchover from the node, the switching node from the master node.

Compared with the Sentinel

The entire process compared with the Sentinel, it is very similar, so that, redis cluster of powerful, direct integration of replication and sentinel function.

Guess you like

Origin www.cnblogs.com/morganlin/p/11980441.html
Recommended