How to Break Down Deadlock Logs with LLMs: Locating the Wait Cycle from Lock Ordering

    Deadlock logs are hard to read because entries are interleaved by time and carry no business meaning. This article shows a three-step method for turning messy logs into a verifiable structure: build a thread-to-lock mapping table, reconstruct the wait-for cycle as a directed graph and mark the crossover points where lock ordering conflicts, then confirm the lock order in the source code. It also covers common fixes such as enforcing a consistent lock order, narrowing lock scope, splitting transactions, and adjusting isolation levels, plus common pitfalls and a reusable troubleshooting checklist. LLMs can help with structuring and graph reconstruction, but root-cause judgment, code review, and testing still require human verification.

    Why Deadlock Logs Are Hard to Read

    Deadlock logs typically contain thread IDs, the locks already held, the locks being waited on, and the wait duration. The problem is that these records are printed in time order, so entries from multiple threads are interleaved. A thread might hold lock A on the first line and only start waiting for lock B on the thirtieth line, with other threads’ records mixed in between. It is very hard for a reader to keep the state of dozens of threads in mind at once.

    Logs also have an inherent limitation: they record only lock and wait actions, not business meaning. You can see the IDs of two locks but not which table or transaction each one corresponds to, so you cannot tell why two threads acquire them in opposite order.

    The goal of the analysis, therefore, is to reconstruct three things: who holds what, who is waiting for what, and where the wait relationships close into a cycle. Once the lock ordering is clear, the root cause is usually easy to locate.

    Step 1: Build a Thread-to-Lock Mapping Table

    The first step is to organize the data without drawing conclusions yet. Break the log down line by line, giving each thread one row and recording the following fields:

    • Thread identifier
    • Locks currently held
    • Lock being waited on
    • Wait duration
    • Transaction or statement involved

    List the lock objects separately in their own table, recording the resource each lock corresponds to, such as a specific row in a table or an index range. Once the associations between the two tables are established, the rest of the analysis goes much faster.

    This step can be handed to a large language model. The prompt should explicitly require structured organization only, for example:

    1. Split the deadlock log below into a table by thread. For each thread, list the locks
    2. held, the lock being waited on, the corresponding transaction or statement, and the
    3. wait duration. In this step, only organize the information; do not judge the cause
    4. of the deadlock.

    The table produced by the model must be checked row by row against the original log. Errors or omissions in the organizing stage propagate directly into the later cycle analysis, so manual spot-checking cannot be skipped.

    Step 2: Reconstruct the Wait Cycle and Mark the Crossover Points

    With the thread table in hand, the next step is to connect “who waits for whom” into a graph. Each thread is a node in the graph. If thread A waits for a lock held by thread B, draw a directed edge from A to B. When a closed loop appears in the graph, for example A waits for B, B waits for C, and C waits for A again, that loop is the deadlock’s wait cycle.

    After finding the cycle, annotate each lock on it with the corresponding business operation. For example:

    1. Thread A: holds lock1 (order line), waits for lock2 (inventory row) Operation: deduct inventory, then update order
    2. Thread B: holds lock2 (inventory row), waits for lock1 (order line) Operation: update order, then deduct inventory

    The lock ordering of these two paths is exactly reversed, and they are the crossover points that need the most attention. A cycle may contain more than two paths; during analysis, compare them pairwise and find the point in each pair of paths where the lock ordering is inconsistent.

    This step is also well suited to a large language model. The emphasis in the prompt should be on having the model reconstruct the cycle from the table and point out the paths with reversed ordering. Root-cause judgment should still be done by a human; the model’s conclusions can only serve as hypotheses to be verified.

    Step 3: Verify the Lock Ordering in the Code

    The cycle is only a runtime phenomenon; the final conclusion must go back to the code. When verifying, confirm the following:

    1. Which function or transaction each lock statement on the cycle belongs to;
    2. Whether the two code locations access the same resource, only in a different order;
    3. Whether the lock granularity of the two locations is consistent, for example one side locks an entire table while the other locks a single row;
    4. Whether the transaction contains remote calls or time-consuming operations that keep locks held for too long.

    After confirmation, common fix directions include:

    • Unify the lock ordering: agree that all code paths acquire resources in the same order, for example always lock the order first and then the inventory. This is the most fundamental approach and the one most often overlooked.
    • Narrow the lock scope: hold locks only within the necessary code sections to reduce overlap time with other transactions.
    • Split transactions: break a large transaction into several smaller ones, but evaluate business consistency after the split.
    • Adjust the isolation level: some isolation levels reduce range locks such as gap locks, but they change read semantics and must be tested against the business before deciding whether to adopt them.

    The fix must be verified in practice. It is recommended to reproduce the original deadlock scenario with concurrent test cases in an environment close to production, confirm that no cycle appears after the fix, and observe whether throughput and response time degrade noticeably.

    Common Pitfalls

    First, treating a deadlock as an occasional failure and relying on retries. Retrying masks the symptom, but as long as the lock ordering is not unified, similar deadlocks will reappear under high concurrency.

    Second, looking only at the last record in the report. Databases usually retain only the transaction that was ultimately rolled back and partial lock information; complete diagnosis requires combining application logs and code paths.

    Third, having the model directly generate fix code and merge it. The model’s reasoning may overlook implicit business constraints, such as a branch that runs only under specific conditions. Code review and testing are irreplaceable steps.

    A Reusable Troubleshooting Checklist

    1. Collect the complete log and confirm the time range and the number of threads involved;
    2. Build a held-and-waited table by thread, with lock objects in a separate table;
    3. Draw a directed graph from the wait relationships and find the closed loop;
    4. Annotate each lock on the cycle with its business operation and compare the lock ordering across paths;
    5. Go back to the code to confirm the location and order of the lock statements;
    6. Choose a fix direction and reproduce and verify it in a test environment;
    7. Write the unified lock ordering into the team’s development standards and check for it during code review.

    The value of deadlock troubleshooting lies in turning messy logs into a structure that can be verified item by item. Once the structure is clear, the root cause is often right in front of you.