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.

Worth knowing

A deadlock is a cycle in the wait-for graph: A waits on B, B waits on A. Waiting alone is not a deadlock.
Locks are held until the transaction ends. There is no way to release one early.
The database detects the cycle and kills one transaction. It cannot avoid the cycle for you.
The fix is discipline, not configuration: always take locks in the same order, everywhere.

Overview

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.

In this guide
  1. A cycle of waits
  2. Reduce the chance of a cycle
  3. Deadlocks and isolation
  4. Make retries safe
  5. Recognize the failed transaction
  6. Further reading

A cycle of waits

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.

Reduce the chance of a cycle

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.

Deadlocks and isolation

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.

Make retries safe

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.

Recognize the failed transaction

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.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. What distinguishes a deadlock from ordinary lock waiting?

  2. What is the reliable way to prevent deadlocks?

  3. After a deadlock error, what must the application retry?

Cheat sheet

Deadlocks

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.

DATABASE · vizlearn.in/database/deadlocks_in_sql.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.