# Transactions Best Practices on TiDB

__*Transactions best practices on TiDB*__

+ Keep transaction small
  + By default, a transaction is limited to 5000 SQL commands
  + By default, each key-value pair entry is no more than 6 MB
  + The total size of key-value pair entries in a transaction does not exceed 10 GB
  + It is recommended that the number of records per transaction be less than 200, and the data size of a single record is less than 100 KB
------------------------------------------------------------
Because distributed transactions require a 2-phase commit and the underlying Raft replication is performed, if the transaction is very large, the commit process will be very slow, so the next Raft replication process will get stuck. You can find similar restrictions in Google Cloud Spanner.

Take pessimistic transactions as an example, due to the fact that before the transaction commits:
- TiDB first executes the DML edit key and then locks
- Data edit are cached in the MemBuffer of the TiDB Server
- Collisions will only be detected and data written to TiKV layer upon submission

Therefore, you also need to pay attention to the memory consumption caused by large transaction. In a production environment, it is recommended to split large transactions into small transactions, to some extent, to improve the probability of successful transaction submission and reduce the cost of transaction rollback.