Database Queue driver for Laravel with optimistic locking
Laravel database queue implementation using optimistic locking. Increases concurrency, eliminates deadlocks.
- Install this package via composer using:
composer require ph4r05/laravel-queue-database-ph4
- Create job table
php artisan queue_ph4:table php artisan migrate
- Queue configuration
<?php // config/queue.php return [ 'database_ph4' => [ 'driver' => 'database_ph4', 'table' => 'jobs_ph4', 'queue' => 'default', 'retry_after' => 4, 'num_workers' => 1, 'window_strategy' => 1, ], ];
num_workersshould correspond to the number of workers processing jobs in the queue.
- 0 means worker selects one available job for processing. Smaller throughput, job ordering is preserved as with pessimistic locking.
- 1 means workers will select
num_workersnext available jobs and picks one at random. Higher throughput with slight job reordering (for more info please refer to the blog)
To use the optimistic locking you can now change your
Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues
Run the tests with:
Blog post about optimistic locking
- No need for explicit transactions. Single query auto-commit transactions are OK.
- No DB level locking, thus no deadlocks. Works also with databases without deadlock detection (older MySQL).
- Job executed exactly once (as opposed to pessimistic default DB locking)
- High throughput.
- Tested with MySQL, PostgreSQL, Sqlite.
- Job ordering can be slightly shifted with multiple workers (reordering 0-70 in 10 000 jobs)
You can contribute to this package by discovering bugs and opening issues. Please, add to which version of package you create pull request or issue.
Thank you for all your support!