Skip to content

Lesson 2 of 9 · 23m

Transactions, Isolation, and InnoDB Locking

MVCC, isolation levels, row locks, gap locks, deadlocks, and why the transaction you forgot about is often the real incident.

A database rarely freezes because InnoDB suddenly forgot how to lock rows. It freezes because one transaction touched rows in the wrong order, another held a snapshot open for twenty minutes, and a third ran a schema change behind both of them. Understanding isolation is not academic; it is how you read a production stall without guessing.

MVCC: readers see versions, writers create new ones

InnoDB implements multi-version concurrency control using undo records. Under the default REPEATABLE READ isolation level, a consistent read sees the data as of the transaction's snapshot, even if other transactions commit newer versions meanwhile.

START TRANSACTION;
SELECT balance FROM accounts WHERE id = 42;
-- this repeatable-read snapshot stays pinned while the transaction remains open
UPDATE accounts SET balance = balance - 25 WHERE id = 42;
COMMIT;

This is wonderful for consistency, but a long snapshot keeps old row versions alive. The purge thread cannot remove undo history that an open transaction might still need.

Isolation levels are operational choices

REPEATABLE READ is the InnoDB default and gives stable snapshots plus gap-lock behavior. READ COMMITTED creates a fresh read view for each statement and generally reduces gap locking. Many high-write applications prefer READ COMMITTED, but do not change it casually: replication format, application assumptions, and phantom-read behavior all matter.

SELECT @@transaction_isolation;
SET SESSION transaction_isolation = 'READ-COMMITTED';

Test with the real transaction patterns, not a single-row demo. The bugs live in retries, batches, and concurrent writers.

Locks follow indexes

InnoDB locks index records, not abstract rows. If an update cannot use an index, it may scan and lock far more records than you intended. Under REPEATABLE READ, InnoDB can also use gap locks to prevent inserts into ranges needed to preserve consistent results.

-- indexed user_id: narrow row locks
UPDATE orders SET status = 'packed' WHERE user_id = 9182 AND id = 102233;

-- unindexed status: can scan huge ranges and lock what it scans
UPDATE orders SET packed_at = NOW() WHERE status = 'paid';

The second statement is how a simple cleanup job becomes a warehouse outage.

Deadlocks are normal; useless retries are not

A deadlock means two transactions each hold something the other needs. InnoDB detects the cycle and kills one victim. The correct application response is usually to retry the whole transaction, not to retry the final statement or disable detection.

SHOW ENGINE INNODB STATUS\G

Read the LATEST DETECTED DEADLOCK section. It shows the transaction statements, the indexes involved, and which transaction was rolled back. Fix ordering first: if every workflow updates rows in the same deterministic order, many deadlocks disappear.

Field notes

  • Use SELECT * FROM information_schema.innodb_trx during incidents; the oldest transaction is often more important than the newest one.
  • FOR UPDATE is a write lock even inside a read-looking statement. Use it deliberately.
  • autocommit=0 in a pooled connection is a classic leak: one abandoned session holds locks and undo for hours.
  • Deadlock logs lie by omission. They show a sample, not every deadlock. Instrument deadlock counts and application retries.

Was this lesson helpful?

Sign in to rate