Deadlocks
Two transactions taking the same two locks in opposite orders. Step through it and watch the cycle close.
Deadlocks
This module needs JavaScript: it steps through two transactions rather than showing a finished picture.
Two transactions taking the same two locks in opposite orders. Step through it and watch the cycle close.
This module needs JavaScript: it steps through two transactions rather than showing a finished picture.
Imagine transaction A locks row 1 and then requests row 2. At the same time, transaction B locks row 2 and requests row 1. A waits for B, while B waits for A. Waiting alone is normal; the cycle is the deadlock.
Imagine transaction A locks row 1 and then requests row 2. At the same time, transaction B locks row 2 and requests row 1. A waits for B, while B waits for A. Waiting alone is normal; the cycle is the deadlock.
Most databases detect the cycle and abort one transaction so the other can continue. The victim is not necessarily the transaction that caused the design problem. The application must handle the error and, when safe, retry the whole transaction.
Have transactions acquire the same resources in a consistent order—for example, lock accounts by increasing account ID. Keep transactions short, avoid waiting on network calls while holding locks, and update only the rows needed. These practices lower risk; they cannot eliminate every deadlock in a concurrent system.
For a transfer, both code paths should lock the lower account ID first, then the higher one. If a deadlock still occurs, roll back and retry the complete unit with bounded backoff and jitter. Do not retry only the last SQL statement, because earlier statements in the aborted transaction no longer form a valid partial result.
A deadlock is different from a serialization failure or a lock timeout. A deadlock is a cycle; a timeout is a wait that exceeded a configured duration. Isolation levels affect which conflicts are possible, but choosing a stronger level does not remove the need for retry handling. See transactions and ACID and isolation levels.
A retry can repeat work outside the database if a transaction sends an email, charges a card, or publishes a message before commit. Keep external side effects outside the retryable section, or use an outbox/idempotency key so repeating the transaction does not duplicate them. Log the database error code and transaction context, but avoid retry loops without a bound. A retry policy should distinguish deadlock and serialization errors from permanent failures such as a constraint violation.
After the database chooses a deadlock victim, the transaction is rolled back or marked failed according to the engine. The application should discard assumptions based on its earlier reads, begin a new transaction, and rerun the operation from the start. Keep the retry count bounded and add jitter so multiple clients do not repeat the same conflict in lockstep. Capture enough context to find the competing resource order, while avoiding sensitive parameter values in logs.
Answer without scrolling back up.
What distinguishes a deadlock from ordinary lock waiting?
Ordinary waiting resolves itself when the holder commits. A cycle cannot, because every participant is blocked on another participant that will never move.
What is the reliable way to prevent deadlocks?
A cycle requires two transactions to disagree about the order. Ordering by primary key is the usual choice because it is total and can be applied mechanically.
After a deadlock error, what must the application retry?
The victim's transaction was rolled back entirely, so re-running one statement starts from a state that no longer exists.
Set up two transactions that lock rows in opposite order, watch the deadlock form, and see the one change in lock order that prevents it.