ACID Transactions
The four guarantees that let you treat a group of operations as one all-or-nothing unit.
ACID transactions in 30 seconds
- A transaction groups steps so they all take effect or none do — essential for transfers, payments, inventory.
- Atomicity: all-or-nothing. Consistency: moves between valid states. Isolation: no peeking at half-done work. Durability: survives crashes.
- Isolation is the dial you tune: higher levels are safer but throttle concurrency.
- Common default is read-committed; add row locks or serializable only where a specific anomaly actually hurts.
- BASE (basically available, soft state, eventually consistent) trades strict consistency for scale and availability.
- ACID vs BASE is the CAP trade-off at the transaction level — draw the ACID line around money and state changes.
Transferring money between two accounts
Imagine moving $100 from Alice to Bob. It's really two steps: subtract $100 from Alice, then add $100 to Bob. Now imagine the power flickers right between them — Alice's $100 is gone, but Bob never got it. The money has simply evaporated. Or the reverse: Bob is credited, the subtraction fails, and $100 appears from nowhere. Any store that can leave you stranded between those two steps is useless for money.
What you want is a promise: treat both steps as one indivisible action — either the whole transfer happens, or none of it does, no matter what crashes. That promise is a transaction, and the four letters of ACID spell out exactly what the database guarantees about it. Everything on this page is unpacking that one scenario.
The problem it solves
Real operations often involve several steps that must succeed together. Transferring money debits one account and credits another — if the debit happens but the credit fails, money vanishes. A transaction groups steps so they either all take effect or none do. ACID is the set of four guarantees a database makes about transactions so you can reason about correctness even when things fail mid-way.
The four letters, one at a time
ACID is four separate promises that happen to spell a word. It's worth meeting each on its own, because they protect against different disasters.
| Letter | Guarantee | What it prevents |
|---|---|---|
| Atomicity | All steps happen, or none do. | A half-finished transfer leaving money lost or duplicated. |
| Consistency | A transaction moves the DB from one valid state to another, honoring all rules. | Breaking constraints (negative balance, orphaned rows). |
| Isolation | Concurrent transactions don't see each other's half-done work. | Reading another transaction's uncommitted, possibly-doomed changes. |
| Durability | Once committed, it survives crashes. | Losing an acknowledged write when the power dies. |
Atomicity — the 'all or nothing' letter
Atomicity is the one that saves our transfer. 'Atomic' means indivisible — the database refuses to apply only part of a transaction. If step two fails, step one is undone as if it never happened. The mechanism is a write-ahead log: before changing anything, the database records what it's about to do, so on a crash it can either finish the committed work or roll back the half-done work on restart.
BEGIN; -- start the atomic unit
UPDATE accounts SET balance = balance - 100
WHERE id = 'alice'; -- debit
UPDATE accounts SET balance = balance + 100
WHERE id = 'bob'; -- credit
-- Consistency rule: nobody may go negative
-- If the debit left Alice overdrawn, undo the whole thing:
-- ROLLBACK; -- both updates vanish, money is never lost
COMMIT; -- durable: survives a crash nowConsistency and Durability — valid states that stick
Consistency here means the database only ever moves from one valid state to another — it enforces your rules (a balance can't go negative, an order must point at a real user) and rejects any transaction that would break them. Durability means that once the database says 'committed,' the change survives a crash, a power cut, or a process kill the very next millisecond. A commit isn't acknowledged until the change is safely on durable storage (again, via that write-ahead log).
Isolation — the subtle one, and the only dial you tune
Isolation is about what happens when many transactions run at the same time. In a perfect world each would run as if it were alone, seeing a clean snapshot. But perfect isolation means transactions effectively queue up, which crushes throughput. So databases offer levels — each permits certain surprises (called anomalies) in exchange for more concurrency. This is the one ACID property you actively choose, so it deserves real attention.
Before the levels, meet the anomalies they're trading away — the weird things that can happen when two transactions overlap:
- Dirty read — you read a value another transaction wrote but hasn't committed; if it rolls back, you acted on data that never really existed.
- Non-repeatable read — you read a row twice in one transaction and get two different values because someone committed a change in between.
- Phantom read — you run the same
WHEREquery twice and new rows have appeared, because another transaction inserted matching rows. - Lost update — two transactions read the same value, both compute a new one, and the second silently overwrites the first.
The isolation levels
Each level is a line drawn on that list — everything below the line is allowed, everything above it is prevented. Higher levels are safer but take more locking, so they allow less concurrency.
| Level | Allows | Cost |
|---|---|---|
| Read uncommitted | Dirty reads (seeing uncommitted data). | Cheapest, least safe. |
| Read committed | Non-repeatable reads. | Common default; good balance. |
| Repeatable read | Phantom reads (new rows appearing). | Stronger, more locking. |
| Serializable | Nothing — behaves as if transactions ran one at a time. | Strongest, most contention. |
The gap a weak level can't fill — a worked lost update
Here's why the level matters in practice. Two shoppers buy the last item at the same instant. Both transactions read the stock as 10, both subtract one, both write 9 — so two sales happened but stock only dropped by one. That's a lost update, and read-committed happily allows it. The fix is to make the second reader wait by taking a lock on the row.
-- ANOMALY: two sessions each read 10, both write 9 -> one sale vanishes
-- T1: SELECT stock FROM items WHERE id = 1; -- reads 10
-- T2: SELECT stock FROM items WHERE id = 1; -- also reads 10
-- T1: UPDATE items SET stock = 9 WHERE id = 1; -- 10 - 1
-- T2: UPDATE items SET stock = 9 WHERE id = 1; -- 10 - 1 (lost update!)
-- FIX: lock the row so the second reader must wait
BEGIN;
SELECT stock FROM items WHERE id = 1 FOR UPDATE; -- locks the row
UPDATE items SET stock = stock - 1 WHERE id = 1; -- computed on fresh value
COMMIT;Why not just use ACID everywhere?
If these guarantees are so valuable, why does any store give them up? Because they have a price that bites hardest exactly where you most want to scale. Enforcing atomicity and isolation across many machines means coordination — nodes have to agree before a write is safe — and coordination is slow and fragile when the network between nodes hiccups. For a globally distributed system taking a million writes a second, that coordination can be the difference between staying up and falling over.
# On ONE machine, a transaction is cheap: lock rows, write log, commit.
# Across MANY machines holding different rows, atomicity needs a vote:
# coordinator -> "everyone PREPARE to commit"
# each node -> "ready" (and now HOLDS LOCKS, waiting...)
# coordinator -> "COMMIT" (only if ALL said ready)
#
# If one node is slow or the network drops mid-vote, the others
# sit locked and blocked. That stall is the price of strict ACID
# at scale -- and the reason some systems choose BASE instead.ACID vs BASE
Many NoSQL systems favor BASE instead — Basically Available, Soft state, Eventually consistent. They relax strict ACID guarantees (especially cross-record consistency) to scale horizontally and stay available during partitions. Neither is 'better': ACID protects correctness for money and inventory; BASE buys scale and availability where brief inconsistency is acceptable. This is the CAP trade-off showing up at the transaction level.
| ACID | BASE | |
|---|---|---|
| Core promise | Correctness, all-or-nothing, strong consistency. | Availability and scale; consistency catches up later. |
| Writes see each other | Immediately and safely. | Eventually; brief disagreement is expected. |
| Scales writes across nodes | Harder — needs coordination. | Easily — nodes act independently. |
| Right for | Money, inventory, state changes with invariants. | Feeds, logs, counters, high-volume ingest. |
| Typical home | Relational core. | Many NoSQL / wide-column stores. |
ACID isn't a database feature you toggle — it's the contract that lets you reason about correctness when steps fail mid-way, and it's the reason you can trust a bank balance or an inventory count at all.
Why it matters
- It's what makes 'the money either moved or it didn't' a guarantee rather than a hope.
- Isolation levels are a real performance lever — they decide how much concurrency you can safely allow.
- Knowing where ACID is required (and where BASE is fine) is how you keep correctness and scale from fighting.
If ignored
- Half-finished operations leave money lost, inventory oversold, or orphaned rows that corrupt reports.
- Running everything at serializable 'to be safe' quietly strangles throughput under load.
- Choosing a store with weak guarantees for a money path means discovering the gap in production.
Where it fits
- Sits in the write path, wrapping the steps that must change state together.
- Lives in the relational core of most systems; selectively offered by some NoSQL stores.
- Marks the boundary between the ACID 'truth' layer and the BASE 'scale' layer around it.
Related topics
- Consistency models: the broader spectrum that isolation levels are one slice of
- CAP & PACELC: why distributed stores often trade strict ACID for availability
- SQL vs NoSQL: which stores give you ACID and which relax it for scale
- Distributed transactions: how to keep ACID-like guarantees when a change spans services
Interview lens
Questions it unlocks
- What does ACID guarantee, and why does it matter for a payment?
- Which isolation level would you use, and what anomaly are you accepting?
- When would you give up ACID for BASE?
60-second answer
ACID gives atomicity, consistency, isolation, and durability so a group of steps behaves as one all-or-nothing unit — essential for transfers, payments, and inventory. Isolation is the dial I actually tune: I run at read-committed by default and reach for row locks or serializable only on the specific operations that need it, because full serializable throttles concurrency. Where I need scale and availability over strict cross-record consistency, I accept BASE and eventual consistency — which is just the CAP trade-off at the transaction layer.
Common traps
- Running everything at serializable and strangling throughput.
- Assuming NoSQL has no transactions, or that ACID and BASE are good-vs-bad.
- Ignoring which isolation anomalies your chosen level actually permits.
- Confusing the C in ACID (constraints) with the C in CAP (replica agreement).