All articles

MySQL

Ending Deadlock Loops on MySQL: The Ordering Fix

Oct 1, 2026 deadlocks · locking · innodb · transactions

Deadlocks are ordering problems wearing an error costume. Here is how to read the lock graph and re-sequence the fight away.

When the same deadlock reappears every week, the retry loop is not a solution — it is an anesthetic. The transactions are fighting over the same rows in a different order, and InnoDB resolves the fight by killing one side.

The durable fix is access-order discipline, and it usually lands in a single commit.

Decode the lock graph first

SHOW ENGINE INNODB STATUS plus the data_locks tables tell you exactly which statement held what and which one waited. Map it: transaction A locks row 1 then row 2; transaction B locks row 2 then row 1. That circular wait is your deadlock — drawn, not guessed.

One ordering, everywhere

Pick a global order for lock acquisition — by primary key, by table, whatever rule fits — and enforce it in every transaction that touches those resources. Shorten critical sections while you are there: commit early, defer external calls out of transactions. Deadlocks do not survive consistent ordering.

Tags

deadlocks locking innodb transactions

Related articles

Work with Burj

Your database deserves engineering, not guesswork.

Tell us what is slow — CPU, queries, migrations, or cost — and we will scope the smallest safe fix.

Plan an engagement