# Execute and Write


![left](./student-guide/diagram/05-Write-execution-01-01.png)

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

Let's start with the introduction of data that needs to be modify has been read into the TiDB Server cache. The thread pool modules involved in writing in TiDB Server are mainly 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:
  - The transaction module starts to enter the two-stage commit. The two-stage commit will be introduced in Lesson 03: TiKV's “Distributed Transaction”. Here we briefly understand it as a 2-step operation, the first is the prewrite operation, that is, modify the data and add the lock, and the second is the commit operation, that is, commit Modify, release the lock. Then, in these two operations, modify data, adding locks, releasing locks, and committing modify are all write operations, so we will introduce write operations later.
  The Transaction first traverses the MemBuffer cache from scratch. The keys in the cache are in order, and then sends the modify that the transaction needs to make to the KV module.

Note that the commit operation in a two-stage commit, in addition to writing data, previously requests the PD to get the current TSO as the commit_ts of the current thing. Such a transaction has commit_ts that are committed (at the end).
  - 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.
  - The encapsulated write operation is sent to the relevant TiKV through the TiKV Client module.
