Laravel SQLSTATE lock wait timeout exceeded, why does this happen?

Asked by Abdullah A. · · Viewed 95 times

6

We randomly get 'Lock wait timeout exceeded' errors in Laravel under load but it's not consistent at all, sometimes it's fine for days. What's actually causing this?

laravelmysqldatabasedebugging

3 Answers

  • 6

    ✓ Accepted answer

    Usually it's a transaction holding a row lock longer than it should, could be a queued job doing a bunch of work inside DB::transaction, or two processes trying to update the exact same row at the same time. Keep transactions as short as possible and make sure you're not running some slow report query inside the same transaction as a write, that's a really common cause.

    Afrixh TeamTeam

  • 2

    We had this exact problem, turned out to be a queued job updating the same counters row from multiple workers at once. Switched to an atomic increment() instead of read then write and it went away completely.

    Brian W.

  • 1

    Yeah also check if a long running report is sitting inside the same transaction as a write somewhere, we found one buried pretty deep in our code.

    Ayesha N.

Your answer