Stop 12 · Transactions & ConcurrencyTopics · 7/9

Deadlocks

Part of the SQL Roadmap

In short

Two transactions can block each other forever, each waiting for the other to let go. Databases spot this and cancel one, and your app needs to know what to do next.

A deadlock happens when two transactions each hold a lock the other needs, so neither can continue. The database detects it and cancels one of them, which the application should retry.

In a narrow old-city lane, two cycle rickshaws are stuck nose to nose while the jaadu, wearing a traffic constable's cap, waves one of them to reverse.

Think of it like this

Two rickshaws in a narrow lane

In a lane in Chandni Chowk, two cycle rickshaws meet nose to nose. Each needs the space the other is in, and neither will reverse. Nobody moves. Finally a traffic constable picks one and makes it back out. The other passes, and the first one tries again. That constable is the deadlock detector.

Watch it happen

Aarav pays Meera while Meera pays Aarav

Txn 1
accounts
Txn 2

UPDATE id 1 (lock row 1)

1/5Txn 1 debits Aarav's account and holds a lock on row 1.

The big ideas

What you need to know

A waiting cycle

A deadlock needs at least two transactions, each holding a lock the other one wants.

Detected automatically

PostgreSQL checks after a short wait (deadlock_timeout, 1 second by default) and aborts one transaction.

Same order everywhere

If every transaction locks rows in the same order, such as lowest id first, a cycle can't form.

Retry, don't panic

Deadlocks happen in busy systems. Catch the error and rerun the whole transaction.

See it in code

Walk through the code

sql
1-- Session 1: Aarav (id 1) pays Meera (id 2) ₹5002BEGIN;3UPDATE accounts SET balance = balance - 500 WHERE account_id = 1;4 5-- Session 2: Meera pays Aarav ₹300, at the same time6BEGIN;7UPDATE accounts SET balance = balance - 300 WHERE account_id = 2;8 9-- Session 110UPDATE accounts SET balance = balance + 500 WHERE account_id = 2;  -- waits11 12-- Session 213UPDATE accounts SET balance = balance + 300 WHERE account_id = 1;14-- ERROR:  deadlock detected15 16-- The fix: lock both rows up front, always lowest id first17BEGIN;18SELECT account_id FROM accounts19WHERE account_id IN (1, 2)20ORDER BY account_id21FOR UPDATE;22-- ...now run both UPDATEs, then COMMIT

1/5Session 1 locks Aarav's row.

PostgreSQL output shown. MySQL reports error 1213 and SQL Server error 1205 ('chosen as the deadlock victim'). In every database, the fix is the same: consistent lock order plus a retry.

Save yourself the pain

Common mistakes

Oops

Treating a deadlock error as a bug and showing it to the user.

Do this instead

It's a normal event under load. Retry the transaction a couple of times automatically.

Oops

Updating rows in whatever order the request arrives.

Do this instead

Sort the ids and lock in that order, in every code path that touches those rows.

Your mission

Make a deadlock on purpose

~20 min

Quick check

What is the most reliable way to prevent most deadlocks?

Go deeper

  1. 1Reproduce a deadlock with two sessions updating two rows in opposite order.
  2. 2Read the deadlock error message and find which transaction was chosen as the victim.
  3. 3Prevent most deadlocks by always touching rows and tables in the same order.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge