Distributed Data Consistency/Distributed Transaction Summary

1. Brief introduction

1. Root cause of distributed transaction/distributed data consistency problem: the distribution of storage resources, writing multiple resources, writing cannot guarantee success 2.
Distributed transaction model:
 1) AP: Application, application program
 2) RM: Resource Manager, Resource Manager
 3) TM: Transation Manager, Transaction Manager
 Note: Transactions can be divided into global transactions and branch transactions

2. Common solutions

1. Two-phase commit
 1) Implementation ideas:
  a) Preparation phase: The transaction manager (TM) notifies the resource manager (RM) to prepare branch transactions, record transaction logs, and inform the transaction manager of the preparation results. That is, each resource manager executes all except the commit operation.
  b) Commit/rollback phase: In the first phase, all RMs return success, then TM sends commit commands to all RMs; if any RM returns failure, TM sends all RM sends rollback command
 2) Disadvantages
  a) Synchronous blocking: RM resources involved in the preparation phase will be locked until all commit or rollback
  b) Too conservative: any failure will roll back (the author thinks there is nothing wrong with this)
  c) TM single point of failure: TM failure in the second stage will cause the resources locked in the first stage to not be released
  d) data inconsistency caused by split brain: local network abnormalities, etc. In the second stage, some RM notifications may arrive, while others may not
2. Three-phase submission
 1) Implementation ideas
  a) Inquiry phase: TM asks all RMs whether it can be executed
  b) Preparation phase: If all RMs reply in the first phase that it can be executed, then perform all operations on all RMs except commit ( Same as the first phase of the two-phase commit), otherwise send a transaction interruption request to all RMs
  c) Commit/rollback phase: same as the second phase of the two-phase commit
 2) Advantages compared to the two-phase commit:
  a) Increase inquiry stage, you can find out that the operation cannot be performed as early as possible and abort the subsequent behavior
  b) Introduce a timeout mechanism. Once the timeout occurs, both TM and RM will continue to submit the transaction by default and consider the status as successful. Because the possibility of successful transaction submission is much greater than the possibility of failure, but this approach may lead to data inconsistency, so it is necessary to add some necessary logs or alarms for traceability when timeout occurs
3. TCC compensation plan (Try-Confirm-Cancel)
 1) Try: This stage is mainly for data verification or resource reservation
 2) Confirm: Confirm the actual execution tasks, and only operate the resources reserved in the Try stage
 3) Cancel: cancel the execution and release the resources reserved in the Try
 phase
The permanent solution
  uses the reliability mechanism class of message middleware to achieve data consistency delivery, that is, transaction messages and transaction review
5. Best effort notification type
  . If the downstream does not return a message confirmation, the upstream needs to continue to retry until Received a message acknowledgment or reached the maximum number of retries

3. Seata, a distributed transaction framework

1. Seata provides 4 transaction modes: AT, Sega, TCC (compensation scheme), XA (two phases)
2. AT mode
 1) Implementation ideas:
  a) Phase 1: Business data and rollback logs are recorded in the same Commit in a local transaction
, release local locks and connection resources, that is, reduce the lock granularity   b) The second stage: commit asynchronously . Global commit , indicating that all branch transactions have been submitted at this time, and the rollback log can be cleaned up through the asynchronous queue ; the rollback is reversely compensated through the rollback log of the first stage   Note: When using AT mode, RM must be supported Relational database for local transactions  2) Isolation   a) Write isolation: In the first stage, acquire local locks -> business operations -> acquire global locks -> local commits release local locks -> release global locks Note: a) Acquire   global   locks If the lock fails to retry multiple times, the transaction is rolled back and the local lock is released   b) The deadlock problem between the global lock and the local lock is solved by acquiring the global lock timeout   d) Read isolation: the default read is not committed, relying on eventual consistency 3, Saga model








 1) Implementation idea: long transaction solution, split a long transaction in a business process into multiple local short transactions, each participant in the business process submits a real submission to the local short transaction, when one of the participants If the execution of the transaction fails, the previous successful participants will be compensated through the compensation mechanism.
 2) Compensation recovery method
  a) Backward recovery: undo the previous execution results one by one
  b) Forward recovery: retry failed transactions
 3) Advantages and disadvantages
  c) Advantages: one-stage direct commit, high performance, Asynchronous execution
  d) Disadvantage: does not support atomicity and isolation
 3) Coordination mode:
  a) Event orchestration (distributed): Sega's decision-making and execution order logic is distributed among each participant of Sega, and they exchange events b
  ) command coordination (centralized): Concentrate Sega's decision-making and execution sequence logic in a Sega control class, which communicates with each service in a command/reply manner, telling them what to execute Operation
4. Seata file storage mode
 1) Stand-alone mode: by default, global transaction session information is persisted in local files, with high performance
 2) db storage mode: high-availability mode, global transaction session information is shared through db, and performance is relatively poor. Global transaction session information consists of global transactions, branch transactions, and global locks
3. Seata configuration
 1) registry.conf
  a) registry: configure the address of the registry
  b) config: configure the address of the Seata configuration file
 2) file.conf: configuration of Seata, Including: protocol, server, monitoring

Related recommendations:
1. Registration center selection comparison
2. Configuration center selection comparison
3. Gateway selection comparison
4. Remote call selection comparison
5. Distributed data consistency
6. Message queue selection comparison
7. Monitoring tool selection Contrast
8. Comparison of full link tracking model selection

Guess you like

Origin blog.csdn.net/qq_21033663/article/details/109543979