Skip to article content
FrontendXLesson 12 / 24

Transactions, ACID and isolation

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

advanced · about 60 minutes · 2 exercises

  • Predict
  • Debug

What you will be able to do

#
  • Wrap a multi-statement write in a transaction with commit and rollback.
  • Explain atomicity, consistency, isolation and durability.
  • Choose an isolation level and name the anomaly it prevents.

Overview

#

A transaction groups statements so they all commit or all roll back. ACID describes the guarantees: atomicity (all or nothing), consistency (constraints hold), isolation (concurrent transactions do not see each other's partial work), durability (committed data survives a crash).

A transaction that moves value

snippet.sql
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

If the process dies between the two updates, the transaction rolls back and no money is lost. Without the transaction, the first update could persist alone.

Isolation levels and anomalies

  • Read committed: no dirty reads, but non-repeatable reads and phantoms are possible.
  • Repeatable read: a row read twice returns the same value; phantoms may still appear.
  • Serializable: transactions behave as if run one at a time; prevents phantoms at the cost of aborts and retries.

Lost update

Two transactions read the same balance and both write back, so one update is lost. Fix it with a row lock (SELECT ... FOR UPDATE), an atomic UPDATE ... SET balance = balance - 100, or an optimistic version column.

Common pitfalls

  • Holding a transaction open while awaiting a slow network call, which blocks other writers.
  • Reading a value, computing in application code, then writing it back without a lock.
  • Assuming the default isolation level prevents every anomaly.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    Two concurrent transfers lose an update. Explain the lost update and give one fix.

    starter.javascript
    const { rows } = await client.query('SELECT balance FROM accounts WHERE id = $1', [1]);
    const next = rows[0].balance - 100;
    await client.query('UPDATE accounts SET balance = $1 WHERE id = $2', [next, 1]);

    Reveal the reference answer
    reference.javascript
    Both requests read the same balance, then both write a value derived from that stale read, so one decrement is lost. Fix it with an atomic update: UPDATE accounts SET balance = balance - 100 WHERE id = $1, or lock the row with SELECT ... FOR UPDATE inside the transaction.

    Authored expected output: An explanation of read-modify-write on a stale value plus an atomic or locked fix.

    Read-modify-write across two statements is unsafe under concurrency unless the read is locked or the write is expressed as a single atomic statement.

  2. Exercise 2Predict

    At read committed isolation, a transaction reads a row, another transaction commits an update, and the first reads again. What does it see?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.sql
    BEGIN;
    SELECT balance FROM accounts WHERE id = 1;  -- 100
    -- other transaction commits 150
    SELECT balance FROM accounts WHERE id = 1;  -- ?
    COMMIT;
    Choose the best answer

Common misconceptions

#
  • A transaction locks the whole database: most databases lock rows or ranges, not the entire database.
  • Higher isolation is always better: serializable can abort and retry transactions, so it trades throughput for correctness.

Follow-up practice

#
  • backend-databases-indexing

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.