A database transaction is a series of operations that are treated as a single logical unit of work. The key point: either all the operations happen, or none of them do.
Internals DB Operations
1. Step-by-Step Internal Flow
-
BEGIN → The database assigns a transaction ID (TID) and starts tracking all operations.
-
Read/Write Operations → Data pages are fetched from disk into the Buffer Pool.
-
Locking / MVCC →
-
Lock-based systems: lock rows/tables to ensure isolation.
-
MVCC systems: keep multiple versions of data so reads don’t block writes.
-
-
Undo Logging → Before any change, the old value is written to an Undo Log.
-
Change in Memory → Updates are made to in-memory pages in the buffer pool.
-
Redo Logging (WAL) → The intended changes are written to a Write-Ahead Log on disk.
-
Commit → The WAL is flushed to disk, guaranteeing durability.
-
Post-Commit → Locks are released, and dirty pages are eventually written to disk.
-
Rollback (if needed) → Use the Undo Log to restore old values.
2. Key Internal Components
-
Buffer Pool → Memory space to hold frequently used data.
-
Undo Log → Stores old versions for rollback.
-
Redo Log / WAL → Ensures committed changes survive crashes.
-
Transaction Table → Tracks active transactions.
-
Lock Table → Keeps record of who holds which lock.
-
Version Store (in MVCC) → Keeps older row versions for consistent reads.
3. Isolation Levels (How Concurrency Is Controlled)
-
Read Uncommitted → Reads can see uncommitted changes (dirty reads possible).
-
Read Committed → Reads only see committed data.
-
Repeatable Read → Guarantees same row returns same data in one transaction.
-
Serializable → Full isolation as if transactions run one after another.
4. Propagation Levels (For Nested Transactions)
-
REQUIRED → Use current transaction or start one if none exists.
-
REQUIRES_NEW → Always start a new transaction.
-
MANDATORY → Must join existing transaction.
-
NESTED → Sub-transaction inside a main one.
-
SUPPORTS → Join if exists, else run without.
-
NOT_SUPPORTED → Run without transaction.
-
NEVER → Fail if there’s an active transaction.
5. How Popular Databases Handle Transactions
Database | Concurrency Control | Logging | Default Isolation |
---|---|---|---|
PostgreSQL | MVCC | WAL | Read Committed |
MySQL (InnoDB) | MVCC + Locks | Redo + Undo Logs | Repeatable Read |
Oracle | MVCC | Redo + Undo | Read Committed |
SQL Server | Locks + Snapshot | Transaction Log | Read Committed |
MongoDB | Document Locks | Journal Log | Snapshot Isolation |
6. Real-World Analogy
Imagine editing a Word document:
-
Begin → Open file.
-
Undo Log → Ctrl+Z stores old text.
-
Redo Log / WAL → Auto-save captures changes.
-
Commit → Click Save.
-
Rollback → Close without saving.
In short: Internally, a database transaction relies on logging, memory buffers, and concurrency control to ensure that your operations are safe, consistent, and recoverable — even in the event of crashes.