Each of the queue drivers supported by Laravel has its pros and cons, and sometimes you may want to switch between drivers to accommodate business needs.
In this post we're going to look into the possible ways to switch between drivers.
Stopping the World
One way of doing this—and it's the most popular in the forum threads—is to put the application in maintenance mode, wait for the queue to be emptied, deploy the new driver, and then bring the application up.
While in maintenance mode, a Laravel application will return a 503 response on every request, preventing visitors from taking any action that could enqueue more jobs.
You should also disable the scheduler—by deactivating the CRON job—if any of your scheduled commands enqueue jobs.
Finally, you should start your workers with the
--force option so they continue processing jobs while the application is in maintenance mode. Otherwise, they'll just pause and wait for the application to be back up.
With this approach, your application will have a downtime until the workers empty the queue. You're basically going to stop the world for a bit.
Another approach is to follow these steps:
- Configure a new queue connection in your queue.php configuration file for the new driver.
- Update your code to push new jobs to that connection.
- Start new workers to process jobs from the new connection.
- Wait until old workers finish processing all jobs still stored in the old connection and then stop those workers.
👋 The graceful approach in this post is part of a book I recently published "Laravel Queues in Action". Check it out for a crash course, a cookbook, a guide, and a reference.
For example, if you're switching from the
database queue connection to the
redis connection. You need to update your code to push new jobs on the
Then start a few workers to process jobs from the
redis connection so end-users don't experience too much delay:
php artisan queue:work redis --queue=invoices --timeout=30
Finally, you can use
Queue:size() to find the number of pending jobs in each queue:
When all queues under the
database connection are empty, you can stop the old workers to make room for adding more workers that process jobs from the new connection.
That way you can switch between drivers without taking your application down or stopping your users' world.