Menu

Laravel Queues and Database Transactions: Why Your Job Ran Before the Order Existed
📰

Laravel Queues and Database Transactions: Why Your Job Ran Before the Order Existed

DEV Community: laravel·Tuğrul Yıldırım·8 days ago
#f51sj0RX
#devtolaravel#devto#dev#order#transaction
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.