ACID Properties β Complete Interview Guide π―
ACID properties are the backbone of reliable database transactions. This is a must-know topic for Data Engineering, Backend, Database, and System Design interviews.
π§ What are ACID Properties? (The Elevator Pitch)
βACID is a set of 4 guarantees that ensure database transactions are processed reliably, even in the face of errors, crashes, or concurrent access.β
π¦ The Real-World Analogy β Bank Transfer
Letβs use a βΉ5,000 transfer from Kedar β Raj as our running example throughout:
1
2
3
4
5
6
7
8
| Kedar's Account Raj's Account
Balance: βΉ20,000 Balance: βΉ10,000
Step 1: Debit Kedar β βΉ20,000 - βΉ5,000 = βΉ15,000
Step 2: Credit Raj β βΉ10,000 + βΉ5,000 = βΉ15,000
β
Both steps MUST succeed together
β If only Step 1 runs β βΉ5,000 disappears into thin air!
|
This is exactly what ACID prevents. Letβs break it down:
π€ A.C.I.D β The 4 Properties
1
2
3
4
5
6
7
8
9
10
11
12
13
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β A.C.I.D β
ββββββββββββββββ¬βββββββββββββββ¬ββββββββββββββ¬ββββββββββββββ€
β Atomicity β Consistency β Isolation β Durability β
ββββββββββββββββΌβββββββββββββββΌββββββββββββββΌββββββββββββββ€
β "All or β "Valid state β"Transactionsβ "Once saved,β
β Nothing" β to valid β don't β it stays β
β β state" β interfere" β saved" β
ββββββββββββββββΌβββββββββββββββΌββββββββββββββΌββββββββββββββ€
β π£ Bomb β π Rules β π§± Walls β πΎ Cement β
β defusal β of the game β between β it in β
β analogy β β players β stone β
ββββββββββββββββ΄βββββββββββββββ΄ββββββββββββββ΄ββββββββββββββ
|
πΉ A β Atomicity (βAll or Nothingβ) π£
βEither ALL operations in a transaction succeed, or NONE of them do. Thereβs no in-between.β
The Analogy
βLike defusing a bomb β you either cut ALL the right wires, or the whole thing resets. You canβt leave it half-defused.β
Example
1
2
3
4
| BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 5000 WHERE user = 'Kedar'; -- Step 1 β
UPDATE accounts SET balance = balance + 5000 WHERE user = 'Raj'; -- Step 2 β ERROR!
ROLLBACK; -- β¬
οΈ Step 1 is also UNDONE. Kedar gets his money back.
|
1
2
3
4
5
6
7
8
| β
SUCCESS Scenario: β FAILURE Scenario:
Step 1: Debit Kedar β
Step 1: Debit Kedar β
Step 2: Credit Raj β
Step 2: Credit Raj β (crash!)
COMMIT β
ROLLBACK β©οΈ (Step 1 undone!)
Kedar: βΉ15,000 Kedar: βΉ20,000 (restored!)
Raj: βΉ15,000 Raj: βΉ10,000 (unchanged)
|
How Databases Implement It
| Mechanism | How It Works |
|---|
| Write-Ahead Log (WAL) | Log all changes BEFORE applying them. On crash, replay or undo from log |
| Undo Log | Records the old values so changes can be rolled back |
| Shadow Paging | Writes to a copy; swap pointers only on commit |
- Interview tip: βAtomicity = a transaction is an indivisible unit. If any part fails, the entire transaction rolls back. Think of it as a single atomic operation.β
πΉ C β Consistency (βValid State β Valid Stateβ) π
βA transaction takes the database from one valid state to another valid state. All rules, constraints, and invariants are maintained.β
The Analogy
βLike the rules of chess β every move must result in a legal board position. You canβt end a move with two kings on the same square.β
Example
1
2
3
4
5
6
7
8
9
10
11
| RULE: Total money in the system must ALWAYS = βΉ30,000
(Kedar βΉ20,000 + Raj βΉ10,000 = βΉ30,000)
Before Transaction:
Kedar: βΉ20,000 + Raj: βΉ10,000 = βΉ30,000 β
After Transaction:
Kedar: βΉ15,000 + Raj: βΉ15,000 = βΉ30,000 β
β INVALID State (Consistency Violated):
Kedar: βΉ15,000 + Raj: βΉ10,000 = βΉ25,000 β (βΉ5,000 vanished!)
|
What Consistency Enforces
1
2
3
4
5
6
7
8
9
10
11
12
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONSISTENCY CHECKS β
ββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββ€
β Primary Keys β No duplicates, not null β
β Foreign Keys β References must exist β
β Unique Constraints β No duplicate emails, usernames β
β Check Constraints β age > 0, balance >= 0 β
β Triggers β Custom business rules β
β Data Types β INT field can't hold "hello" β
β NOT NULL β Required fields must have values β
β Business Rules β Total money conserved β
ββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββ
|
Example β Constraint Violation
1
2
3
4
5
6
7
8
| -- Constraint: balance >= 0
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 25000 WHERE user = 'Kedar';
-- β FAILS! balance would be βΉ20,000 - βΉ25,000 = -βΉ5,000
-- Violates CHECK constraint: balance >= 0
ROLLBACK;
-- Database stays in valid state β
|
- Interview tip: βConsistency is about rules. The database was valid before, and it MUST be valid after. Itβs the application + database working together to enforce invariants.β
πΉ I β Isolation (βTransactions Donβt Interfereβ) π§±
βConcurrent transactions execute as if they were running one after another (serially). One transaction canβt see anotherβs uncommitted changes.β
The Analogy
βLike exam halls β each student (transaction) works independently. You canβt peek at another studentβs answer sheet (uncommitted data).β
The Problem Without Isolation
1
2
3
4
5
6
7
8
9
10
11
| Transaction A Transaction B
(Kedar β Raj βΉ5,000) (Read Kedar's balance)
βββββββββββββββββ βββββββββββββββββ
1. Read Kedar: βΉ20,000
2. Debit Kedar: βΉ15,000
3. Read Kedar: βΉ15,000 β DIRTY READ!
4. β CRASH β ROLLBACK (This data was never committed!)
Kedar back to βΉ20,000
5. Transaction B used βΉ15,000
which was WRONG! π
|
The 4 Read Problems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β READ PROBLEMS β
βββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββ€
β π΄ Dirty Read β Reading data that was NEVER COMMITTED β
β β "Seeing someone's draft before they send" β
βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββ€
β π‘ Non-Repeatableβ Same query returns DIFFERENT VALUES β
β Read β because another txn modified & committed β
β β "Price changed while you were checking out"β
βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββ€
β π Phantom Read β Same query returns DIFFERENT NUMBER OF ROWSβ
β β because another txn inserted/deleted rows β
β β "New items appeared in your search results"β
βββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββ€
β π΅ Lost Update β Two txns update the same row; one β
β β overwrites the other's change β
β β "Two people editing the same doc" β
βββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββ
|
Detailed Examples of Each Problem
π΄ Dirty Read:
1
2
3
4
| Txn A: UPDATE balance SET βΉ15,000 (not committed yet)
Txn B: SELECT balance β reads βΉ15,000 β DIRTY!
Txn A: ROLLBACK β balance is back to βΉ20,000
Txn B: Already used βΉ15,000... WRONG! π
|
π‘ Non-Repeatable Read:
1
2
3
| Txn A: SELECT price WHERE id=1 β βΉ100
Txn B: UPDATE price SET βΉ150 WHERE id=1 β COMMIT β
Txn A: SELECT price WHERE id=1 β βΉ150 β DIFFERENT! π±
|
π Phantom Read:
1
2
3
| Txn A: SELECT COUNT(*) WHERE city='Mumbai' β 5 rows
Txn B: INSERT INTO customers (city='Mumbai') β COMMIT β
Txn A: SELECT COUNT(*) WHERE city='Mumbai' β 6 rows β PHANTOM! π»
|
π Isolation Levels (From Lowest to Highest)
1
2
3
4
5
6
7
8
9
10
11
12
13
| WEAKEST STRONGEST
(Fastest) (Slowest)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Read Read Repeatable Serializable
Uncommitted Committed Read
β β β β
β Allows: β Prevents: β Prevents: β Prevents:
β Everything β Dirty Reads β Dirty Reads β ALL problems
β β β Non-Repeatable β
β β‘ Fastest β β Reads β π’ Slowest
β β Unsafe β π‘ Moderate β π’ Safe β β
Safest
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
Isolation Level vs Problems Matrix
| Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read | Performance |
|---|
| Read Uncommitted | β Possible | β Possible | β Possible | β‘β‘β‘β‘ Fastest |
| Read Committed | β
Prevented | β Possible | β Possible | β‘β‘β‘ Fast |
| Repeatable Read | β
Prevented | β
Prevented | β Possible | β‘β‘ Medium |
| Serializable | β
Prevented | β
Prevented | β
Prevented | β‘ Slowest |
What Databases Default To
| Database | Default Isolation Level |
|---|
| PostgreSQL | Read Committed |
| MySQL (InnoDB) | Repeatable Read |
| Oracle | Read Committed |
| SQL Server | Read Committed |
| SQLite | Serializable |
- Interview tip: βIsolation is a spectrum β you trade off safety for performance. Most production systems use Read Committed as a balance.β
πΉ D β Durability (βOnce Saved, It Stays Savedβ) πΎ
βOnce a transaction is committed, the changes are permanent β even if the system crashes immediately after.β
The Analogy
βLike carving in stone β once itβs engraved, even a power cut canβt erase it. Unlike writing on a whiteboard.β
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| BEGIN TRANSACTION;
UPDATE accounts SET balance = βΉ15,000 WHERE user = 'Kedar';
UPDATE accounts SET balance = βΉ15,000 WHERE user = 'Raj';
COMMIT; β
β At this exact moment, it's PERMANENT
π₯ SERVER CRASHES 0.001 seconds later!
π Server restarts...
SELECT balance FROM accounts WHERE user = 'Kedar';
β βΉ15,000 β
(Still there! Not lost!)
SELECT balance FROM accounts WHERE user = 'Raj';
β βΉ15,000 β
(Still there! Not lost!)
|
How Databases Ensure Durability
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DURABILITY MECHANISMS β
ββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ€
β Write-Ahead Log (WAL)β Changes logged to disk BEFORE β
β β applying. On crash, replay log. β
ββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ€
β Redo Log β Records committed changes. β
β β Replays on recovery. β
ββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ€
β Checkpointing β Periodically flush in-memory β
β β changes to disk. β
ββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ€
β Replication β Copy data to multiple nodes. β
β β If one dies, others have it. β
ββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ€
β Battery-backed cache β Hardware ensures write cache |
β β survives power failure. β
ββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ
|
- Interview tip: βDurability means committed = permanent. Databases achieve this through WAL (Write-Ahead Logging) and replication.β
π Complete Transaction Lifecycle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| βββββββββββββββ
β BEGIN β
β TRANSACTION β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ βββββββββββββββββββββββββββββββββ
β Execute β β ACID in Action: β
β Operations β β β
β β β A: All ops tracked as a unit β
β - Debit β β C: Constraints checked β
β - Credit β β I: Isolated from other txns β
β - Validate β β D: Changes logged to WAL β
ββββββββ¬βββββββ βββββββββββββββββββββββββββββββββ
β
βββββ΄ββββ
βError? β
βββββ¬ββββ
/ \
Yes No
/ \
βΌ βΌ
ββββββββββββ ββββββββββββ
β ROLLBACK β β COMMIT β
β β β β
β Undo ALL β β Write to β
β changes β β disk β
β (Atom.) β β (Dura.) β
ββββββββββββ ββββββββββββ
|
βοΈ ACID vs BASE
In the world of NoSQL and distributed systems, thereβs an alternative philosophy called BASE:
1
2
3
4
5
| ACID (RDBMS) BASE (NoSQL)
ββββββββββββ ββββββββββββ
Strong Consistency Eventual Consistency
Strict Rules Flexible Rules
Pessimistic Optimistic
|
| Β | ACID | BASE |
|---|
| Full Form | Atomicity, Consistency, Isolation, Durability | Basically Available, Soft state, Eventual consistency |
| Philosophy | βBe correct at all costsβ | βBe available at all costsβ |
| Consistency | Strong (immediate) | Eventual (over time) |
| Transactions | Strict, locked | Flexible, optimistic |
| Performance | Lower (more locks) | Higher (fewer locks) |
| Scalability | Vertical (scale up) | Horizontal (scale out) |
| Use Case | Banking, Inventory, Booking | Social Media, IoT, Caching |
| Examples | PostgreSQL, MySQL, Oracle | Cassandra, DynamoDB, MongoDB |
BASE Explained
1
2
3
4
5
6
7
8
9
10
11
| B.A. β Basically Available
System guarantees availability (may serve stale data)
"The store is always open, even if some shelves aren't restocked yet"
S. β Soft State
State may change over time even without new input (due to syncing)
"The system is always in flux β data is flowing between nodes"
E. β Eventual Consistency
Given enough time, all nodes will converge to the same state
"Everyone will eventually get the memo"
|
- Interview tip: βACID = pessimistic, prioritizes correctness. BASE = optimistic, prioritizes availability. Choose based on your use case.β
π ACID + CAP Connection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| ββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β CAP Choice β ACID Behavior β
ββββββββββββββββΌβββββββββββββββββββββββββββββββββββββ€
β CP Systems β Full ACID (strong consistency) β
β (MongoDB, β Transactions are strict β
β HBase) β β
ββββββββββββββββΌβββββββββββββββββββββββββββββββββββββ€
β AP Systems β BASE (eventual consistency) β
β (Cassandra, β Relaxed ACID, flexible β
β DynamoDB) β β
ββββββββββββββββΌβββββββββββββββββββββββββββββββββββββ€
β CA Systems β Full ACID (traditional RDBMS) β
β (PostgreSQL, β Single-node, no partitions β
β MySQL) β β
ββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββ
|
π§ͺ SQL Examples β ACID in Action
Atomicity Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| -- Transfer βΉ5,000 from Kedar to Raj
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 5000
WHERE user_id = 'Kedar' AND balance >= 5000; -- Check sufficient funds (Consistency)
IF @@ROWCOUNT = 0
ROLLBACK; -- β Insufficient funds β undo everything (Atomicity)
RETURN;
END IF;
UPDATE accounts SET balance = balance + 5000
WHERE user_id = 'Raj';
COMMIT; -- β
Both succeeded β make permanent (Durability)
|
Isolation Example
1
2
3
4
5
6
7
8
| -- Set isolation level explicitly
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
SELECT balance FROM accounts WHERE user_id = 'Kedar'; -- Locked! π
-- No other transaction can modify Kedar's balance until we COMMIT
UPDATE accounts SET balance = balance - 5000 WHERE user_id = 'Kedar';
COMMIT; -- Lock released π
|
Consistency Example
1
2
3
4
5
6
7
8
9
10
11
| -- Constraints enforce consistency automatically
CREATE TABLE accounts (
user_id VARCHAR(50) PRIMARY KEY, -- No duplicates
balance DECIMAL(15,2) CHECK (balance >= 0), -- No negative balance
email VARCHAR(255) UNIQUE NOT NULL, -- Required & unique
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- This will FAIL β maintains consistency
UPDATE accounts SET balance = -500 WHERE user_id = 'Kedar';
-- β ERROR: CHECK constraint violated (balance >= 0)
|
π― Memory Aid β The ACID Story
Imagine youβre at an ATM withdrawing money:
A (Atomicity): The ATM either gives you the cash AND debits your account, or does neither. It wonβt debit without dispensing. βAll or nothing.β
C (Consistency): Your total wealth doesnβt change β money moves from bank to wallet. No money created or destroyed. βRules are followed.β
I (Isolation): If someone else is transferring to your account simultaneously, their transaction wonβt mess up yours. βNo interference.β
D (Durability): Once the ATM says βTransaction Successful,β even if the ATM crashes the next second, your money is safe. βItβs permanent.β
π€ Top Interview Questions & Answers
| # | Question | Best Answer |
|---|
| 1 | What is ACID? | 4 properties guaranteeing reliable transactions: Atomicity, Consistency, Isolation, Durability |
| 2 | Explain Atomicity with an example | Bank transfer β either both debit and credit happen, or neither does |
| 3 | Whatβs the difference between Consistency in ACID vs CAP? | ACID Consistency = rules/constraints enforced. CAP Consistency = all nodes see same data |
| 4 | What are isolation levels? | Read Uncommitted β Read Committed β Repeatable Read β Serializable (weakest to strongest) |
| 5 | What is a dirty read? | Reading uncommitted data from another transaction that might rollback |
| 6 | Phantom vs Non-Repeatable read? | Non-Repeatable = same row, different value. Phantom = different number of rows |
| 7 | How is durability achieved? | Write-Ahead Logging (WAL), redo logs, checkpointing, replication |
| 8 | ACID vs BASE? | ACID = strict, consistent, RDBMS. BASE = flexible, eventually consistent, NoSQL |
| 9 | Which databases are ACID compliant? | PostgreSQL, MySQL (InnoDB), Oracle, SQL Server, SQLite |
| 10 | Can NoSQL be ACID? | Yes! MongoDB (4.0+) supports multi-document ACID transactions. DynamoDB has DynamoDB Transactions |
| 11 | Default isolation level of MySQL? | Repeatable Read (InnoDB engine) |
| 12 | Default isolation level of PostgreSQL? | Read Committed |
π Quick Revision Cheat Sheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| A = Atomicity β "All or Nothing" β ROLLBACK on failure
C = Consistency β "Valid β Valid" β Constraints enforced
I = Isolation β "No interference" β Isolation levels
D = Durability β "Committed = Permanent" β WAL, Redo logs
π Isolation Levels (weak β strong):
Read Uncommitted β Read Committed β Repeatable Read β Serializable
π΄ Read Problems:
Dirty Read β Reading UNCOMMITTED data
Non-Repeatable β Same query, DIFFERENT values
Phantom Read β Same query, DIFFERENT number of rows
βοΈ ACID vs BASE:
ACID β Strict, Correct, RDBMS (PostgreSQL, MySQL)
BASE β Flexible, Available, NoSQL (Cassandra, DynamoDB)
π¦ ATM Analogy:
A β Cash + Debit happen TOGETHER
C β Total money conserved
I β Other transactions don't interfere
D β "Success" message = money is SAFE forever
|
π‘ Pro Interview Tip: If an interviewer asks βHow do you ensure data integrity in your system?β, structure your answer as:
- Database level: ACID transactions with appropriate isolation level
- Application level: Validation, idempotency, retry logic
- Infrastructure level: Replication, backups, WAL
- Monitoring: Alerts on constraint violations, transaction failures
This shows you think holistically, not just textbook definitions!