Skip to content

Detecting InnoDB deadlocks on a cPanel host before customers notice

InnoDB detects and rolls back deadlocks automatically, but the rollback surfaces as an application error. Capturing the deadlock details turns a recurring problem into a fixable one.

A WooCommerce site occasionally fails an order at checkout. The error log shows a 500 and “Deadlock found when trying to get lock; try restarting transaction.” InnoDB detected two transactions waiting on each other’s locks, picked one to roll back, and returned an error. The application doesn’t retry. The customer thinks the order was placed; the database thinks it wasn’t. From the database side, the deadlock is normal — InnoDB is doing its job. From the customer’s side, this is a recurring incident, and fixing it requires capturing what happened, when, and which queries collided.

What’s happening

A deadlock occurs when two transactions hold locks that the other needs. InnoDB’s deadlock detector identifies the cycle, picks the “cheaper” transaction (one with fewer locks held, less work done), and rolls it back. The other transaction proceeds. The rolled-back client gets ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction.

By default, InnoDB only logs the most recent deadlock in SHOW ENGINE INNODB STATUS. Earlier deadlocks are lost. To see history, you need either:

  • innodb_print_all_deadlocks = ON — logs every deadlock to the error log.
  • An external collector that snapshots SHOW ENGINE INNODB STATUS periodically.

Without one of these, you can’t reproduce or fix the underlying query pattern — the next deadlock just looks like the first.

How to verify

mysql -e "SHOW VARIABLES LIKE 'innodb_print_all_deadlocks'"
mysql -e "SHOW VARIABLES LIKE 'innodb_lock_wait_timeout'"
mysql -e "SHOW ENGINE INNODB STATUS\G" | sed -n '/LATEST DETECTED DEADLOCK/,/^-/p'
grep -i deadlock /var/log/mysql/error.log | tail -20
mysql -e "SHOW GLOBAL STATUS LIKE 'Innodb_deadlocks'"  # MariaDB only

The LATEST DETECTED DEADLOCK section shows the two transactions, their queries, the locks each held, and which got rolled back. The error log (with innodb_print_all_deadlocks on) gives history. The status counter (MariaDB only) shows total deadlocks since last restart — sudden increases indicate a new pattern.

The fix

  1. Enable deadlock logging persistently:
    /etc/my.cnf.d/cpanel.cnf:
    [mysqld]
    innodb_print_all_deadlocks = ON
    Restart MySQL. Now every deadlock is in /var/log/mysql/error.log.
  2. For a deadlock you’ve captured, read the LATEST DETECTED DEADLOCK section:
    • Two TRANSACTION blocks, labeled (1) and (2).
    • Each shows HOLDS THE LOCK(S) (what it has) and WAITING FOR THIS LOCK TO BE GRANTED (what it needs).
    • WE ROLL BACK TRANSACTION (N) says which was rolled back.
  3. Identify the collision pattern. Common patterns:
    • Two UPDATEs hitting the same rows in different order: order them consistently in code.
    • An INSERT colliding with another INSERT on a unique index: use INSERT IGNORE or a UPSERT pattern.
    • A SELECT FOR UPDATE chain reads in different order across transactions: enforce a canonical order (ORDER BY id).
    • A FK check during INSERT colliding with the parent table’s update: a known pattern, harder to avoid; often acceptable to retry.
  4. The application should retry deadlock errors. A deadlock isn’t a bug — it’s a sign the database protected itself. Idempotent retries (with exponential backoff) handle it transparently. WordPress’s WC_Order_Factory does this; many custom checkouts don’t.
  5. Reduce deadlock window with shorter transactions. Long transactions hold locks longer and are more likely to be involved in deadlocks. If your transaction is “begin; update 50 things; commit”, split it into smaller commits where business logic allows.
  6. For “deadlocks but I can’t change the application”, add an index. Most deadlocks involve table scans or range locks. An index that turns a range scan into a single-row lookup eliminates the lock surface area. EXPLAIN the involved queries; if you see range or ALL in type, add the index.
  7. Monitor the rate. A deadlock every hour during normal traffic is fine. Deadlocks every minute mean the pattern is hot — fix it.
  8. For lock-wait timeouts (a slower cousin of deadlock), innodb_lock_wait_timeout = 50 is the default — connections wait up to 50 seconds for a lock. Raise to 120 if your application gracefully retries; lower to 10 if you want to fail faster and retry quickly.

For environments where deadlock patterns recur and the application can’t be changed quickly, Stack Harbor’s environment management team correlates the deadlock log with the application traffic pattern and proposes index or transaction-order changes that close the loop.