Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 4 OF 6 · 3 MIN READ

Transactions, ACID and Isolation Levels

Transactions make several writes succeed or fail together. ACID, isolation levels and anomalies like dirty reads and lost updates are standard interview material, especially for anything involving money or stock.

What you will learn

  • Explain each letter of ACID
  • Describe dirty reads, non-repeatable reads, phantoms and lost updates
  • Compare the four isolation levels
  • Prevent race conditions with locks or optimistic versioning

Key concepts

ACID

Atomicity: all or nothing. Consistency: constraints always hold. Isolation: concurrent transactions do not see each other's partial work. Durability: once committed, the data survives a crash (write-ahead log).

Isolation levels

Read Uncommitted (dirty reads possible), Read Committed (PostgreSQL default; sees only committed data), Repeatable Read (MySQL InnoDB default; same rows on re-read), Serializable (as if transactions ran one at a time; may abort with serialization errors you must retry).

Pessimistic locking

SELECT ... FOR UPDATE locks the rows until commit, so other writers wait.

Optimistic locking

A version column. UPDATE ... SET version = version + 1 WHERE id = ? AND version = ?; if 0 rows changed, someone else won and you retry.

Step by step

The lost update race

Two requests read stock = 1, both see it available, both write stock = 0, and you sell two items you do not have. Fix it with an atomic conditional update or a lock.

-- atomic: the check and the write are one statement
UPDATE products SET stock = stock - 1 WHERE id = $1 AND stock > 0 RETURNING stock;
-- 0 rows returned → out of stock

Examples

A money transfer in Node.js (pg)

const client = await pool.connect()
try {
  await client.query('BEGIN')
  const { rows } = await client.query('SELECT balance FROM accounts WHERE id = $1 FOR UPDATE', [fromId])
  if (rows[0].balance < amount) throw new AppError('Insufficient balance', 422)
  await client.query('UPDATE accounts SET balance = balance - $1 WHERE id = $2', [amount, fromId])
  await client.query('UPDATE accounts SET balance = balance + $1 WHERE id = $2', [amount, toId])
  await client.query('INSERT INTO ledger (from_id, to_id, amount) VALUES ($1, $2, $3)', [fromId, toId, amount])
  await client.query('COMMIT')
} catch (err) {
  await client.query('ROLLBACK')
  throw err
} finally {
  client.release()
}

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Explain how two transfers A→B and B→A at the same time can deadlock, and how ordering locks by id prevents it.
  • Implement optimistic locking for editing a document.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What is a phantom read?

Running the same range query twice in one transaction returns new rows the second time because another transaction inserted them. Serializable isolation (and MySQL's Repeatable Read with gap locks) prevents it.

When would you choose optimistic over pessimistic locking?

Optimistic when conflicts are rare and you do not want to hold locks (editing profiles, CMS documents). Pessimistic when conflicts are common and retries are costly (a hot inventory row, account balances).

Why should transactions be short?

Open transactions hold locks and keep old row versions alive, which blocks other writers and bloats the table. Never call slow external APIs inside a transaction.

Summary

Transactions give all-or-nothing writes; isolation levels decide what concurrent transactions see. Prevent races with atomic conditional updates, row locks or version columns, and keep transactions short.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus