Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 142/ 160
08. Transactions, Security & Production SQL Administration

ACID Properties Deep Dive (Atomicity, Consistency, Isolation, Durability)

Explain ACID guarantees and what each letter means under failure.

BeginnerACID · Txn · Guarantees3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

A transaction is all-or-nothing, keeps the rules, isn't disturbed by others, and is never forgotten once it's saved.

Think of it like…

A bank transfer at a counter: the cashier either completes both the withdrawal and the deposit or neither (atomicity), never lets an account go below its limit (consistency), serves one customer's transfer without the next customer seeing it half-done (isolation), and writes it in the ledger before handing you the receipt (durability).

Where you’ve seen it

Moving ₹500 between accounts: BEGIN; UPDATE accounts SET balance = balance - 500 WHERE id = 1; UPDATE accounts SET balance = balance + 500 WHERE id = 2; COMMIT;. If the server crashes after the first UPDATE, atomicity means neither change is kept.

Words you’ll hear

Transaction
A group of statements treated as one unit: BEGIN … COMMIT or ROLLBACK.
Write-ahead log (WAL)
A log written and flushed before data pages; it's how databases provide atomicity and durability after a crash.
Constraint
A rule the database enforces (NOT NULL, UNIQUE, FOREIGN KEY, CHECK).
Isolation level
How strictly concurrent transactions are kept apart.

Visual diagram

Loading interactive diagram

Theory & example queries

What each letter really guarantees

Atomicity: if anything fails before COMMIT (an error, a crash, a ROLLBACK), every change in the transaction is undone. Consistency: the database enforces its constraints, so a transaction that would violate a NOT NULL, UNIQUE, FOREIGN KEY or CHECK rule fails as a whole. Business rules the database doesn't know about are still your code's job.

Isolation: concurrent transactions behave as if separated to a degree set by the isolation level; at the default Read Committed, some anomalies are still possible. Durability: once COMMIT returns, the change is in the write-ahead log on disk and will be recovered after a crash or power loss.

Consistency enforced by a constraintQuery example
1Input dataStart from the source rows or state that the statement sees.
accounts
2 rows
accounts sample rows
idownerbalance
1Maya1000
2Ravi500
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1ALTER TABLE accounts2ADD CONSTRAINT balance_non_negative CHECK (balance >= 0);
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/2
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected output
ALTER TABLE

Note: Representative PostgreSQL status; the command fails if any existing balance violates the new constraint.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

A transfer that would overdraw the account now fails and is rolled back.

Key takeaway: Atomicity and durability handle failures; consistency and isolation handle rules and concurrency.

How databases deliver ACID, and where it bends

Most relational databases use a write-ahead log: changes are recorded in the log and flushed to disk before COMMIT returns, so after a crash the database replays committed work and discards uncommitted work. Isolation is provided by locks, MVCC snapshots, or both.

Some settings trade guarantees for speed. PostgreSQL's synchronous_commit = off can lose the last few hundred milliseconds of commits on a crash, and asynchronous replicas can be behind when a failover happens. Many distributed and NoSQL systems relax isolation or atomicity across partitions; know what yours promises.

An explicit rollbackQuery example
1Input dataStart from the source rows or state that the statement sees.
accounts
2 rows
accounts sample rows
idownerbalance
1Maya1000
2Ravi500
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1BEGIN;23UPDATE accounts4SET5  balance = balance - 5006WHERE7  id = 1;89ROLLBACK;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Start transaction

Open a transaction so changes can be checked and committed or rolled back together.

1/9
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected output
BEGIN UPDATE 1 ROLLBACK

Note: Assumes account 1 exists; the displayed update is discarded by the rollback.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

The debit is undone, as if it never happened. The same happens automatically if the session crashes before COMMIT.

Key takeaway: ACID holds only as strongly as your durability settings and isolation level.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Transaction

Group related changes

Put every statement that must succeed or fail together between BEGIN and COMMIT.

SQL at this stepReadable preview
1BEGIN;23… two UPDATEs …4COMMIT;
What to watch at this step
Unit
all or nothing
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

ACID is the set of guarantees a database gives for transactions. Atomicity: all of a transaction's changes happen, or none do. Consistency: every transaction leaves the data valid under its constraints. Isolation: concurrent transactions don't see each other's half-finished work. Durability: once committed, changes survive a crash.

Why it matters: These guarantees are why you can trust a database with money, orders and inventory. Interviewers ask about ACID to see whether you understand what happens on failure, such as a crash halfway through a transfer, and what each property actually protects you from.

When to reach for it

  • Explaining why a transfer needs a transaction
  • Deciding which rules to enforce as database constraints
  • Reasoning about what happens if the server crashes mid-operation

When to avoid or be careful

  • Running related changes as separate auto-committed statements
  • Confusing ACID consistency with CAP consistency (replicas agreeing)
  • Turning off synchronous commit for data you can't afford to lose

The four properties side by side

Atomicity
Best for: Multi-step changes like transfers or order + stock updates

Strengths: No half-finished state after an error or crash.

Watch out: Long transactions hold locks and resources until they finish.

Consistency
Best for: Data that must always obey rules

Strengths: Invalid data is rejected at write time.

Watch out: Only covers rules declared as constraints; business logic still lives in code.

Isolation + Durability
Best for: Concurrent users and crash recovery

Strengths: No interference between transactions; committed data isn't lost.

Watch out: Stricter isolation costs waiting or retries; full durability costs a disk flush per commit.

Interview signals & numbers to know

When this topic comes up

  • “Explain ACID”
  • “What happens if the server crashes in the middle of a transfer?”
  • Transactions and rollback
  • “Is MongoDB / your NoSQL store ACID?”
  • Write-ahead log

Benchmarks & rules of thumb

4 -
properties: A, C, I, D
0 -
changes kept from a transaction that didn't commit
1 flush -
of the WAL per commit for full durability

Common mistakes & follow-ups

Common interview mistakes

  • Saying consistency means "replicas agree"; that's the C in CAP, not in ACID.
  • Assuming isolation means transactions never interfere, at any isolation level.
  • Doing a transfer as two separate statements without a transaction and relying on nothing failing in between.

Likely interviewer follow-ups

  • How does a database guarantee durability after a power failure?
  • What's the difference between consistency in ACID and in the CAP theorem?
  • Which ACID property is weakened by Read Committed isolation, and how?
Interview tip: Explain all four with one example, the bank transfer, and say what each letter protects against. Then add one real-world nuance (WAL, or isolation levels) to show you know how it's actually implemented.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply ACID Properties Deep Dive (Atomicity, Consistency, Isolation, Durability) with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Transactions (BEGIN, COMMIT, ROLLBACK, SAVEPOINT)
Next topic
Concurrency Anomalies (Dirty Reads, Non-Repeatable Reads, Phantom Reads, Lost Updates)