Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adpxl's avatar
Level 1

MySql Deadlock on Cache Database

In my application sometimes it throws error

SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction (SQL: delete from cache where key = app_cachefd132f0b54b29ef64581153ac64a2d5d9109e1dc:timer)

How to solve it?

0 likes
3 replies
Tray2's avatar

This is a common issue when using transactions.

User 1 selects row 1 for update and for some reason don't finish the transaction or maybe it selects thousands of records then user 2 tries to select the same record then you'll get this error.

There are a few things you can try.

  1. Add nowait skip locked to the select for update and that record will be skipped. (MySQL 8)

  2. Add a try catch and if the record is locked sleep a second or two then try again. Then you can make three tries and if it still is locked then raise an exception.

  3. Decrease the amount of records for each commit so the locked time decreases

Deadlocks will always occur in a relational database.

adpxl's avatar
Level 1

Yes that is the cause, but how to do it? Because in my case this is happen in the Laravel cache database. It is not in my custom database.

Please or to participate in this conversation.