ACID Properties

Difficulty: Intermediate

Question

What are the ACID properties of a database transaction, and how does a database actually guarantee each one?

Answer

ACID is the set of guarantees a database gives for a transaction: Atomicity (all of its changes happen or none do), Consistency (it moves the database from one valid state to another, respecting every constraint), Isolation (concurrent transactions do not see each other's half-done work), and Durability (once committed, the change survives a crash). Together they let you write a money transfer as a few SQL statements and trust that it behaves like a single indivisible step.

The classic example: a wallet transfer

Moving ₹500 from Asha's Paytm wallet to Ravi's is two updates: debit Asha, credit Ravi. Without ACID, a crash between them destroys ₹500, two concurrent transfers can both read the old balance and overspend, and a "successful" transfer could vanish after a power cut. Each letter closes one of these holes.

How each property is implemented

  • Atomicity: the engine records every change in a log before applying it. On ROLLBACK or after a crash, uncommitted changes are undone. Postgres does this with MVCC row versions (an aborted transaction's rows are simply invisible), InnoDB with undo logs.
  • Consistency: enforced by constraints you declare: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK (balance >= 0), NOT NULL. If any statement would break one, the transaction fails. Note that "C" is partly the application's job: the database cannot know that money must be conserved unless you encode it.
  • Isolation: implemented with locks and/or MVCC snapshots. The strength depends on the isolation level (Read Committed, Repeatable Read, Serializable). Postgres and Oracle default to Read Committed, MySQL InnoDB to Repeatable Read, so default isolation is weaker than "fully isolated".
  • Durability: the write-ahead log (WAL) is flushed to disk with fsync before the commit is acknowledged. Data pages can be written later; after a crash the database replays the WAL. Settings like Postgres synchronous_commit = off or MySQL innodb_flush_log_at_trx_commit = 2 trade a small durability window for speed.

Common misconceptions

  • ACID consistency is not CAP consistency. ACID C means "constraints hold"; CAP C means "every read sees the latest write across replicas" (linearizability).
  • Isolation is a spectrum. At Read Committed, lost updates and non-repeatable reads are still possible. Saying "ACID means no concurrency bugs" is wrong.
  • Durability depends on hardware and config. Disks with volatile write caches that lie about fsync can break it. Durability across machines needs synchronous replication.

ACID in modern systems

All major relational databases (PostgreSQL, MySQL InnoDB, SQL Server, Oracle) are ACID. MongoDB has supported multi-document ACID transactions since 4.0 (replica sets) and 4.2 (sharded clusters), and distributed SQL systems like CockroachDB, YugabyteDB, Spanner and TiDB provide ACID across nodes. Payment systems at companies like Razorpay rely on these guarantees for ledgers, often combined with idempotency keys so retries do not double-charge.

How a commit becomes durable. The log is flushed before the client hears "committed"; data pages catch up later.
  1. BEGIN transaction: Changes are made to in-memory pages.
  2. Write changes to WAL buffer: Every modification is logged first.
  3. COMMIT: fsync WAL to disk: This is the durability point.
  4. Acknowledge client: Only after the log is safely on disk.
  5. Checkpoint writes data pages: Crash before this? Replay the WAL.

Code examples

Atomic wallet transfer with a consistency check

CREATE TABLE wallets (
  user_id      BIGINT PRIMARY KEY,
  balance_paise BIGINT NOT NULL CHECK (balance_paise >= 0)  -- Consistency
);

BEGIN;                                   -- Atomicity starts here
UPDATE wallets SET balance_paise = balance_paise - 50000
 WHERE user_id = 1;                      -- fails CHECK if balance too low
UPDATE wallets SET balance_paise = balance_paise + 50000
 WHERE user_id = 2;
COMMIT;                                  -- Durability: WAL flushed before OK

-- If either UPDATE fails, the whole transaction is rolled back
-- and neither wallet changes.

Both updates commit together or not at all. The CHECK constraint makes the database reject an overdraft, and the row locks taken by UPDATE isolate concurrent transfers on the same wallet.

Key points

  • Atomicity: all or nothing, implemented with logs and rollback.
  • Consistency: declared constraints always hold; business invariants still need correct application logic.
  • Isolation: controlled by isolation level; defaults are usually Read Committed or Repeatable Read, not Serializable.
  • Durability: write-ahead log flushed to disk before commit is acknowledged.
  • ACID consistency and CAP consistency are different concepts.

Common follow-up questions

  • What is a write-ahead log and why write the log before the data?
  • What anomalies can still happen at Read Committed?
  • How do distributed databases provide ACID across nodes?
  • What is BASE and how does it compare to ACID?

Interview tips

  • Go beyond definitions: explain how each property is implemented (WAL, constraints, locks/MVCC).
  • Point out the ACID C versus CAP C distinction; it is a frequent trap.
  • Use a money transfer example and show what breaks if each property is missing.

Concepts covered

Atomicity, Consistency, Isolation, Durability, Write-Ahead Log, Transactions

More Database Design questions