Laravel Queues in Action (2nd edition) is now available!

Switching Between Queue Drivers in Laravel Without Stopping the World

Updated: Sep 16, 2020 — 1 min Read#queues

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.

Gracefully Switching

Another approach is to follow these steps:

  1. Configure a new queue connection in your queue.php configuration file for the new driver.
  2. Update your code to push new jobs to that connection.
  3. Start new workers to process jobs from the new connection.
  4. 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 redis connection:

NewJob::dispatch()->onConnection('redis');

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:

Queue::connection('database')->size('invoices')

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.

Hey! 👋 If you find this content useful, consider sponsoring me on GitHub.

You can also follow me on Twitter, I regularly post about all things Laravel including my latest video tutorials and blog posts.

By Mohamed Said

Hello! I'm a former Laravel core team member & VP of Engineering at Foodics. In this publication, I share everything I know about Laravel's core, packages, and tools.

You can find me on Twitter and Github.

This site was built using Wink. Follow the RSS Feed.