Last modified: January 24, 2026
This article is written in: 🇺🇸
Database transactions are a cornerstone of reliable data management. They let an application bundle multiple low-level reads and writes into a single, all-or-nothing unit so the database moves cleanly from one consistent state to another—even when dozens of users race to change the same rows or hardware glitches interrupt the process. Transactions offer the developer a simple success/ failure switch while the engine handles locking, logging, versioning, and recovery behind the scenes.
| Application (Transaction Context) |
| |
| BEGIN TRANSACTION ──► Perform SQL/CRUD Operations |
| |
| COMMIT ◄── Success? ────── Yes ───┐ |
| │ |
| ROLLBACK ◄── On error or cancel ───┘ |
+-------------------------------------------------------------+
A typical flow starts with BEGIN (or an implicit start), runs several statements that may touch many tables, then finishes with COMMIT to make every change permanent—or ROLLBACK to annul them if any step fails.
Single-Object Write Flow
+-------------------------+
| BEGIN |
+-------------+-----------+
|
v
+-------------------------+
| Lock / Version Check |
+-------------+-----------+
|
v
+-------------------------+
| Apply the Write |
+-------------+-----------+
|
v
+-------------------------+
| COMMIT or ROLLBACK |
+-------------------------+
Complex workloads touch many independent resources—multiple tables, shards, or even distinct databases—so additional coordination layers are required.
Coordinator Participant(s)
+-------------------------+ +------------------------------+
| 1. PREPARE (ask to vote) ──► | PRE-COMMIT / VALIDATE |
| | | ─────────────────────────► |
| ◄── 2. VOTES (YES / NO) | | (Vote) |
+-------------------------+ | +------------------------------+
| All YES? |
v |
+-------------------------+ |
| 3. COMMIT else ROLLBACK | |
+-------------------------+ |
Deadlock Example
Transaction A Transaction B
| |
lock X lock Y
| |
wait Y ◄────────────── wait X
| |
(circular wait → deadlock)
| Method | Underlying Mechanism | Pros | Cons | Typical Scenarios |
| Locks | Pessimistic read/write locks | Strong consistency; simple mental model | Blocking, deadlocks, lock-escalation overhead | OLTP systems where conflicts are frequent |
| MVCC | Append row versions + snapshot reads | Readers never block writers; high read scalability | Vacuuming/garbage collection; more I/O for version churn | Mixed read-heavy workloads (PostgreSQL, InnoDB) |
| Timestamps | Assign global time/order to Txns | Easy to serialize logically; no blocking | High abort rate if contention; clock or logical-counter drift | In-memory or distributed DBs (Spanner, FoundationDB) |
| Optimistic | Validate at commit (compare‐and‐swap) | Near-zero overhead during read phase; suits low contention | Late failures waste work; write-write conflicts cause retries | Microservices or mobile apps with rare collisions |