Lesson 3 of 5 Operational and Purpose-Built Databases

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.

ProblemWhat happensUsual causes
BlockingA session waits for locks another session holdsLong transactions; large updates; missing indexes causing scans that lock many rows
DeadlockTwo sessions each wait for the other; the database ends oneTransactions 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.

The deadlock and its fix. Job A locks account 17 and waits for 42, while job B locks 42 and waits for 17, so each waits for the other and InnoDB ends one. When both jobs update accounts in ascending order, whichever reaches account 17 first also proceeds to 42 first, so no cycle can form.
Solid arrows mark locks held; dashed arrows mark locks requested.
Sort into groups

Blocking or deadlock?

Exam skills 2.1.6

Decide whether each situation is blocking or a deadlock.

Log in to try this activity Interactive activities are free with an account.

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.

Match the pairs

Diagnosing locks

Exam skills 2.1.6 · 3.3.4

Match each tool with the engine or view it belongs to.

Log in to try this activity Interactive activities are free with an account.

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.

Worked example

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.

TimeJob AJob B
1Locks account 17Locks account 42
2Waits for account 42Waits for account 17
3Continues after B is endedEnded 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.

Check yourself

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.