A deadlock is a state in which two or more threads, processes or database transactions each hold a resource and wait for a resource held by another member of the group. Because the waits form a cycle, none of them can proceed on its own, and the work stays blocked until something breaks the cycle.
What is a deadlock?
A deadlock is a circular wait. Transaction A has locked row 1 and wants row 2, while transaction B has locked row 2 and wants row 1. Each one waits politely for the other, and without outside intervention they would wait forever. The same pattern appears with threads and mutexes in an application, with processes and file locks in an operating system, and even with services that synchronously wait on each other.
Deadlocks are different from slow code. CPU usage is often low, nothing crashes, and logs may stay silent. What users see are hanging requests, growing latency and timeouts, which makes deadlocks a classic case for careful observability rather than guesswork.
The four conditions for a deadlock
Computer science describes four conditions, often called the Coffman conditions after a 1971 paper by Coffman, Elphick and Shoshani. A deadlock can only occur if all four hold at the same time:
- Mutual exclusion: a resource can be held by only one party at a time.
- Hold and wait: a party keeps the resources it already has while waiting for more.
- No preemption: resources cannot be taken away; they are only released voluntarily.
- Circular wait: there is a cycle of parties, each waiting for the next.
Prevention strategies work by breaking at least one of these conditions. The most common one in practice is breaking the circular wait by always acquiring locks in the same global order.
Deadlocks in databases
Relational databases detect deadlocks themselves and resolve them by aborting one transaction, the victim. The application then receives an error and should retry:
- PostgreSQL checks for deadlocks after a lock wait exceeds
deadlock_timeout(default one second) and reportsERROR: deadlock detectedwith SQLSTATE40P01. The server log contains the involved processes and statements. - MySQL InnoDB returns error 1213,
Deadlock found when trying to get lock; try restarting transaction.SHOW ENGINE INNODB STATUSshows the latest deadlock, and the settinginnodb_print_all_deadlockswrites every deadlock to the error log.
Typical causes are two code paths that update the same tables in different order, missing indexes that make an update lock far more rows than needed, and long transactions that hold locks while calling external services.
Deadlocks in application code
In application code, the database does not come to the rescue. A JVM thread deadlock stays until the process restarts. Useful tools:
- A thread dump (for example with
jstackorjcmd Thread.print) lists Java-level deadlocks explicitly, including the monitors each thread holds and waits for. ThreadMXBean.findDeadlockedThreads()lets a health check detect deadlocks programmatically.- The Go runtime aborts with
fatal error: all goroutines are asleep - deadlock!when every goroutine is blocked, but partial deadlocks are not reported.
A related problem is resource starvation through pools: if each request needs two connections from a connection pool and acquires them one after the other, a full pool can block every request at the same time. Strictly speaking this is a deadlock on pool slots.
How to detect deadlocks in production
Signals that point to a deadlock or lock contention:
- A rising count of deadlock errors or lock wait timeouts in database metrics and logs.
- Traces in which a database or lock span takes almost exactly as long as a configured timeout.
- Threads in state
BLOCKEDthat do not change between two thread dumps. - Throughput dropping to zero for one endpoint while CPU stays low, a pattern that differs from classic saturation.
Prevention follows from the conditions: acquire locks in a consistent order, keep transactions short, add indexes so that updates lock fewer rows, use lock timeouts instead of unbounded waits, and retry transactions that were chosen as victims.
Retries deserve care of their own. A retry should restart the whole transaction, not only the failed statement, because the database has already rolled back everything that happened before. Limit the number of attempts and add a short randomized delay, so two transactions that collided once do not collide again in lockstep. Log every deadlock with the statements involved, even when the retry succeeds: a deadlock that is silently retried a hundred times per hour still costs latency and hints at a design problem that will get worse with more traffic. Counting deadlocks per table or code path over time is often the quickest way to find the two queries that need a consistent lock order.
How Obseria helps
With database monitoring, Obseria shows deadlock errors, lock waits and the affected statements next to the traces that executed them. Continuous profiling reveals threads that spend their time waiting on locks, and Obseria Intelligence correlates the first deadlock errors with deployments or configuration changes.