Databases & StorageAdvanced16 min read2 questions

ACID Transactions and Isolation Levels

The concurrency bugs that only appear in production. What each isolation level actually prevents, why the default is weaker than you think, and how to pick locking strategy.

Covers: Atomicity, consistency, isolation, durability; read phenomena; MVCC; optimistic and pessimistic locking; deadlocks

Isolation levels are the most commonly misunderstood topic in databases, largely because the defaults are weaker than most engineers assume. Postgres defaults to Read Committed. MySQL defaults to Repeatable Read. Neither is Serializable - so anomalies your code does not handle are, by default, possible.

Filter
0/2 mastered
IntermediateacidtransactionsdatabasesAsked at Oracle, Amazon

30-second answer

Atomicity means all-or-nothing. Consistency means the transaction moves the database from one valid state to another, respecting constraints. Isolation means concurrent transactions do not observe each other's partial work. Durability means a committed write survives a crash. In a distributed system, isolation is by far the hardest - atomicity across nodes is solvable with two-phase commit, and durability with replication, but providing serializable isolation across partitions requires global coordination that costs latency on every operation.

AdvancedisolationconcurrencymvccAsked at Stripe, Amazon

30-second answer

Read Uncommitted allows dirty reads and is essentially never useful. Read Committed prevents dirty reads but allows non-repeatable reads and phantoms - it is the Postgres default. Repeatable Read gives a stable snapshot for the transaction's duration, preventing non-repeatable reads; MySQL's InnoDB also prevents most phantoms via gap locks, and it is MySQL's default. Serializable behaves as if transactions ran one at a time, preventing everything, at the cost of aborts or lock contention. Default to Read Committed, and use Serializable specifically for transactions that read data and then write based on what they read.

Showing 2 of 2 questions for acid-transactions-and-isolation-levels.

Check your understanding

4 questions · no sign-up, nothing stored

0/4 answered
Question 1

1.Two transactions each read 'at least one doctor is on call' and each sign a different doctor off. Which anomaly is this?

Question 2

2.What is the default isolation level in PostgreSQL?

Question 3

3.Which is the most robust fix for a counter increment under high concurrency?

Question 4

4.In an MVCC database, what does a long-running idle transaction cause?

Hands-on challenge

Build it - this is what you talk about in a deep-dive round.

Break and then fix a concurrency bug

Reproduce a real anomaly against a real database, then fix it three different ways and compare.

Requirements

  • Create an `accounts` table and reproduce a lost update with two concurrent sessions at Read Committed.
  • Fix it with an atomic UPDATE, then with SELECT FOR UPDATE, then with an optimistic version column.
  • Benchmark all three under 50 concurrent workers on one hot row and record throughput and error rates.
  • Reproduce write skew with a two-row invariant, and demonstrate that Repeatable Read does not stop it.
  • Show Serializable preventing it, and handle the resulting serialization failure with a retry.

Stretch goals

  • Induce a deadlock deliberately and capture the database's deadlock report.
  • Measure how much table bloat a 10-minute idle-in-transaction session causes.