ACID Properties

RoadmapsCore CS

Scenario

You transfer $500 from your savings to your checking account. The bank debits savings — and then the server crashes. When it restarts, the $500 is gone from savings but never arrived in checking.

What guarantee must every database provide so this can never happen — even under crashes and concurrent users?

Mental model

A transaction is an all-or-nothing deal: either every step happens, or it's like none of it ever started.

Think of a vending machine: you put in coins, select a drink, and the machine either gives you the drink AND your change, or it gives you all your coins back and you get nothing. It never keeps your coins AND fails to give a drink. ACID is the formal version of that guarantee, extended to cover crashes, concurrent users, and system-wide consistency rules.

Explanation

ACID stands for Atomicity, Consistency, Isolation, and Durability — the four properties that together guarantee a transaction behaves correctly even in the face of system failures and concurrent access. They were formalised by Jim Gray in the late 1970s and remain the gold standard for relational databases.

Atomicity means a transaction is all-or-nothing. The debit and the credit in the bank transfer are one atomic unit — either both happen or neither does. If the system crashes mid-transaction, the database must undo any partial work during recovery, leaving no trace. This is implemented via an undo log that records the original values before each change.

Consistency means a transaction takes the database from one valid state to another valid state — it must not violate any integrity constraints (primary keys, foreign keys, check constraints, business rules). Importantly, consistency is partly the database's job (enforcing constraints) and partly the application's job (writing correct transaction logic). The DB engine prevents constraint violations; it cannot prevent logic bugs.

Isolation means concurrent transactions execute as if they were running serially — one at a time. If two transactions run simultaneously, neither should see the other's intermediate, uncommitted state. In practice this is expensive to enforce fully, so SQL defines four isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) that trade correctness for performance. The stricter the level, the more locking/blocking occurs.

Durability means once a transaction is committed, its changes survive any subsequent failure — crash, power cut, OS fault. This is typically achieved via Write-Ahead Logging (WAL), as shown in the animation: the log record is flushed to durable storage BEFORE the commit is acknowledged. On recovery, the database replays the log to restore all committed changes. The data pages themselves may still be in the buffer pool, but the log on disk is sufficient for recovery.

The practical takeaway is that ACID guarantees are not free. Atomicity needs undo logs, isolation needs locking or MVCC, durability needs a synchronous disk flush per commit. High-throughput systems sometimes relax isolation (accepting stale reads) or batch commits to improve throughput. Knowing which property was relaxed — and why — is exactly what interviewers probe.

Key points

Common mistakes

Glossary

Atomicity
The rule that guarantees a database transaction is treated as a single, indivisible unit. If one part fails, the entire transaction is cancelled and rolled back.
Consistency
The requirement that a database transaction must only change the database from one valid state to another, strictly following all established rules and constraints.
Isolation
The property that ensures multiple transactions happening at the same time do not interfere with each other, acting as if they were processed one by one.
Durability
The guarantee that once a transaction is successfully completed, its changes are permanently saved and will survive a system crash or power failure.

Recall questions

Questions & answers

You're designing a payment system. Which ACID properties are most critical, and how would you verify the system provides them?

Atomicity (debit + credit as one unit) and Durability (confirmed payment survives a crash) are critical. Verify with failure injection: crash mid-transaction and check the DB state on restart. Confirm WAL is enabled and fsync is not disabled.

Approach: Frame the answer around the specific failure modes of payments — partial transfer and lost confirmation. Show you know the implementation mechanism (WAL, fsync) not just the acronym.

Difference between Atomicity and Durability — they both deal with failures, so what's the distinction?

Atomicity handles failures DURING a transaction (ensures partial work is rolled back). Durability handles failures AFTER commit (ensures committed work is not lost). Together they cover the full failure window.

Approach: Draw the timeline: before commit → Atomicity's domain; after commit → Durability's domain. A single crisp sentence for each.

Continue learning

Next: Schedules & Serializability

Next: Recovery & Logging

Return to Core CS Roadmap