- ACID theory based on Database
- Log records based on undo and redo
- undoLog implementation rollback, redo log to achieve commit scene exception recovery
- Traditional single architecture
- Low requirements for distributed transactions
- What happens to distributed system scenarios?
- Log record – monitoring alarm – manual intervention repair
- Problem traceability, such as: maintenance orders can be created, but failure to invoke maintenance costs causes the entire transaction to roll back
- Possible maintenance cost problems, such as excessive performance pressure, cause the call failure to roll back when requested.
- Possible maintenance cost is successful, but returns.
|Two stage submission|
- Based on XA protocol, relying on TM and RM interaction, the ability to rely on database.
- TMThere is a single point of failure, lock resources occupy a longer time.
- For multi data sources or distributed database design (XA is essentially a specification between TM and RM).
- Architecture for multiple data sources
- MycatXA protocol is also implemented. Some companies use this scheme for distributed transactions, but the application layer is not a microservice architecture.
- It is suitable for core transaction business scenarios with short concurrency and short processing time.
|Three stage submission|
- Based on TCC protocol
- Implementing transaction mechanism outside database to achieve final consistency.
- At the expense of application flexibility, you need to provide concrete implementations of Try, Confirm, and Cancel, and you need to be careful about idempotent operations
- Cross application, but the need to achieve TCC interface, invasion of existing systems larger, suitable for the new system.
- TCC is not a strong dependency on database properties.
- Reference implementation: https://github.com/liuyangming/ByteTCC/
|Reliable message mode|
- Big business is changed into small business. The inconsistency between small things is compensated by additional training tasks.
The idea was first put forward by Ebay: https://queue.acm.org/detail.cfm? Id=1394128
Can be divided into two models based on local events and external events.
- Business logic needs to ensure idempotency.
- Suitable for core module modification, or completely based on message-driven architecture, otherwise the existing system intrusion is greater
- In addition, if you need to roll back, the scenario of more than two practical operations is complex, so this scenario needs to follow the ultimate consistency principle and failures do not roll until compensation is successful.
- Depending on the messaging system or database that has transaction functions, such as RabbitMQ, Kafka, RocketMQ, etc.
- Based on local events:
- Based on external events:
| Reliable message variants|
- Packing the message queue functionality as a Rest service without relying on message queue communication masks the interface of the message queue
- Reduce the shortcomings of architecture and Application Intrusion Based on reliable message mode.
- Maximum effort notification type
- For example, Alipay’s callback mechanism can set the index time to retry, refer to Ali to achieve: https://zhuanlan.zhihu.com/p/26114119
- Downstream application polling
- For example, WeChat’s polling mechanism guarantees consistency from downstream applications.
- Based on workflow, principle: https://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf
- Define the process of sequential operation and rollback operation, and give transaction coordinator unified management.
- Some application frameworks implement this scheme, such as CQRS framework Axon framework: https://github.com/Axon Framework/Axon Framework, and Huawei service comb:httPs://github.com/apache/incubator-servicecomb-saga
- The application side defines the workflow and gives it to SAGA for management. Although this scheme is not hot, it has less intrusion to the application and conforms to the principle of layered design. Adding a composite layer to implement the process that requires distributed transactions separately is enough.
- The route to optimize the XA architecture is similar to that of XA, and the business invasion is small, adding annotations.
- GTSReference: https://zhuanlan.zhihu.com/p/32684212
- Imitation GTS implementation: https://github.com/wxbty/meepo
- Similar to GTS: https://github.com/codingapi/tx-lcn seems to be the most mature open source solution.
- It is applicable to Ali cloud program, and the dedicated line can also be accessed. The third party system can also access TCC.
|Summing up suggestions|
- If it is not necessary to introduce distributed transactions, each micro-service guarantees its own high availability and basically guarantees data consistency, except in extreme cases. –In fact, the microservice architecture BAT was in use ten years ago, and it’s the same without distributed transactions, because of the infrastructure, the availability of each microservice itselfRelatively high, so there is no need to introduce greater complexity.
- If necessary, the first step is to ensure data consistency for core services, such as transactions, using messaging, best effort notification, and polling schemes, which are all bookkeeping in nature and can be traced even if problems arise – this is usually done with the help of the capabilities of third-party payment systems.
- If only a small number of services require distributed transaction characteristics, you can use a reliable message-based solution locally, referring to https://github.com/vvsuperman/coolmq, which requires a lot of detail and is theoretically possible for every link.Current network anomalies require corresponding measures to ensure, such as: if the establishment of an exponential time retry mechanism, downstream service interfaces need to ensure idempotency, which is equivalent to the business itself responsible for maintaining consistency.
- If a large number of businesses require distributed transactions, services like DelayMq can also be introduced to decouple and use this service to provide callback services to concatenate the service chains (messages contain callback Urls, parameters), but downstream service interfaces need to ensure idempotency — the PaaS platform can provide classesSimilar services, reference: https://zhuanlan.zhihu.com/p/26114119. The scheme needs to be able to accept part of the code refactoring.
- If a large number of businesses require distributed transactions, you can introduce a framework similar to GTS that has less intrusion into the business to avoid updating the architecture and code, and add the necessary annotations to the code, such as: https://github.com/coding api/tx-lcn — an open source solutionIt is recommended to be cautious after testing, and this ability can also be studied to see if the PaaS platform can be achieved
- Data consistency is a system engineering. It is not enough to solve it at the level of transaction framework only. It also needs supporting normative measures such as request request ID, link tracing, interface idempotency, log output specification, Key log record specification and so on.Let PaaS take over, provide link service, monitor alarm service, etc.
- Improving infrastructure and reducing the impact of network problems are important prerequisites. PaaS can provide DelayMq-like services for network exceptions when the actual invocation is successful and returns
- Perfect application monitoring and alarm facilities, such as API, access times, failure times monitoring, timely alarm – PaaS can provide a useful real-time monitoring and alarm capabilities