Data and Distributed Systems · 19 min · free lesson

Transactions and Isolation

Isolation is the letter in ACID people actually get wrong.

Core idea

Choose transaction boundaries and isolation for a named invariant on a named database.

Lesson map: a transaction that reads a balance of 100, misses a concurrent commit and writes 90, above the rule to choose isolation for a named invariant on a named database.
In PostgreSQL, Read Committed loses the concurrent update; Repeatable Read aborts this write.

Understand it

ACID describes distinct transaction properties whose exact guarantees depend on the engine and configuration. Serializable execution is equivalent to some serial order; it may require retries and does not automatically imply external consistency or correct application invariants.

ACID is a set of transaction properties, not a promise that every concurrent read sees the same snapshot. Atomicity means the transaction's changes commit together or are rolled back together under the database's contract. Consistency means the declared constraints and application invariants remain valid across a committed transaction. Isolation describes how concurrent transactions interact; durability describes what survives an acknowledged commit according to the storage system's guarantees. Each property depends on the database and configuration in use.

Use a concrete isolation example. Two transactions read the same inventory count and both attempt to reserve the last item. Under a weak isolation pattern, both may make a decision based on stale information unless the update is conditional or locked appropriately. A unique constraint or compare-and-swap style update can protect a specific invariant even without the strongest global isolation level. Write down the anomaly you must prevent: dirty read, lost update, write skew, or phantom. Then choose a mechanism and test with concurrent transactions.

Avoid treating isolation names as identical across every engine. Read committed, repeatable read, snapshot isolation, and serializable have implementation-specific details and costs. Serializable aims for an outcome equivalent to some serial order, often at the price of retries or contention. A strong interview answer names the engine's default, the invariant at risk, the chosen transaction boundaries, and how retryable conflicts are handled. That is more convincing than reciting the acronym.

Take PostgreSQL, whose default is Read Committed. Each command there gets a new snapshot, so nonrepeatable reads and phantoms can occur, and the balance example below loses the concurrent update. Repeatable Read keeps one snapshot for the transaction and aborts that write with a serialization failure, but anomalies such as write skew across different rows remain possible. Serializable also detects those and aborts one transaction, so the application must be ready to retry.

One final distinction: a database constraint may protect one invariant even when a transaction uses a weaker isolation level. Ask whether a lost update, duplicate record, or inconsistent cross-row rule is the actual risk. Then choose the smallest mechanism that prevents that anomaly and describe the retry path.

Interview follow-up: How would an atomic conditional UPDATE protect a nonnegative balance? Compare it with a read-then-write application sequence.

Read the code

BEGIN;
SELECT balance FROM account
  WHERE id = 1;  -- app reads 100
-- another transaction commits 50 here
UPDATE account SET balance = 90
  WHERE id = 1;  -- app writes 100 - 10
COMMIT;

In PostgreSQL, Read Committed loses the concurrent update; Repeatable Read aborts this write.

Practice starter

-- PostgreSQL Read Committed; a concurrent transaction commits an update.
BEGIN;
SELECT balance FROM account WHERE id = 7;
-- Concurrent COMMIT occurs here.
SELECT balance FROM account WHERE id = 7;
COMMIT;

Try it

Name one anomaly that PostgreSQL still permits at Read Committed and one at Repeatable Read, and what Serializable asks of the application.

What success looks like: You can state PostgreSQL’s default level and one anomaly it allows.

Check your understanding

Under PostgreSQL Read Committed, can a second SELECT observe a row committed after the first SELECT?

Reveal the answer
  1. No; BEGIN alone fixes one transaction-wide snapshot at Read Committed.
  2. Yes; separate commands can observe different committed snapshots.
  3. Only if the second SELECT explicitly takes an exclusive row lock.

This is an engine-specific isolation contract. State the invariant and handle retryable conflicts when choosing stronger isolation.

Go deeper

Common misconception: Only if the second SELECT explicitly takes an exclusive row lock.

No lock is needed. At PostgreSQL Read Committed each SELECT takes its own snapshot, so a plain second SELECT already sees the newly committed row.

  1. Identify the contract: Isolation is the letter in ACID people actually get wrong.
  2. Trace the concrete example: In PostgreSQL, Read Committed loses the concurrent update; Repeatable Read aborts this write.
  3. Test the changed constraint: How would an atomic conditional UPDATE protect a nonnegative balance? Compare it with a read-then-write application sequence.

Transfer challenge: How would an atomic conditional UPDATE protect a nonnegative balance? Compare it with a read-then-write application sequence.

Connections

  • Backpressure: Bounded queues and load shedding
  • Indexes and B-trees: Composite index selectivity, planner choices, skip scans and write cost
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Transactions and Isolation (courseId "interview", lessonId "q-acid")
Link: https://crabrix.com/learn/interview/q-acid/
Goal: Choose transaction boundaries and isolation for a named invariant on a named database.
Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.