Skip to content

Lesson 2 of 9 · 25m

Transactions, Locking, and Isolation Without Superstition

Use InnoDB transaction semantics, row locks, gap locks, deadlocks, and isolation levels to reason about correctness and contention.

Most teams can repeat the names of isolation levels and almost none can predict what their application does under contention. The first production deadlock I debugged involved two order-update paths that touched the same rows in opposite order. Each was 'correct' in isolation. Together they serialized perfectly into a deadlock detector graph. Transactions are concurrency design, not just a way to wrap writes.

The default model: repeatable reads and row versions

MySQL's default InnoDB isolation level is REPEATABLE READ. A consistent read sees a snapshot established by the transaction, while writes use current row versions and acquire locks. READ COMMITTED gives each statement a newer snapshot and often reduces gap-lock behavior, which is why some high-contention applications prefer it.

SELECT @@transaction_isolation;
START TRANSACTION;
SELECT balance FROM accounts WHERE account_id = 42 FOR UPDATE;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 42;
COMMIT;

FOR UPDATE is deliberately strong. It reads the latest committed version and locks the row. Use it when the decision depends on the current value, not when you are simply nervous.

Locks follow indexes

InnoDB locks index records, not abstract table rows. If a predicate cannot use an index, the server may scan and lock far more rows than intended. This is why missing indexes are correctness problems as well as performance problems.

UPDATE users SET last_login_at = UTC_TIMESTAMP(6) WHERE email = 'a@sudosecurity.org';

If email is unindexed, that update can traverse huge portions of the table. Under REPEATABLE READ, next-key and gap locking can expand the protected range further. The practical rule is simple: update through selective, stable indexes or expect lock behavior you did not design.

Deadlocks are normal; unexamined deadlocks are not

A deadlock is the database refusing to let two transactions wait forever. InnoDB chooses a victim and rolls it back. Applications should expect occasional deadlocks and retry idempotent transactions, but a high deadlock rate means lock ordering or transaction scope needs work.

SHOW ENGINE INNODB STATUS\G

The LATEST DETECTED DEADLOCK section shows the statements, locks held, and locks waited for. Read it as a graph: transaction A holds X and waits for Y; transaction B holds Y and waits for X. The fix is usually consistent access ordering, smaller transactions, better indexes, or fewer rows touched per transaction.

Keep transactions short and meaningful

Do not perform network calls, user prompts, queue publishes, or file operations inside a database transaction. Every extra millisecond holds row versions, locks, and connection capacity hostage. Group writes that must succeed atomically; do not group an entire business workflow just because it is convenient.

START TRANSACTION;
INSERT INTO payments (...) VALUES (...);
UPDATE invoices SET status = 'paid' WHERE invoice_id = 1001;
COMMIT;

Autocommit is still a transaction

autocommit=1 means each statement is its own transaction. That is often fine, but multi-statement invariants need explicit boundaries. The dangerous middle ground is code that assumes grouping exists because it reads like one operation in application language.

Field notes

  • Retry deadlock victims only when the transaction is idempotent or uses unique constraints correctly.
  • SELECT ... LOCK IN SHARE MODE and FOR UPDATE have different semantics; choose intentionally.
  • Reducing rows touched often reduces lock contention more than changing isolation level.
  • READ COMMITTED is not inherently unsafe; it is a different consistency contract.
  • Killing a transaction can take time because rollback work must complete.

Was this lesson helpful?

Sign in to rate