Failures and antipatterns · Intermediate

Deadlock: a cycle of waiting

Trace a wait cycle and distinguish prevention from detection, recovery and harmless contention.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. A cycle rather than ordinary waiting
  2. Trace two locks
  3. Prevention through ordering
  4. Recovery must respect transaction boundaries
  5. Ask Logeia about this articleCheck your understanding

A cycle rather than ordinary waiting

A deadlock occurs when participants wait on resources held by one another and cannot proceed. Waiting briefly for a lock can instead be ordinary contention. Diagnose the ownership and waiting relationships, not only the duration of a slow request.

Acquiring another lock while already holding one can create a cycle when participants use different orders. Low CPU use does not establish healthy progress: blocked participants can consume little CPU while preventing an operation from completing. The essential observation is lack of progress caused by the wait dependency.

Trace two locks

Let A hold L1 while waiting for L2, and B hold L2 while waiting for L1. Each participant requires the other to release a resource, so neither can perform the release-producing work. Drawing the ownership and wait edges makes the cycle explicit.

Database transactions updating two rows in opposite order can produce an analogous situation. The exact lock resources and victim-selection behavior are product-specific. Use the small example to understand the dependency, then inspect the actual database’s lock records and error messages to identify the real participants.

Prevention through ordering

A global acquisition order can prevent the cycle for locks governed by that rule. For example, transactions touching two accounts can lock them in increasing account-ID order. Every path, including error handling and alternative APIs, must follow the same rule for the argument to hold.

Reducing external calls and unnecessary work while holding locks can reduce contention time. However, shorter critical sections alone do not prove that the cycle structure has disappeared. Distinguish reducing probability from establishing a prevention property based on a consistent ordering discipline.

Recovery must respect transaction boundaries

If a database detects a deadlock and aborts a participant, recovery may require retrying the complete transaction, not just its final statement. External effects such as emails or payments need separate duplicate-effect handling. Transaction rollback does not undo every action outside that database.

A timeout bounds waiting but does not itself repair the acquisition cycle. Preserve lock records, acquisition order and transaction boundaries for diagnosis. Verify the revised behavior under the workload that produced the cycle, rather than treating the disappearance of one timeout as proof of a complete fix.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 18

Understand the systems behind the web · STEP 15

Choose a learning path