Redis Sentinel (Sentinel) mode Quick Start

More welcome attention to micro-channel public number: engineer Xiaohui whole dish. No reply to the public keyword, receive free learning materials.

When the primary server goes down, you need to manually switch from one server to the main server, which requires manual intervention, protracted and painstaking, but also cause some time service is unavailable. So more often, our priority Sentinel (sentinel) mode.

Redis Redis sentinel is a high availability implementation: fault detection, fault automatic transfer, distribution center, client notification. From the beginning of Redis 2.6 version provided, but at the time this version of the model is unstable until after the 2.8 version of Redis, the Sentinel pattern was stabilized in a production environment, if you want to use Redis Sentinel mode, will try to use a later version of the 2.8 version of Redis.

While Sentinel has a separate executable file Redis-sentinel, but it is actually just a run in a special mode of Redis server, you can start by giving a general in the Redis server --sentinelsome design ideas option to start the Sentinel, the Sentinel and zookeeper very similar.

Redis Sentinel mode

Regular tasks of sentinel

There are three important sentinel mechanism of regular tasks.

  1. Each sentinel performed every 10 seconds for the master and slave info

effect:

  • We found slave node.
  • Verify that the master-slave relationship.
  1. Every 2 seconds for each channel master sentinel nodes through information exchange (pub / sub)

effect:

  • Master nodes communicate with each other and the information itself information that can sense sentinel newly added

hello channels interact, all sentinel subscribe to this channel every 2 seconds and release information to the channel: by __sentinel master node __

  1. Per second to ping each other sentinel and sentinel master, slave

effect:

  • Heartbeat

Subjective and objective offline offline

Subjective offline

Subjective off the assembly line: a single sentinel node to node communication failure Redis "prejudice."

This is a subjective offline. Because in a complex network environment, the sentinel of the master barrier, but if the master and the other is through the sentinel of it? So it is a "prejudice."

This is to rely on third Timing: ping per second to look around the sentinel and Redis. For the slave Redis, you can use this subjective offline, because he does not need to fail over; but for the master Redis, you must use objective offline.

Objective offline

Objective offline: All sentinel node node failed to master Redis "consensus" (more than a quorum is unified, quorum can be configured).

This is to rely on the timing of the second: a "consultation" (a sentinel between every two seconds, sentinel by sending SENTINEL is-master-down-by-addr command to another sentinel given server to ask whether the other party that offline.)

For master redis off the assembly line, we must reach a consensus before they can, because it involves failover alone is not enough to judge a sentinel

Leader election

When sentinel failover cluster requires elected Leader failover operation in a cluster. sentinel adopted Raft protocol algorithms Leader election between sentinel, but not entirely consistent with the steps described in the paper. Process failover cluster running sentinel completed, all sentinel will restore equality. Leader is the only role failover appear.

Election Process

  1. After a sentinel node master finds objective off the assembly line, the sentinel will first see if they have not voted, if they have voted for the other sentinel, the time-out double failover himself does not become a Leader . It is the equivalent of a Follower.
  2. If the sentinel has not voted, then it became Candidate.
  3. Raft agreement and described, become Candidate, sentinel need to do several things
    3.1 update failover status Start
    3.2 1 plus the current epoch, the equivalent of entering a new term, epoch is the Raft agreement term in the sentinel.
    3.3 update their timeout time to the current time plus a random period of time, a random number is the random time in milliseconds 1s.
    3.4 send is-master-down-by- addr command request to vote to other nodes. Command to bring your own epoch.
    3.5 to their vote, the sentinel, the ballot is his own master structure in the vote for leader and leader_epoch into the sentinel and its epoch.
  4. Other sentinel receive Candidate of is-master-down-by-addr command. If the sentinel current epoch and Candidate pass as his epoch, it shows that he has put his master's leader in structure and leader_epoch into other Candidate, equivalent to vote for the other Candidate. After voted to other sentinel, in the current epoch and that they can only become a Follower.
  5. Candidate will continue to count their votes until he found his identity became Leader of more than half of the votes and more than its configured quorum (quorum can refer to the "sentinel Design and Implementation redis"). sentinel increased quorum than Raft agreement, such a sentinel elected Leader Can it also depends on the quorum configuration.
  6. If in an election period, Candidate did not get more than half over and it is configured quorum of votes, his election would have failed.
  7. If within one epoch, not a Candidate to get more votes. So after waiting more than twice the timeout failover, Candidate increase epoch re-vote.
  8. If a Candidate received more than half of the votes and more than the quorum configure it, then it becomes the Leader.
  9. Raft with different protocols, Leader will not become Leader of the message to other sentinel. After waiting for the other sentinel Leader selected from the slave master, upon detecting the new master work, the objective will be to remove the identification offline, thereby eliminating the need to enter the failover process.

Failover process

  1. When multiple sentinel discovered and confirmed the master in question
  2. We will then elect a sentinel as a leader
  3. Re-elected as a slave master
  4. Inform the rest of the slave, who is the new master
  5. A master change notification from the client
  6. Finally, sentinel waits for the resurrection of the old master, and then become the new master slave

So, how to choose the "right" of slave node it?

  1. Select slave-priority (slave node priority, manually configured) highest slave node, if present, is returned, then the continued absence.
  2. Second, choose Copy offset the largest slave node (the most complete copy), if it returns, then there is no continuing
  3. Finally, choose the smallest run_id slave node (the earliest start node)

The basic principle of the client to achieve high availability

The client can not be perceived after a failover will not guarantee normal use. Therefore, the high availability implementation of the client steps as follows:

  1. Client gets the sentinel node set

Redis Sentinel mode

  1. To obtain the corresponding client information by the master node sentinel get-master-addr-by-name master-name the api

Redis Sentinel mode

  1. Client authentication currently acquired "master node" is a true master node, this purpose is to prevent a change of the master node during failover

Redis Sentinel mode

  1. Keep in touch with clients and sentinel node set, namely subscription sentinel node related channels, time access to relevant information on the master node

Redis Sentinel mode

As can be seen from the above model, the client only needs the Redis sentinel and sentinel communication during initialization the master node and the switching node information acquiring master, so in the design of the client needs to be considered as a set of configuration (sentinel node and node information related changes) discovery service.

Things need to explain

  • And the same network as the deployment of all nodes Redis sentinel on different physical machines
  • Redis sentinel sentinel node number is less than 3 and preferably should be an odd number. (More than the number of nodes can ensure high availability)
  • Redis sentinel node and the data node indistinguishable from normal data. Each sentinel node was in essence a Redis instance, and only the difference is Redis data node, its main role is to monitor the data node Redis
  • Client connection initialization sentinel node is set, the node is no longer a specific Redis, but the center is not arranged only sentinel agent.

More welcome attention to micro-channel public number: engineer Xiaohui whole dish. No reply to the public keyword, receive free learning materials.

Heck, if my card lost.  Micro-letter search for "whole food engineer Xiaohui," I can still find

Guess you like

Origin www.cnblogs.com/mseddl/p/11495405.html