Skip to content
Back to 50 Concepts
Topic 45 of 50transactionsAdvanced

# 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.

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

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 Trace
Interactive Engine Simulation

ACID Properties Visual Simulator

Step 1 of 3
SQL>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 Input

Step 1: Input & Parsing Stage

Engine Action: Validate SQL syntax and resolve schema metadata for ACID Properties
SQL Input

Initiating ACID Properties

Schema Catalog

Checking Table Invariants

How the Database Executes This:

The database engine inspects active tables, verifies permissions, and prepares the physical operation.

💡 Bank Vault: A transaction is an armored car delivery: all bags delivered together (A), exact account books (C), nobody peeking inside during transport (I), and locked safely in the vault forever (D).

4. Real-World Example

A stock trade execution platform handling millions of dollars in concurrent order fills.

The 4 Pillars of ACID
PropertyWhat it GuaranteesPhysical Database Mechanism
AtomicityAll-or-nothing executionUndo logs / Write-Ahead Log (WAL)
ConsistencyTransitions between valid statesConstraints (PK, FK, CHECK, Types)
IsolationConcurrent transactions don't interfereLocks, MVCC, Snapshot Isolation
DurabilityCommitted data survives power failurefsync() of WAL to non-volatile storage
Relational walkthrough: ACID guarantees that even if a lightning strike hits the data center, settled balances remain 100% accurate.

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
Step 1: ACID transactional demonstration
BEGIN 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

RuleBehavioral 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 protocolEngines recover from power failure using Analysis, Redo (replay WAL), and Undo (rollback active TX) phases.
fsync durability guaranteeDurability 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)
Caution: Setting synchronous_commit = off improves write speed but risks losing the last few milliseconds of transactions upon power loss.

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

Scenario Question:

Which ACID property guarantees that once a transaction commits, its modifications will not be lost even if the physical server loses power immediately after?

Select your answer: