Post

ACID Properties in DBMS: Simple Guide & Interview Prep

Understand ACID properties (Atomicity, Consistency, Isolation, Durability) in layman's terms. Master database transactions for data engineering interviews.

ACID Properties in DBMS: Simple Guide & Interview Prep

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

MechanismHow It Works
Write-Ahead Log (WAL)Log all changes BEFORE applying them. On crash, replay or undo from log
Undo LogRecords the old values so changes can be rolled back
Shadow PagingWrites 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 LevelDirty ReadNon-Repeatable ReadPhantom ReadPerformance
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

DatabaseDefault Isolation Level
PostgreSQLRead Committed
MySQL (InnoDB)Repeatable Read
OracleRead Committed
SQL ServerRead Committed
SQLiteSerializable
  • 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
Β ACIDBASE
Full FormAtomicity, Consistency, Isolation, DurabilityBasically Available, Soft state, Eventual consistency
Philosophyβ€œBe correct at all costsβ€β€œBe available at all costs”
ConsistencyStrong (immediate)Eventual (over time)
TransactionsStrict, lockedFlexible, optimistic
PerformanceLower (more locks)Higher (fewer locks)
ScalabilityVertical (scale up)Horizontal (scale out)
Use CaseBanking, Inventory, BookingSocial Media, IoT, Caching
ExamplesPostgreSQL, MySQL, OracleCassandra, 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

#QuestionBest Answer
1What is ACID?4 properties guaranteeing reliable transactions: Atomicity, Consistency, Isolation, Durability
2Explain Atomicity with an exampleBank transfer β€” either both debit and credit happen, or neither does
3What’s the difference between Consistency in ACID vs CAP?ACID Consistency = rules/constraints enforced. CAP Consistency = all nodes see same data
4What are isolation levels?Read Uncommitted β†’ Read Committed β†’ Repeatable Read β†’ Serializable (weakest to strongest)
5What is a dirty read?Reading uncommitted data from another transaction that might rollback
6Phantom vs Non-Repeatable read?Non-Repeatable = same row, different value. Phantom = different number of rows
7How is durability achieved?Write-Ahead Logging (WAL), redo logs, checkpointing, replication
8ACID vs BASE?ACID = strict, consistent, RDBMS. BASE = flexible, eventually consistent, NoSQL
9Which databases are ACID compliant?PostgreSQL, MySQL (InnoDB), Oracle, SQL Server, SQLite
10Can NoSQL be ACID?Yes! MongoDB (4.0+) supports multi-document ACID transactions. DynamoDB has DynamoDB Transactions
11Default isolation level of MySQL?Repeatable Read (InnoDB engine)
12Default 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:

  1. Database level: ACID transactions with appropriate isolation level
  2. Application level: Validation, idempotency, retry logic
  3. Infrastructure level: Replication, backups, WAL
  4. Monitoring: Alerts on constraint violations, transaction failures

This shows you think holistically, not just textbook definitions!

This post is licensed under CC BY 4.0 by the author.