Concurrency Control, Isolation Levels & Deadlock Resolution
Pick isolation levels consciously and diagnose deadlocks when writers contend on the same rows.
In plain words
What concurrent changes cannot coexist. Next, read Committed, Repeatable Read, or Serializable. Finally, identify cycle, rollback, and bounded retry.
Think of it like…
Two forklifts each block an aisle the other needs; deadlock detection clears the jam by aborting one route, while a fixed one-way lock order prevents it.
Where you’ve seen it
Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.
Words you’ll hear
- Isolation
- The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.
- Locks
- Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.
- Deadlocks
- A wait-for cycle that the database resolves by aborting a victim transaction.
- Operational boundary
- At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects. Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.
Visual diagram
Theory & deep dive
Isolation: the working mechanism
Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks.
What concurrent changes cannot coexist. Read Committed, Repeatable Read, or Serializable, then same sequence, narrow statement, short time; this exposes the state governed by Deadlocks.
Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.
Concurrency Control, Isolation Levels & Deadlock Resolution: failure mode and production response
At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys. Track Retryable SQLSTATE as "PostgreSQL 40001/40P01 classes for serialization or deadlock", and make Transaction time visible to the dataset owner.
How it works, step by step
Step 1 of 41. State invariant
What concurrent changes cannot coexist. Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks.
- Retryable SQLSTATE
- PostgreSQL 40001/40P01 classes for serialization or deadlock
- Locks
- Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.
When to use it & trade-offs
What it is
Transactions choose how concurrent writes and snapshots interact; row/version locks protect invariants, while a deadlock occurs when transactions cyclically wait on each other's owned locks. Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE 40P01, rollback and retry the whole transaction with jitter.
Why it matters: At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects. Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.
When to reach for it
- Pick isolation levels consciously and diagnose deadlocks when writers contend on the same rows.
- Choose Isolation because it provides this capability: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.
- Keep evidence for Retryable SQLSTATE at "PostgreSQL 40001/40P01 classes for serialization or deadlock" so the last production stage-Recover - identify cycle, rollback, and bounded retry-remains testable.
When to avoid or be careful
- At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
- Do not conflate Locks with Isolation. The second concept contributes different behavior: Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix.
- Release only after this guardrail is in place: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.
Isolation / Locks / Deadlocks - roles, limits, and operating cost
Strengths: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking. Transactions choose how concurrent writes and snapshots interact.
Watch out: At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
Strengths: Synchronization on rows or objects held until commit or rollback, subject to an engine-specific lock matrix. In this workflow it supports: Order locks - same sequence, narrow statement, short time.
Watch out: The operating cost is real: keep lock and snapshot duration measured in milliseconds or bounded seconds.
Strengths: A wait-for cycle that the database resolves by aborting a victim transaction. Its output can be checked with Lock order.
Watch out: Operating guardrail: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Isolation, and how would Retryable SQLSTATE reveal it?”
- “Why is Locks distinct from this behavior: a wait-for cycle that the database resolves by aborting a victim transaction?”
- Production clue: At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
- Design probe: Use the weakest isolation that preserves the invariant, lock rows in a global order, shorten transactions, add retry policies only for classified serialization/deadlock errors, and test multi-worker races with deterministic keys.
Benchmarks & rules of thumb
- Retryable SQLSTATE
- PostgreSQL 40001/40P01 classes for serialization or deadlock
- Transaction time
- keep lock and snapshot duration measured in milliseconds or bounded seconds
- Lock order
- every multi-row path acquires the same key sequence
Common mistakes & follow-ups
Common interview mistakes
- At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
- Reporting success from a job exit code without checking Retryable SQLSTATE and Transaction time.
- Treating this example as complete without replay, rollback, or access boundaries: Transaction A locks order then inventory; B must lock inventory then order-no, reverse that to order then inventory for both. On PostgreSQL SQLSTATE
40P01, rollback and retry the whole transaction with jitter.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: The transaction visibility and anomaly contract, trading consistency strength against abort rate and blocking.
- What does the recovery plan do when this occurs? At REPEATABLE READ or SERIALIZABLE a transaction may be aborted and must retry, while READ COMMITTED can still observe changing rows across separate statements and a deadlock retry can rerun external side effects.
- When is Isolation insufficient for Transaction time, and what does adding Locks cost in Retryable SQLSTATE?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Concurrency Control, Isolation Levels & Deadlock Resolution with data engineer interview Q&A and hands-on practice.