Distributed transaction concepts and solutions

First, the concept of distributed transactions

1.1 What is a transaction

A combination of a plurality of transaction is an atomic operation. In the transaction, if one operation fails, the remaining operations are no longer performed, and the operation was performed before also need to roll back.

1.2 Characteristics affairs

There transactional atomicity, consistency, isolation, durability four characteristics, take the first letter of the English name, referred to as the ACID properties.

1.原子性(atomicity):一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
2.一致性(consistency):事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
3.隔离性(isolation):一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
4.持久性(durability):持久性也称永久性,指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

1.3 What is Distributed Transaction

Distributed transaction, the transaction includes the name suggests is a distributed operating system, different nodes, can be understood as the same concept will expand the database transaction to transaction of more than one library, the purpose is to ensure the consistency of data in a distributed system .

Popular terms, is a major operation by the composition of different small operation, these small operations distributed across different servers, and belong to different applications, distributed transactions need to ensure that these small operations either all succeed, or all fail. In essence, a distributed transaction is to ensure the consistency of data from different databases.

Distributed transaction processing is the key: there must be a way to know where all the action any transaction made, the decision to commit or roll back the transaction must yield consistent results: all committed or rolled back.

Second, the Distributed Transaction Solutions

2.1 two-phase commit (2PC)

Two-phase commit (Two-phase Commit, 2PC), to coordinate the participants' behavior by introducing Coordinator (Coordinator), the participants and the final decision whether to actually execute the transaction.

working process:

* 准备阶段:
    协调者询问参与者事务是否执行成功,参与者发回事务执行结果。        
* 提交阶段:
    如果事务在每个参与者上都执行成功,事务协调者发送通知让参与者提交事务;否则,协调者发送通知让参与者回滚事务。

Problems:

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

2.2 TCC program

TCC program is an improvement over the two-phase commit, the core idea is: for each operation, should a registration confirmation and the compensation corresponding (undo) action. It is divided into three stages:

* Try 阶段主要是对业务系统做检测及资源预留
* Confirm 阶段主要是对业务系统做确认提交,Try阶段执行成功并开始执行 Confirm阶段时,默认 Confirm阶段是不会出错的。即:只要Try成功,Confirm一定成功。
* Cancel 阶段主要是在业务执行错误,需要回滚的状态下执行的业务取消,预留资源释放。

Problems:

* 对应用的侵入性强:
    业务逻辑的每个分支都需要实现 try、confirm、cancel 三个操作,应用侵入性较强,改造成本高。
* 实现难度较大:
    需要按照网络状态、系统故障等不同的失败原因实现不同的回滚策略。为了满足一致性的要求,confirm 和 cancel 接口必须实现幂等。

2.3 Local message table

第一阶段:
    事务开始
        扣除 A 账号减少 100 元金额
        向事件表插入一条记录
    事务结束    
第二阶段:
    定时器定时查询事件表
    向消息队列写入消息  
第三阶段:
    事务开始
        向消息执行日志表增加一条记录(幂等性设计,避免消息重复执行)
        向 B 账号增加 100 元金额    
    事务结束    

Advantages and disadvantages:

优点:
    一种非常经典的实现,避免了分布式事务,实现了最终一致性。
缺点:
    消息表会耦合到业务系统中,如果没有封装好的解决方案,会有很多杂活需要处理。

2.4 MQ transactional messages

For local news table to solve the improvements made, there are some third-party MQ transactional messages, such as RocketMQ, the way they support the transaction message is similar to the use of two-stage submission, but some are available in the market mainstream MQ It does not support the transactional messages, such as RabbitMQ and Kafka are not supported.

Advantages and disadvantages:

优点:
    实现了最终一致性,不需要依赖本地数据库事务。
缺点:
    实现难度大,主流 MQ 不支持,RocketMQ 事务消息部分代码也未开源。      

Guess you like

Origin www.cnblogs.com/markLogZhu/p/12052741.html