> For the complete documentation index, see [llms.txt](https://eric-zhang-seattle.gitbook.io/mess-around/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eric-zhang-seattle.gitbook.io/mess-around/traditional-db/distributedtransactions.md).

# 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](/files/XofUkh4HC8mX5Mrkatab)

### Failure case flowchart

![Failure cases](/files/NmUQkdHIm4dle8IWPWM7)

### 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](/files/mPxyimpR0CbZOStkg6Rl)

### Failure case flowchart

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

![Failure case](/files/MBxn8Yv6qwWymv4R4xgl)

### Fix consistency flow

* TCC is an eventual consistency model.

#### Asynchronously fix inconsistency

![Asynchronously fix](/files/k1ypDGYgnJQ3nP9t2sEW)

#### Fix inconsistency on discovery

![Fix on discovery](/files/3pQMuRYA5FFXOwVtTvvS)

## Saga

### Flowchart

![Fix on discovery](/files/70eGBP9teozeV2Lcalpc)

### 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](/files/0qR4QClNIwLVj1gmRnuh)

![Begin transactions on targeted shards when executing specific SQL statements](/files/qPT7cvnrUjtXgTRbVNpy)

### Fix consistency

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eric-zhang-seattle.gitbook.io/mess-around/traditional-db/distributedtransactions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
