# Distributed Transaction

![`smart`](PercolatorTiKV01_aa_002_20220511.png)

-----------------------------------------------------------------------
Distributed transactions are used in distributed databases, such as TiDB, to ensure the consistency and atomicity of operations that span multiple nodes or regions. This is important because, in a distributed system, it is possible for operations on different nodes to be executed concurrently, which can lead to inconsistencies and conflicts.

To support distributed transactions, TiDB uses a modified version of Google's Percolator system, which is based on the two-phase commit (2PC) protocol. In this system, each node in the database cluster is assigned a globally unique and monotonically increasing timestamp by the PD (Placement Driver) component. This timestamp is used to identify the start and commit times of transactions, and it ensures that operations are executed in the correct order.

In TiDB, the Percolator system uses three column families in the RocksDB instance to store data, transaction, and lock information. These column families are called the default column family, the write column family, and the lock column family. For example, when a write operation is performed on a key-value pair, the lock information for the key is recorded in the lock column family, and the value is stored in the default column family. If the transaction is successfully committed, the locked information is deleted, and the updated value is recorded in the default column family.

Overall, the distributed transaction support in TiDB allows for consistent and atomic operations across multiple nodes or regions, ensuring the integrity and reliability of the data in the database.
