Analyzing Deadlock Logs with LLMs: Reconstruct Lock Order in Three Steps
Deadlock logs are hard to read because thread and lock information is interleaved, wait relationships are buried in text, and business context is missing. This article explains a three-step method for using a large language model to turn raw deadlock logs into a structured thread–lock table, reconstruct the wait cycle, and cross-check the code to find the root cause. It also covers practical tips for prompting in layers and keeping the key decisions with developers.
1. Why Deadlock Logs Are So Hard to Read
When a deadlock occurs at the database or application level, the system usually emits a lot of information: thread numbers, transaction IDs, lock objects, wait times, the SQL statements involved, and more. In theory the data is complete, but reading it still leaves you disoriented. Three reasons stand out.
1. Thread and lock information is interleaved
A log is fundamentally text appended in time order. Thread A holds lock 1 and waits for lock 2; thread B holds lock 2 and waits for lock 1. These two records may sit far apart in the log, with unrelated output in between. Reading line by line, you have to track the state of several threads at once in your head. The cognitive load is heavy, and a circular structure is especially hard to spot directly.
2. Wait relationships aren’t intuitive
“Who holds what, who waits for whom” is expressed entirely as prose in the log. You have to convert that prose into a directed graph yourself before you can tell whether a closed loop exists. The conversion is fully manual, slow, and error-prone, especially when dozens of threads and multiple locks are involved.
3. Business context is missing
The log records locking and waiting behavior, but not the business actions behind them. Which transaction each lock belongs to, and why two transactions access the same set of resources in opposite order, are the key facts the log never captures. Without that context, even after you find the cycle it is hard to trace the problem back to the code.
So the core of deadlock analysis is not “reading text” but reconstructing a structured relationship graph: who holds what, what each thread is waiting for, and where the cycle is. Once the order is reconstructed, the cause becomes findable.
2. A Three-Step Analysis Method with an LLM
The value of a large model here is not to have it declare “the deadlock cause is X” directly. Such an unverified conclusion is often unreliable. The better use is to let the model handle the structuring work: break the unstructured log text into a table, then reconstruct the wait relationships from that table. The workflow has three steps.
Step 1: Build a thread–lock table, and hold off on conclusions
Hand the deadlock log to the model and ask it to break the log into a structured table, one row per thread, with these fields:
- Thread identifier (thread ID or transaction ID)
- Locks currently held
- Locks currently being waited on
- Wait time
- The transaction or SQL statement involved
An illustrative prompt:
Break this deadlock log into a table by thread. List the locks eachthread holds, the locks it waits for, and the correspondingtransaction. Do not judge the cause yet.
The instruction “do not judge the cause yet” matters. If you let the model jump straight to a conclusion, it may fabricate a plausible-sounding explanation from partial information and send your investigation in the wrong direction. Separating the fact layer (the table) from the reasoning layer (the cycle) is key to analysis quality.
Step 2: Reconstruct the wait cycle and mark the crossing points
With the thread–lock table in hand, the wait relationships become a graph: every “thread T1 waits for lock L, which is held by thread T2” relationship maps to an edge from T1 to T2. Have the model traverse the edges and find the closed loop, for example:
T1 holds L1, waits for L2T2 holds L2, waits for L3T3 holds L3, waits for L1 <- forms a cycle
Once the cycle is found, do two more things:
- Annotate each lock in the cycle with its business operation — which table, which row, or which transaction logic the lock protects;
- Mark where the lock order is reversed — the typical deadlock pattern is two paths accessing the same set of resources in opposite order: path A locks L1 then L2, path B locks L2 then L1. The model should point out exactly where these two crossing points are.
The corresponding prompt:
Using the thread table, reconstruct the wait cycle. Mark each lockin the cycle and its operation, and point out which two paths lockresources in opposite order.
Step 3: Cross-check the code and suggest a fix direction
A cycle holding at the log level does not confirm the root cause. You must go back to the relevant code or transaction definition, check the actual order of the lock statements, and determine which category the problem belongs to:
- Same resource, different order: two transactions lock the same set of resources in opposite order. Unify the lock order (for example, sort by resource ID and lock in sequence);
- Inconsistent lock granularity: one transaction uses row locks while another uses a table lock or range lock, and the granularity mismatch causes unexpected wait relationships;
- Transaction too long: a transaction holds locks for too long, raising the chance of colliding with other transactions.
Common fix directions include unifying lock order, narrowing lock scope, splitting large transactions, lowering the isolation level, or switching to optimistic locking. Which option to choose depends on the business scenario and must be validated by testing.
For this step, still have the model only provide directions rather than edit the code directly:
Cross-check the lock statements in the cycle against the code,confirm the order-conflict point, and suggest fix directions suchas unifying order, narrowing lock scope, or splitting thetransaction. Do not edit the code directly.
Code changes affect business correctness and must be confirmed by the developer before being applied. The model’s role is to organize the evidence and narrow the search.
3. Practical Tips
- Prompt in layers: ask for the table, the cycle, and the fix directions in separate steps. You can review the output of each step, avoiding a one-shot black-box conclusion.
- Keep the original log snippets: have the model note in the table which lines of the log each conclusion comes from, so you can trace and verify it and prevent the model from omitting or inventing information.
- Watch for lock-naming differences: databases represent locks differently (row locks, gap locks, table locks, and so on). When building the table, ask the model to preserve the original lock identifiers rather than over-normalizing them.
- Review after the fix: once the current deadlock is resolved, codify a convention of “access resources in a fixed order” in your coding standards to reduce similar problems at the source.
4. Summary
The difficulty of deadlock log analysis is turning a text-form timeline into a structured “thread–lock–wait” relationship graph. A large model is well suited to this mechanical, tedious structuring work, while the human verifies that the cycle matches the code and weighs the trade-offs among fix options. The three steps — build the table, reconstruct the cycle, cross-check the code — put the model’s organizing ability to use while keeping the key decisions in the developer’s hands, a practical path that balances efficiency with reliability.
