Laravel 2 min read

Two updates, one order: where a Laravel deadlock starts

Trace a Laravel database deadlock back to competing writes, then test lock order and safe retries without duplicating side effects.

Two updates, one order: where a Laravel deadlock starts

Imagine a dispatch screen updates an order and then its stock row. A background import updates the same stock row and then the order. If both hold their first lock while waiting for the other, the database can abort one transaction. That’s the pair of writes you need to find.

Get both sides of the collision

Start with the actual database error and its timestamp. Match it to the request or queue job, then check the database’s deadlock diagnostics for the queries and locks involved. A slow query, a lock wait timeout and a deadlock can feel identical to the person pressing Save. The logs tell you which you’re dealing with.

Look at lock order and transaction length

Check whether both paths can touch shared rows in the same order. Keep unrelated work outside the transaction where possible. Look at the query plan if an update appears to lock more rows than expected. Verify the changes against a reproduction with two concurrent requests or jobs.

Check what would run twice

Laravel supports retry attempts on a database transaction. Before adding them, read the whole callback. A database rollback can’t undo an email or a request to a payment API. Separate external effects from the retried transaction and make those effects safe to repeat where needed. Test the final order and stock state as well as the response.

If saves fail intermittently, send me the database error and what was running at the same time.

Need a hand with this?

Work directly with me on an existing Laravel application, from a focused assessment to ongoing development.

Talk directly to Adam

Share this article

Back to Blog