# Distributed transactions

* [Comparison](#comparison)
* [Two phase commit](#two-phase-commit)
  * [Success case flowchart](#success-case-flowchart)
  * [Failure case flowchart](#failure-case-flowchart)
  * [Cons](#cons)
* [Three phase commit](#three-phase-commit)
* [TCC](#tcc)
  * [Definition](#definition)
  * [Success case flowchart](#success-case-flowchart-1)
  * [Failure case flowchart](#failure-case-flowchart-1)
  * [Fix consistency flow](#fix-consistency-flow)
    * [Asynchronously fix inconsistency](#asynchronously-fix-inconsistency)
    * [Fix inconsistency on discovery](#fix-inconsistency-on-discovery)
* [Saga](#saga)
  * [Flowchart](#flowchart)
  * [Compensation](#compensation)
* [AT transaction](#at-transaction)
* [Delayed transactions](#delayed-transactions)
  * [Flowchart](#flowchart-1)
  * [Fix consistency](#fix-consistency)

## Comparison

* https://docs.google.com/spreadsheets/d/1Sw0T4R6-Bb3orF0abwkmiZRBbCioevH1jvyjnw7aqhs/edit?usp=sharing

## Two phase commit

### Success case flowchart

![Success case](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-38c8bad7e019d6eb31db4c31901a54b50785b442%2FdistributedTrans_twophase_success.png?alt=media)

### Failure case flowchart

![Failure cases](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-1aab72958f41ad8b584631b52325dd1073a1ce5e%2FdistributedTrans_twophase_failure.png?alt=media)

### Cons

1. The nodes are in blocked status during the execution.
2. The coordinator could be the single point of failure in the entire process.
3. If any single failure happens among participants, all participants need to rollback transaction. And this could be high performance cost.

## Three phase commit

* This introduces another phase before prepare/commit phase to guarantee that the "Cons 3" in two phase commit get resolved.
* However, this seldomly gets used in practice because the benefits are limited.

## TCC

### Definition

* Each of three phases corresponds to a local transaction.
  * Try: Prepare transaction but not commit. Insert data.
  * Confirm: Corresponds to "commit phase" in two phase commit.
  * Cancel: Corresponds to "rollback phase" in two phase commit.

### Success case flowchart

![Success case](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-d361607b935d546cc20bf05a59910ef7a9f7d6dd%2FdistributedTrans_tcc_success.png?alt=media)

### Failure case flowchart

* Typically it won't fail in Try Phase. And it will fail only in Confirm phase.

![Failure case](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-c22ad70d7016d55e972c1f0ab2d5c0c3e0a186e7%2FdistributedTrans_tcc_failure.png?alt=media)

### Fix consistency flow

* TCC is an eventual consistency model.

#### Asynchronously fix inconsistency

![Asynchronously fix](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-1dd1604ae5c6383f9f5667c5523e3f9cb4fc2a26%2FdistributedTrans_tcc_fixInconsistency_async.png?alt=media)

#### Fix inconsistency on discovery

![Fix on discovery](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-4f8596a2eef552844b2d389f03770e62ca945acd%2FdistributedTrans_tcc_fixInconsistency_ondiscovery.png?alt=media)

## Saga

### Flowchart

![Fix on discovery](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-bd813b50142947a859879b03535f8db68e88045a%2FdistributedTrans_saga_flowchart.png?alt=media)

### Compensation

* Compensation is more accurate than rollback in saga context because
  * Rollback applies in uncommitted transaction
  * Compensation applies in committed transaction
* In saga, compensation is a better fit because it reverse committed local transactions.

## AT transaction

* AT transaction applies when you operate on multiple databases concurrently.

## Delayed transactions

### Flowchart

* When sharding middlware begins transactions on sharded DB, it has two options:
  1. At "Begin", start transactions on all DB shards.
  2. When executing a certain SQL statement, decide which shards to start transactions

![Begin transactions on all shards](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-7f592eec937b32de852f4743e6a41f5821dd14a5%2FdistributedTrans_delayed_beginAll.png?alt=media)

![Begin transactions on targeted shards when executing specific SQL statements](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-7d60d0cadb17d461628a3d0cf34bbcc7ebc7ff0d%2FdistributedTrans_delayed_beginCertainStatements.png?alt=media)

### Fix consistency

1. Retry on failure
2. Human intervention
3. Asynchronously fix inconsistency
