The error “SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction” appears in var/log/exception.log on many busy Magento stores. Two requests each held a row the other needed. MySQL cancelled one of them, and the customer behind that request got an error, often while placing an order or updating a cart.

Why it only happens on the live store

Deadlocks need two requests touching the same rows at the same moment. That almost never happens in testing, and happens regularly under real checkout traffic.

Read the deadlock report

SHOW ENGINE INNODB STATUS\G

Find the LATEST DETECTED DEADLOCK section. It shows both transactions, their queries and the rows they were waiting for.

Is it a problem?

A single deadlock is normal. MySQL recovers immediately. What matters is whether it repeats. Check again later and see whether the timestamp has moved, and count how often the error appears in the logs.

Fixing repeated deadlocks

  • Order placement: turn on asynchronous order grid indexing, so saving an order no longer writes to the admin grid in the same transaction (see asynchronous grid indexing):
    bin/magento config:set dev/grid/async_indexing 1
  • A custom module named in the report: change it to update fewer rows, or always lock rows in the same order.
  • Indexers on “Update on Save” during busy hours: switch them to “Update by Schedule”.

Catch it automatically

MageMonitor checks for this every few minutes as part of its Database checks, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.

Related fixes