Recognising and Preventing Relational Locks and Deadlocks
This lesson explains how relational databases protect concurrent transactions with locks, and how pipelines that run long queries or large updates against operational databases can block applications or deadlock with them.
Learning outcomes
After completing this lesson, you will be able to:
- Describe row-level locking and multiversion concurrency control.
- Distinguish blocking from deadlocks.
- Diagnose locking with database tools.
- Prevent problems with short transactions and consistent ordering.
How locking works
Relational databases such as MySQL InnoDB and PostgreSQL use row-level locks for writes, and multiversion concurrency control so that reads usually do not block writes. Locks are held until the transaction ends, so a long transaction holds them for a long time.
Blocking and deadlocks
Locking problems take two forms. Blocking happens when one session waits for a lock that another session holds; the waiting session is stuck until the other commits or rolls back. It is usually caused by long transactions, large updates, or missing indexes that make a query scan, and lock, far more rows than it needs.
A deadlock happens when two sessions each hold a lock the other needs, so neither can continue. The database detects the cycle and ends one session with an error, which the application should retry. Deadlocks usually come from transactions that update the same rows in different orders, so making every transaction update rows in the same order prevents them. The following table summarises the two problems.
| Problem | What happens | Usual causes |
|---|---|---|
| Blocking | A session waits for locks another session holds | Long transactions; large updates; missing indexes causing scans that lock many rows |
| Deadlock | Two sessions each wait for the other; the database ends one | Transactions updating the same rows in different orders |
The following diagram compares the order in which two jobs lock accounts before and after a deadlock is fixed.

Blocking or deadlock?
Diagnosing locking
When jobs or queries hang on a relational database, they are often waiting for locks held by another session, and several tools show which sessions hold and wait for locks. CloudWatch Database Insights, which replaces the Performance Insights console, shows lock waits as a top wait event and identifies the queries involved.
Inside the database, PostgreSQL's pg_locks and pg_stat_activity views show which sessions hold locks and which are waiting, and MySQL's SHOW ENGINE INNODB STATUS and performance_schema tables show the same for InnoDB, including details of the most recent deadlock.
Diagnosing locks
Preventing problems
Most locking problems can be prevented by design. Transactions should be kept short, and large updates committed in batches rather than in one long transaction that holds locks for minutes. Every job should update rows in a consistent order, such as by primary key, because deadlocks occur when two transactions lock the same rows in opposite orders.
Columns used to find rows to update should be indexed, so the database locks only the rows it changes rather than scanning and locking many. Extracts for analytics should run on read replicas, away from the primary's write traffic. Finally, applications should retry transactions that the database chose as deadlock victims, because occasional deadlocks are normal in busy systems.
Two jobs that deadlock
Two batch jobs update account balances in Aurora MySQL. Occasionally one fails with a deadlock error. SHOW ENGINE INNODB STATUS shows the sequence described in the following table.
| Time | Job A | Job B |
|---|---|---|
| 1 | Locks account 17 | Locks account 42 |
| 2 | Waits for account 42 | Waits for account 17 |
| 3 | Continues after B is ended | Ended by InnoDB as the deadlock victim |
Making both jobs update accounts in ascending order of account ID removes the cycle of waits, because whichever job reaches account 17 first also proceeds to 42 first. The jobs also commit in batches of 1,000 rows to shorten lock times, and retry a transaction that is chosen as a victim.
What causes deadlocks between two jobs, and how are they avoided?
Show answer
Updating the same rows in different orders. Updating rows in a consistent order avoids them.
Exam Tips
Remember for the exam
- When queries block each other, check locks and long transactions with CloudWatch Database Insights.
- Deadlocks come from updating the same rows in different orders; use a consistent order.
- Commit large updates in batches to shorten lock times.
- Missing indexes cause scans that lock many rows.
- Retry transactions that the database ends as deadlock victims.