The bug report said: "Sometimes the stock reservation job fails with No query results for model [App\Models\Order] ." Sometimes. Not in local, not in staging, not in the test suite. Only in production, only under load, and only for a few orders a day.
The order was there when we looked. The job had still failed. Both things were true, and the reason was one line inside a database transaction.
If you dispatch queued work from inside DB::transaction() in Laravel, this article is for you. We will look at why the worker can run before your data exists, the tools Laravel gives you to fix it (there are more than people think), and the pitfalls that remain after the fix.
The problem: your worker cannot see uncommitted rows
Here is a simplified version of the code.…