# Distributed Transaction
__*Distributed Transaction*__
__*Lock*__
__*Default*__
__*Write*__
__*start timestamp*__
__*commit timestamp*__
__*StartTS*__
__*start_ts*__
__*Committs*__
__*commit_st*__

![left](./student-guide/diagram/PercolatorTiKV01_en_001_20220511.png)

------------------------------------------------------------
After learning about RocksDB, which is closely related to TiKV's database persistence, let's take a look at another feature implement of TiKV, which is distributed transaction.

If we want to manipulate multiple rows of data at the same time, and these data fall on different Region (and possibly even on different TiKV), we need distributed transaction to ensure consistency of operations.

The problem to be solved by distributed transaction is, < 1，Tom > for example, we need to change to < 1，Jack >, < 2，Andy > to < 2, Candy > (< 员工编号，员工姓名 > key - value) at the same time, < 1，Tom > and < 2，Andy > It is stored in a different region or even TiKV, so when the operation is finished, it may be < 1，Tom > changed to < 1，Jack > success, < 2，Andy > < 2, Candy > failure, or vice versa. This breaks the atomic nature of the transaction. Let's take a look at how TiDB solves this problem.

TiDB refers to Google's Percolator and optimizes 2PC (2-phase commit) to provide distributed transaction support. Let's take a look at the implement below:
1. The entire database needs to have a globally unique and monotonically incremented timestamp that identifies the start time of the transaction start timestamp (startTs/start_ts) and commit timestamp (commitTs/commit_st). The timestamp is provided by PD.
2. Percolator uses the three column families in the RocksDB instance, the column family (CF), to store data, transaction, and lock information. They are: Lock, Default, and Write, respectively. For example, if we write < 3, Frank >, we will record a lock (lock) with key = 3 in the Lock column (CF), put the values “3" and “Frank” in Default, and if committed successfully, it will be recorded in the Write column family (CF) and deleted Lock down information.
Next, we'll take a look at Percolator's specific process.