Deadlock Log Analysis: A Three-Step Method to Reconstruct Lock Ordering
This article presents a practical three-step method for analyzing deadlock logs. Instead of reading the logs as linear text, you extract facts into a thread-to-lock mapping table, reconstruct the wait cycle as a directed graph to locate where lock ordering conflicts, and verify the actual locking sequence against transaction code and database execution plans. It also covers common deadlock variants, typical fixes such as enforcing a global lock order, and the boundary between what AI coding tools can do and what engineers must decide.
1. Why Deadlock Logs Are Hard to Read
Deadlocks themselves are not complicated. What makes them complicated is how the logs present them. Deadlock reports from a database or runtime are usually printed in timestamp order, interleaving lock acquisition, waiting, and rollback actions across multiple threads. The reader gets a linear piece of text, while the actual problem is a directed graph. This mismatch between describing a graph structure in linear text is the root cause of slow troubleshooting.
The confusion usually comes from three things:
- Threads and locks interleaved: Entries are ordered by timestamp. Thread A holds lock 1 and waits for lock 2, while thread B holds lock 2 and waits for lock 1. The two threads are interleaved, and the cycle is broken apart by the timeline.
- Wait relationships are not intuitive: Who waits for whom and who holds what is all expressed in natural language, so the reader has to build the graph mentally and can easily miss or misdraw an edge.
- No business context: The log records only locks and waits, not business actions, so you cannot tell which transaction each lock belongs to or why the ordering is reversed.
So the core of deadlock analysis is not reading text. It is reconstructing three things: who holds what, what it is waiting for, and where the cycle is. Once the ordering is reconstructed, the cause becomes obvious.
2. Step 1: Build a Thread-to-Lock Mapping Table Before Judging the Cause
The first step is to set conclusions aside and extract facts only. Break the log into a structured table, one row per thread, with lock objects in separate columns:
| Thread | Currently Holds | Waiting For | Wait Duration | Related Transaction/Statement |
|---|---|---|---|---|
| T1 | L1 | L2 | 320ms | Order update |
| T2 | L2 | L1 | 320ms | Inventory deduction |
Example prompt you can reuse:
Break this deadlock log into a table by thread. List the lock each threadholds, the lock it waits for, the wait duration, and the related transaction.Do not judge the cause yet.
The value of this step is turning reading into filling in a table. Once the table takes shape, missing edges, isolated locks, and timestamp anomalies surface automatically. At this stage, neither the model nor a person should offer causal conclusions, or later reasoning gets contaminated.
3. Step 2: Reconstruct the Wait Cycle and Mark the Crossing Point
With the mapping table in place, the wait relationships can be abstracted into a directed graph: nodes are threads (or transactions), and edges mean “waits for.” Connect the edges by who waits for whom and look for a closed loop:
T1 --waits for L2--> T2 --waits for L1--> T1
Label every lock on the cycle with its business operation, and clearly point out where the two paths acquire locks in the opposite order. Example prompt:
Reconstruct the wait cycle from the thread table. Label each lock on thecycle and its operation, and point out which two paths acquire locks inopposite order.
Watch for three common variants: a compound cycle made of several locks, inconsistent lock granularity (row locks mixed with table locks), and wait escalation caused by mixing read locks with exclusive locks. If there is no cycle in the graph but blocking still occurs, look instead at lock wait timeouts, long transactions holding locks, and resource contention rather than a deadlock.
4. Step 3: Check Against Code and the Database, Then Decide the Fix
Once the cycle is located, go back to the transaction code and the database execution plan to verify the real order of the locking statements, and confirm whether it is “same resources, different order” or “inconsistent lock granularity.” Common fix directions include:
- Enforce a consistent lock order: Agree on a global ordering by resource (for example, ascending by primary key) and acquire locks in that order.
- Narrow the lock scope: Hit indexes precisely to avoid lock spread from range scans.
- Split the transaction: Shorten lock hold time and move non-essential work outside the transaction boundary.
- Adjust the isolation level: Lower isolation strength when the business allows, reducing the surface for lock conflicts.
Illustrative code with a unified order:
def transfer(a_id, b_id, amount):first, second = sorted([a_id, b_id])with lock(first):with lock(second):move(amount, a_id, b_id)
Any concrete solution must be validated with load testing and a canary rollout. Do not change production code directly.
5. Where the Human-Machine Boundary Lies
AI coding tools handle structured breakdown and candidate cycle annotation, producing “verifiable hypotheses.” Fix decisions, trade-offs, and release calls still belong to engineers. The line in the table is clear: the model draws the structure and marks candidates; the human owns judgment and release.
