# Writes Execution
__*Writes Execution*__
__*Transaction*__
__*KV*__
__*TiKV Client*__

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

------------------------------------------------------------
The write process will first read the data that needs to be modify into the TiDB Server cache. The memBuffer in the figure represents the TiDB Server cache. This read operation is consistent with the DML statement read flow described earlier. We can refer to the previous article: SQL Parse and Compile and Read Execution .

Let's start with the fact that the data that needs to be modify has been read into the TiDB Server cache. The main thread pool modules involved in writing in TiDB Server are: Transaction, KV, and TiKV Client. For an introduction to these three thread pools, please refer to Lesson 02: TiDB Server. The process is as follows:

1. The Transaction module begins to enter the second stage commit. The second stage commit will be introduced in Lesson 03: TiKV's "Distributed Transaction". Here we briefly understand it as a two-step operation, the first is the prewrite operation, that is, modify the data and lock, and the second is the commit operation, that is, commit the modify , Release the lock. Well, in these 2 operations, modify data, adding locks, releasing locks, and committing modify are all write operations, so let's introduce write operations later.
The Transaction first traverses the MemBuffer cache from the beginning, the keys in the cache are ordered, and then the modify required by the transaction are sent to the KV module.
It is important to note here that the commit operation in a two-stage commit, in addition to writing data, will previously request the PD to obtain the current TSO as the commit_ts of the current thing. Such a transaction has commit_ts committed (at the end).

2. The KV module is responsible for encapsulating transaction-related operations (write, commit, rollback, lock release, etc.) as write operations and send them to the TiKV Client module.

3. The encapsulated write operation is sent to the relevant TiKV via the TiKV Client module.
