# Topic 45: ACID Properties
Atomicity, Consistency, Isolation, and Durability guarantees and their physical database engine implementations.
1. Definition
ACID is the set of four foundational guarantees (Atomicity, Consistency, Isolation, Durability) that ensure reliable processing of database transactions despite errors, crashes, or concurrent access.
2. Explain Like I'm 5
ACID is the ultimate safety guarantee for money: either the transfer happens completely or not at all (A), rules are never broken (C), other people can't snoop on money while it moves (I), and once confirmed, a power outage can't erase it (D).
3. Animation & Visual Simulation
Interactive Engine TraceACID Properties Visual Simulator
BEGIN;
-- All operations must succeed together:
INSERT INTO ledger (account_id, delta) VALUES (42, -100);
INSERT INTO ledger (account_id, delta) VALUES (99, +100);
COMMIT;Active InputStep 1: Input & Parsing Stage
Initiating ACID Properties
Checking Table Invariants
The database engine inspects active tables, verifies permissions, and prepares the physical operation.
4. Real-World Example
A stock trade execution platform handling millions of dollars in concurrent order fills.
| Property | What it Guarantees | Physical Database Mechanism |
|---|---|---|
| Atomicity | All-or-nothing execution | Undo logs / Write-Ahead Log (WAL) |
| Consistency | Transitions between valid states | Constraints (PK, FK, CHECK, Types) |
| Isolation | Concurrent transactions don't interfere | Locks, MVCC, Snapshot Isolation |
| Durability | Committed data survives power failure | fsync() of WAL to non-volatile storage |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectBEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- Atomicity & Consistency: UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; -- Durability guaranteed upon COMMIT: COMMIT;
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| Atomicity (A) | If any statement fails, the entire transaction rolls back. |
| Consistency (C) | Transactions must transition the database from one valid state to another, obeying all schema constraints. |
| Isolation (I) | Intermediate uncommitted states are invisible to concurrent transactions. |
| Durability (D) | Once committed, data is written to non-volatile disk and survives server crashes. |
| ARIES recovery protocol | Engines recover from power failure using Analysis, Redo (replay WAL), and Undo (rollback active TX) phases. |
| fsync durability guarantee | Durability relies on the operating system flushing write caches to non-volatile disk platters via fsync. |
7. Deep Dive: How Engines Guarantee Durability without Crashing Disk I/O
Writing random 8 KB table pages to SSD on every commit would kill database throughput. Instead, engines append compact delta records to a sequential Write-Ahead Log (WAL) file and issue an fsync() call. Sequential writes are 100x faster than random page writes, enabling high-performance durability.
-- Durability setting in PostgreSQL: -- synchronous_commit = on (Guarantees WAL is flushed before COMMIT returns to client)
8. Advantages & Disadvantages
Advantages
- •Absolute reliability for mission-critical financial and medical records.
- •Protects data integrity against unexpected system crashes and power cuts.
Disadvantages & Trade-offs
- •Strict isolation and fsync() flushes introduce latency compared to non-ACID NoSQL stores.
9. Common Interview Questions
Q1.Explain each of the four ACID properties with concrete database engine mechanisms.
1. Atomicity: All mutations succeed or none do; implemented via Write-Ahead Logging (WAL) and Undo logs. 2. Consistency: Database state transitions strictly preserve all schema constraints, foreign keys, and invariants. 3. Isolation: Concurrent transactions do not interfere with each other; implemented via Lock Managers (2PL) and MVCC snapshots. 4. Durability: Committed state survives crashes and power outages; implemented via fsync() flushing WAL records to non-volatile storage.
Q2.What is the Write-Ahead Logging (WAL) protocol and why is it essential for Atomicity and Durability?
The WAL protocol dictates that log records describing a change MUST be flushed to persistent disk BEFORE the corresponding dirty data page in shared RAM is allowed to be written to disk. If the server loses power, the database boots up, replays the WAL logs during the Redo phase to restore committed transactions, and reverses uncommitted transactions during the Undo phase (ARIES recovery).
Q3.What are the four ANSI SQL Isolation Levels and which anomalies does each prevent?
1. Read Uncommitted: Allows dirty reads, non-repeatable reads, and phantom reads. 2. Read Committed: Prevents dirty reads; allows non-repeatable reads and phantom reads. 3. Repeatable Read: Prevents dirty reads and non-repeatable reads; prevents phantom reads in PostgreSQL (MVCC snapshot); allows write skew. 4. Serializable: Prevents ALL concurrency anomalies using Serializable Snapshot Isolation (SSI) or strict 2-Phase Locking.
Q4.What is the "fsync=off" trap in database benchmarks?
Setting fsync=off tells the engine to acknowledge commits as soon as data hits the operating system memory buffer, without waiting for the disk hardware to write the bytes. In benchmarks, this shows 10x higher write throughput. However, if the server loses power or the kernel crashes, the entire database cluster suffers catastrophic unrecoverable corruption.
Q5.What is the difference between Application-level Consistency and Database Consistency?
Database Consistency ensures the database transitions from one valid schema state to another, satisfying all CHECK, UNIQUE, NOT NULL, and FOREIGN KEY constraints. Application-level consistency ensures business logic rules that span beyond database constraints (e.g. transfer amounts cannot exceed daily fraud limits) are valid before committing.
10. Interview-Ready Answer
“ACID represents the core transactional guarantees of relational databases: Atomicity via WAL rollbacks, Consistency via schema constraints, Isolation via MVCC and locks, and Durability via fsync flushes of write-ahead log records.”
11. Quick Test
Which ACID property guarantees that once a transaction commits, its modifications will not be lost even if the physical server loses power immediately after?