Skip to content
Commitwise
Menu

Deadlock

Two transactions each hold a lock the other needs, so neither can continue.

Definition

Transaction 1 locks row A and waits for row B while transaction 2 holds B and waits for A. Databases detect the cycle and abort one transaction with an error (PostgreSQL after deadlock_timeout, 1 second by default); the application should retry it. Locking rows in a consistent order, such as by ascending id, prevents most deadlocks. The same idea applies to threads and mutexes.

In practice

Two transfers between the same accounts in opposite directions deadlock unless both lock the lower account id first.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.