# Deadlocks in Transaction Processing

> A deadlock in transaction processing occurs when two or more transactions each wait for a lock held by another, forming a cycle in which none can proceed. Databases typically detect it with a wait-for graph and resolve it by choosing one transaction in the cycle as a victim, aborting and rolling it back to release its locks.

Canonical: https://devsunite.com/resources/core-subjects/dbms/deadlocks
Last updated: 2026-11-22

Subject: DBMS · Category: Transaction Processing · 3 min read

A deadlock in transaction processing occurs when two or more transactions are each waiting for a lock held by another transaction in the same group, forming a cycle where nobody can proceed. Without intervention, every transaction in that cycle would wait forever.

## How a deadlock actually forms

```
T1: acquires lock on row A
T2: acquires lock on row B
T1: tries to acquire lock on row B  -- must WAIT, T2 holds it
T2: tries to acquire lock on row A  -- must WAIT, T1 holds it
```

T1 is waiting on T2, and T2 is waiting on T1. Neither can ever get what it needs, since the other is also stuck waiting. This is a deadlock: a cycle of transactions each blocked on a lock the other one holds.

## How databases detect and resolve it

Most databases handle this with a wait-for graph: a graph where each transaction is a node, and an edge from T1 to T2 means "T1 is waiting for a lock T2 holds." The database periodically checks this graph for a cycle. If one exists, that's a deadlock, and the database resolves it by choosing one transaction in the cycle (often the one that's done the least work, or based on some other cost heuristic) as the "victim," forcibly aborting and rolling it back. That releases its locks, breaking the cycle and letting the remaining transaction(s) proceed.

## A worked example, resolved

Continuing the T1/T2 example above, the database detects the cycle T1 waits for T2 waits for T1, and picks T2 as the victim:

```
Database: ABORT T2, rollback all of T2's changes, release T2's locks
T1: now acquires lock on row B successfully
T1: completes and commits
T2: the application typically retries the whole transaction from scratch
```

The application layer needs to actually handle this: catching a deadlock-related error from a failed transaction and retrying it, since from the database's point of view, an aborted transaction due to deadlock is a normal, expected outcome under real concurrent load, not something to treat as a fatal error.

## Where this matters in real systems

Deadlocks are a genuinely routine occurrence in busy, write-heavy relational databases, not a rare edge case, and production applications that use transactions with row locking are expected to include retry logic specifically for deadlock-related failures. One practical mitigation that reduces deadlock frequency: always acquiring locks in a consistent order across a codebase (e.g. always lock the lower account ID before the higher one in a transfer), which prevents the exact circular-wait pattern that causes deadlocks in the first place.

## What interviewers actually ask about deadlocks in transaction processing

- **"What is a deadlock?"** A cycle of two or more transactions, each waiting for a lock held by another transaction in the cycle, so none of them can ever proceed.
- **"How does a database detect a deadlock?"** By maintaining a wait-for graph of which transactions are waiting on which, and periodically checking it for a cycle.
- **"How does a database resolve a detected deadlock?"** It picks one transaction in the cycle as a victim, aborts and rolls it back, releasing its locks so the remaining transaction(s) can proceed.
- **"What's a practical way application code can reduce deadlock frequency?"** Always acquiring locks (or performing writes) in a consistent, predictable order across the whole codebase, so circular wait patterns can't form in the first place.

## Key takeaways

- A deadlock is a cycle of transactions each waiting for a lock another one in the cycle holds.
- Databases detect deadlocks using a wait-for graph and look for cycles in it.
- The database resolves a deadlock by aborting one transaction (the victim), releasing its locks.
- Applications using row locking should include retry logic for deadlock failures, and can reduce their frequency by locking resources in a consistent order.

## Related

- [Previous: Locking Protocols](https://devsunite.com/resources/core-subjects/dbms/locking-protocols)
- [Next: Recoverability of Schedules](https://devsunite.com/resources/core-subjects/dbms/recoverability)
- [More in Transaction Processing: What Is Transaction Processing?](https://devsunite.com/resources/core-subjects/dbms/what-is-transaction-processing)
- [More in Transaction Processing: Schedules and Their Types](https://devsunite.com/resources/core-subjects/dbms/schedules-and-types)
- [More in Transaction Processing: Serializability](https://devsunite.com/resources/core-subjects/dbms/serializability)
- [More in Transaction Processing: What Is Concurrency Control?](https://devsunite.com/resources/core-subjects/dbms/what-is-concurrency-control)
- [More in Transaction Processing: Problems in Concurrency Control](https://devsunite.com/resources/core-subjects/dbms/problems-in-concurrency-control)
- [All 174 DBMS interview questions](https://devsunite.com/resources/core-subjects/dbms)
