Distributed coherency protocol: 2PC and 3PC

Foreword
 

In the course of a distributed system architecture design, often repeated trade-off between data availability and consistency of the system, so he produced a series of consistent protocols and algorithms. Most notably the two-phase commit protocol (2PC), three-phase commit protocol (3PC) and Paxos algorithm.

In a distributed system, each machine node although can clearly know the result of the process of conducting its own affairs operation is success or failure, but could not get directly into the operating results of other distributed nodes. Thus, when the operation requires a transaction across multiple nodes of a distributed, in order to maintain the ACID properties of transactions, it is necessary to introduce a component called a "coordinator (Coordinator)" to the unified execution logic of all the distributed nodes, these are distributed nodes were scheduled to become "participant (participant)". Scheduling coordinator is responsible for the behavior of participants and those participants take the final decision whether to actually commit the transaction. Based on this idea, derived from the two-phase commit protocol (2PC), three-phase commit protocol (3PC).
 
 

Two-phase commit protocol (2PC)
 

With this agreement to complete a distributed transaction to coordinate all the participants easily, it decided to unify the transaction commits or is rolled back, which can effectively ensure the consistency of distributed data, and therefore submit a number of distribution agreements are widely used in the second stage type systems.

As the name suggests, 2PC transaction submission process is divided into two stages for processing. Follows

 
Stage one: the preparation phase

Coordinator asks each participant whether the transaction is successful, participants feedback the results of the transaction.

Here Insert Picture Description
 
Here Insert Picture Description
Phase II: commit phase
 
commit phase there are two possibilities

  • If the transaction is executed successfully on each participant, the transaction coordinator sends a notification so that participants commit the transaction;
  • Otherwise, as long as there is a failure, the coordinator sends a notification to all participants to roll back the transaction.

It should be noted that, in the preparation stage, the participants performed the transaction but not yet committed. Only after receiving the notification sent by the coordinator during the commit phase, only be committed or rolled back.

2PC is the core for each transaction after treatment are used to try to submit, and therefore can be submitted algorithm seen as a strong consistency of the second stage.
 
2PC analysis of the advantages and disadvantages of
 
the advantages

  • Simple principle
  • Facilitate the achievement

Shortcoming

  • 同步阻塞:所有事务参与者在等待其它参与者响应的时候都处于同步阻塞状态,无法进行其它操作,极大的限制了系统的性能。
  • 单点问题:协调者在 2PC 中起到非常大的作用,发生故障将会造成很大影响。特别是在阶段二发生故障,所有参与者会一直等待,无法完成其它操作。
  • 数据不一致:在阶段二,如果协调者只发送了部分 Commit 消息,此时网络发生异常,那么只有部分参与者接收到 Commit 消息,也就是说只有部分参与者提交了事务,使得系统数据不一致。
  • 太过保守:任意一个节点失败就会导致整个事务失败,没有完善的容错机制。

 
二阶段提交协议(2PC)
 
是2PC的改进版,其将二阶段提交协议的阶段一分成两个过程,形成了由CanCommit、PreCommit 和 do Commit 三个阶段组成的事务处理协议。

阶段一:CanCommit
 
协调者向参与者发送一个包含事务内容的CanCommit请求,参与者回馈 Yes 或者 No

阶段二:PreCommit
 
根据阶段一的反馈结果,有两种操作可能

  • 如果阶段一所有参与者都反馈了 Yes ,那么协调者给参与者发送 preCommit 请求,执行预提交。如果参与者成功执行了事务操作,那么就反馈 Ack,之后等待最终命令 :提交(commit)还是终止(abort)。
  • 只要有一个 No 反馈,或者等待超时,那么就会中断事务。协调者向所有参与者发出终止(abort)请求。

阶段三:doCommit
 

该阶段将进行真正的提交,也是两种可能

  • 协调者收到了所有参与者的 Ack 确认,那就全部正式提交
  • 否则,全部回滚。

Note that, once entered Phase III, there may be two faults.

  • Coordinator problems
  • Network between coordinator and participant failure.

In either case, eventually resulting in participants not received in time doCommit or abort the request from the coordinator, for such unusual circumstances, participants will be timed out after waiting to continue the transaction commits.

 
3PC the advantages and disadvantages of
 
advantages:

Compared to 2PC, the biggest advantage is to reduce the blocking 3PC range of participants, and will continue to reach an agreement after a single point of failure.

Disadvantages:

3PC while removing the obstruction also introduces new problems, that is, after the participants received preCommit message, if the network is partitioned, this time coordinator nodes and where the participants can not be normal network traffic, in this case next, the participant will still be submitted to the transaction, which is bound to data inconsistency.

Guess you like

Origin blog.csdn.net/u013568373/article/details/91489352