TiDB implements fully ACID-compliant transactions that span multiple storage nodes. You use the sameDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/pingcap/tidb/llms.txt
Use this file to discover all available pages before exploring further.
BEGIN/COMMIT syntax you know from MySQL, and TiDB handles the distributed coordination transparently.
ACID guarantees
TiDB transactions satisfy all four ACID properties:Atomicity
All writes in a transaction either commit together or are fully rolled back. Partial writes never become visible.
Consistency
Constraints are enforced at commit time. The database moves from one valid state to another.
Isolation
Concurrent transactions do not see each other’s uncommitted data. TiDB defaults to Repeatable Read isolation.
Durability
Committed data survives node failures. Transactions are only acknowledged after being written to a majority of Raft replicas.
Two-phase commit (2PC)
TiDB uses a two-phase commit protocol to coordinate writes that span multiple TiKV nodes. The TiDB server acts as the transaction coordinator. Phase 1 — Prewrite: The coordinator selects one key as the primary lock and writes tentative (“prewrite”) records for all keys involved in the transaction. Each prewrite points back to the primary lock. Phase 2 — Commit: Once all prewrites succeed, the coordinator commits the primary lock. From this moment the transaction is considered committed. Secondary locks are resolved asynchronously in the background. If the coordinator crashes mid-transaction, any TiKV node can determine the transaction’s outcome by checking the status of the primary lock, ensuring correctness without coordinator recovery.Transactions are committed only after a majority of Raft replicas have acknowledged the writes, providing strong durability guarantees even when individual nodes fail.
Transaction modes
TiDB supports two transaction modes. You can select the mode that fits your workload’s conflict rate and latency requirements.Optimistic mode
Optimistic transactions buffer all writes locally and detect conflicts only at commit time. Reads never acquire locks.- Best for: Low-conflict workloads where most transactions succeed on the first try.
- Benefit: Lower per-statement latency — no round trips to TiKV during execution.
- Drawback: Commit may fail with a write-write conflict error, requiring the application to retry.
Pessimistic mode
Pessimistic transactions acquire locks on rows as each DML statement executes, before commit time. This is the default mode.- Best for: High-conflict workloads, or when you need guaranteed forward progress without application-level retry logic.
- Benefit: Commit rarely fails due to conflicts; blocking behavior matches MySQL semantics.
- Drawback: Each write statement adds a lock round trip to TiKV.
Choosing between modes
| Factor | Optimistic | Pessimistic |
|---|---|---|
| Conflict rate | Low | High |
| Application retry logic | Required | Not required |
| Per-statement latency | Lower | Slightly higher |
| MySQL behavioral compatibility | Lower | Higher |
| Default | No | Yes |
Transaction isolation levels
TiDB supports two isolation levels.Repeatable Read (default)
Each transaction reads from a consistent snapshot taken at the start of the transaction. Writes from other committed transactions that happen after your snapshot are not visible within your transaction.TiDB’s Repeatable Read implementation uses Multi-Version Concurrency Control (MVCC). Phantom reads are prevented within a single transaction’s snapshot, but the behavior differs slightly from MySQL’s gap-lock-based approach. For write operations (
SELECT ... FOR UPDATE), TiDB reads the latest committed data rather than the snapshot.Read Committed
Each statement within the transaction reads the latest committed data at the time the statement executes, rather than a snapshot from transaction start.Stale Read
Stale Read lets you query data at a point in the past. Because the query can be served from any replica — including followers — without a network round trip to the leader, it significantly reduces read latency for read-heavy workloads. TiDB provides four ways to use Stale Read:AS OF TIMESTAMP clause (single statement)
AS OF TIMESTAMP clause (single statement)
Attach
AS OF TIMESTAMP directly to a SELECT statement to read from a specific point in time.START TRANSACTION READ ONLY AS OF TIMESTAMP (transaction)
START TRANSACTION READ ONLY AS OF TIMESTAMP (transaction)
Begin a read-only transaction at a specific timestamp. All reads within the transaction use that snapshot.
SET TRANSACTION READ ONLY AS OF TIMESTAMP (next transaction)
SET TRANSACTION READ ONLY AS OF TIMESTAMP (next transaction)
Set the staleness for the next transaction or statement before beginning it.
tidb_read_staleness session variable (session-wide)
tidb_read_staleness session variable (session-wide)
Enable Stale Read for all subsequent
SELECT statements in a session, up to a maximum staleness.Large transaction support
TiDB handles large transactions that write many rows or consume significant memory. By default, TiDB limits total transaction size to prevent unbounded memory use (controlled bytidb_txn_total_size_limit). For workloads that require writing large batches, TiDB supports Pipelined DML, which flushes write buffers to TiKV incrementally rather than holding everything in memory until commit.
When using pipelined DML, the transaction is not fully atomic in memory — a rollback after a partial flush requires TiDB to clean up already-flushed data. Use this mode only for batch operations where you control the workflow end-to-end.