System Design Academy

Saving…

Lesson 3 of 10 in Module 30 of 54 lessons complete

Transactions & ACID

Module 3, lesson 3Concept15 min

Definition

Transaction

In plain English
A transaction is a group of database changes that succeed together or fail together.
Think of it like
It is like a bank transfer where money can't leave one account without arriving in the other.

ACID

In plain English
ACID is a set of four promises a transaction makes: atomic, consistent, isolated and durable.
Think of it like
It is like a contract with four clauses that protect your data.

Why it exists

You reserve seat 14C but fail to record its payment. The customer sees a sold seat without a completed booking.

A database needs to treat related changes as one unit. Otherwise a crash can leave only half the intended result.

How it works

Atomicity

In plain English
Atomicity means all of a transaction's changes happen, or none do.
Think of it like
Like a train that either reaches the station with every carriage or doesn't leave at all.

Consistency

In plain English
Consistency means a transaction moves data from one valid state to another, never breaking the rules you declared.
Think of it like
Like a referee who rejects any move that breaks the game's rules.

Isolation

In plain English
Isolation means transactions running at the same time don't see each other's half-finished work.
Think of it like
Like two cooks each using their own chopping board.

Durability

In plain English
Durability means once a transaction says "done", its changes survive a crash.
Think of it like
Like a signed receipt that stays valid even if the shop's lights go out.

Commit and rollback

In plain English
Commit makes a transaction's changes permanent, while rollback throws them all away.
Think of it like
Like pressing "send" on an email versus "discard draft".
PropertyPromiseWithout it
AtomicityAll changes or noneMiles leave without arriving.
ConsistencyDeclared rules stay trueA balance becomes negative.
IsolationConcurrent work follows isolation rulesTwo buyers take the last item.
DurabilityConfirmed changes survive a crashA confirmed booking disappears.

The app begins a transaction, debits and credits miles, then commits both changes or rolls both back.

Finish the transaction by committing success or rolling back failure.

The app begins a transaction, debits and credits miles, then commits both changes or rolls both back.

Examples

Record a complete booking

You mark seat 14C sold and record its payment in one database transaction.

PostgreSQL: the app checks the changed-row count before recording payment.
SQL
BEGIN;
UPDATE seats SET sold = TRUE
WHERE seat_id = '14C' AND sold = FALSE;
-- Continue only if exactly one seat row changed.
INSERT INTO payments (booking_id, amount) VALUES (714, 80);
COMMIT;
PostgreSQL failure path: rollback discards the uncommitted seat change.
SQL
BEGIN;
UPDATE seats SET sold = TRUE
WHERE seat_id = '14C' AND sold = FALSE;
-- If the payment statement fails, the app sends:
ROLLBACK;

Recording payment is a database change. Charging an external payment service needs separate coordination; database rollback cannot undo that charge.

The insight

Atomicity prevents half-finished database actions.

Trade-offs

Pros

  • Prevents partially completed changes.
  • Protects declared rules during failures.

Cons

  • Locks and coordination add work.
  • Transactions across machines need extra coordination.

Use when

  • Money and inventory must add up.
  • Related booking changes must succeed together.

Avoid when

  • Independent readings need no grouped changes.
  • Approximate counters tolerate occasional missing updates.

Quick check

Miles leave one account but a crash stops them reaching the other. Which ACID property prevents this?
The app shows "Booking confirmed", then the database server loses power. Which property means the booking is still there?

Takeaways

  • A transaction succeeds or fails as a unit.
  • ACID covers atomicity, consistency, isolation, and durability.
  • Choose transaction boundaries and isolation around the rules you must protect.

Try the quick check above whenever you feel ready.

Previous: Relational Databases & SQL