Distributed consensus algorithm combing

  Distributed Consensus Algorithm for mainstream programs: 2PC, 3PC, leader / follower, paxos

  Consistency There are two scenarios:

  1, multiple copies of the same data, in a modification, to ensure consistency more than
  2, more parts of a different variant of service data, to be consistent, to be successful are successful, have failed to failure

 

  Causes inconsistencies:

  1, leading to abnormal operation is unsuccessful
  2, network partition
  3, application failure

  Two-phase commit (2PC)

  The transaction will be submitted into the preparation and submission of two phases to achieve consistency algorithm, mainly used for transaction management, consistency distributed, two-phase commit a need to increase the role of coordinator in the middle of updating the initiator and participant. Specific steps are as follows:

  1, changes were initiated by the coordinator after the change request asking whether each participant can submit, wait for all participants to respond
  2, participants perform transactional operations to be submitted to the point of instruction (the process record undo, redo log)
  3, participants will be ready to submit a response to the results of the coordinator, coordinator and block waiting for the next instruction
  4, the coordinator of all participants accept the response, if there is a timeout response is not received, then when the rollback process
  5, instruction coordinator committed or rolled back released to all participants
  6 participants commit or rollback, the release of occupied locks and other resources, and to respond to
  7, the end of the

  Two-phase commit deficiencies:

  1, can block, participants will be blocked while waiting for the coordinator, and if preparation is completed, the coordinator of downtime, the participant has been blocked
  2, inconsistent, coordinator issues a commit or rollback command, participants downtime, receive messages, resulting in inconsistent (required manual processing)

  2PC implementation: XA specification, java achieve JTA field

  Three-phase commit (3PC): increased pre-submission stage and supermarkets mechanism to solve the problem 2PC arise, specific steps:

  3PC the shortcomings: the failure of some participants in precommit, coordinator downtime, waiting for the timeout after, precommit successful participants will be presented, resulting in inconsistent data, and timeout is hard to control, so 3PC no industrial implementation.

  leader / master programs, all update operations initiated by the leader, to achieve a simple majority of consistency scenes are distributed in this way, for example zookeeper.

  leader / master programs Disadvantages: leader load, leader single point of failure cluster is unavailable (new leader during the election)

  paxos algorithm

  Role: proposer (proposers, responsible for the proposal), acceptor (recipient responsible for voting), learner (learning, does not participate in the voting), a node can be both proposer may be acceptor

  Proposal constitutes: Proposal number (globally unique increment, reflect the proposals in the order), the updated value

  Writing process:

  1, the preparation phase (voting phase, the proponent of a proposal to take the recipient, if the recipient agreed to the proposal, a response is made promise, and promise not to accept (accept) a lower number than the current proposal of the proposal,
proposers received a majority of the response, the next stage, or re proposal
  2, to accept the change phase (commit phase), proposers accept the message sent to the recipient, the recipient comparison accept the proposal number if the current proposal has promise than their number small responded NACK (bill of lading number their current promise of return), or to accept accpet.

  The same proposal, proposers 1, 2 proponent has been approved, the normal scenario of demo

  2 and 3 receivers proponent lost contact with the receiver 1 1 proposer lost contact, abnormal presentation scenes

  Reading process:

  1, the receiving node broadcasts client requests to retrieve the current value everyone,
  2, receives the majority of the same value, that value is returned, and if not then update the value of the local value of
  3, the same half of the values are not read failure

  此外还有一种常用的分布式一致性算法实现:RAFT,原理参考http://thesecretlivesofdata.com/raft/

  

Guess you like

Origin www.cnblogs.com/hhhshct/p/11567365.html