This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Saga mode

Saga mode called long transaction solution, it is mainly described in the absence of a two-phase commit, the core idea is to split the long transaction of a business process for multiple local short transactions, each participant in the business process will be submitted to the real submitted to the local short transactions, when one of the participants failed to perform the transaction, The participants who have finished the work are compensated by compensation mechanism.

Saga is composed of a series of sub-transaction Ti, and each Ti has its corresponding compensation action Ci. The compensation operation acts on the data change result caused by canceling Ti. Compared with TCC, it lacks the reserved action of Try, and each Ti operation actually affects the database.

There are two ways Saga works.

  • T1, T2, T3… Ti means that all transactions execute normally
  • T1, T2… , Ti, C1… Cj (0

Saga offers the following two types of compensation recovery

  1. Backward recovery refers to the second working mode, in which if any of the sub-transactions fail, the previous executions are undone one by one
  2. Forward recovery, in which a failed transaction is retried without compensation, is suitable for scenarios where the transaction must execute successfully.

Recovery, whether backward or forward, can fail and, in the worst case, involve human intervention in the transaction.

Advantages and disadvantages of Saga

Compared with XA or TCC, it has the advantages of directly committing local transactions in one stage without lock waiting and high performance. In event-driven mode, short transactions can be carried out asynchronously, and the implementation of compensation mechanism is relatively simple.

Disadvantages: Saga does not provide atomicity and isolation support, which has a great impact. For example, after a user buys a product, the system gives a coupon. If the user has already used the coupon, the transaction will be rolled back if there is an exception.