# Writes Data

![`smart`](ExecuteandWrite01_aa_005_20220512.png)

-----------------------------------------------------------
The write process will first read the data to be modified into the cache of the TiDB server, where the MemBuffer in the figure represents the cache of the TiDB server.

Below we start to introduce from the data that needs to be modified has been read into the TiDB server cache, the thread pool modules that participate in writing in TiDB server are mainly: `Transaction`, KV and TiKV Client. The process is as follows:

1. The `Transaction` module begins to enter the two-phase commit, which we briefly understand as two steps: the first is the Prewrite operation, that is, to modify data and lock, and the second is the Commit operation, that is, to submit the modification and release the lock. So the modifications, locking, unlocking and submitting modifications in these two operations are all write operations. The `Transaction` will first traverse the MemBuffer cache, and the keys in the cache are sorted, and then the modifications that the transaction needs to make will be sent to the KV module. Here it needs to be noted that in the two-phase commit operation, in addition to writing data, it will apply to PD to obtain the current TSO as the commit_ts of the current transaction before that. In this way, a transaction has a commit_ts when it is committed (ended).

2. The KV module is responsible for encapsulating the transaction-related operations (writes, commits, rollbacks, locks, and lock releases, etc.) as write operations and sending them to the TiKV Client module.

3. The encapsulated write operations are sent to the relevant TiKV server through the TiKV Client module.


