To use this site please enable javascript on your browser! Multiple Queues of the Same Connection in Laravel

We use cookies, as well as those from third parties, for sessions in order to make the navigation of our website easy and safe for our users. We also use cookies to obtain statistical data about the navigation of the users.

See Terms & Conditions

Multiple Queues of the Same Connection in Laravel

by Bryce Andy 07:10 Oct 06 '21

Have you ever wondered how you could set up multiple queues using the same connection? Let's figure out why and how.

Multiple Queues
Illustration from

Why Use Multiple Connections for Laravel Queues?

Sometimes your application needs to process different jobs with a higher priority than others. To do this, Laravel enables you to specify queue names and give them priorities as you wish.

Imagine you have jobs like SendVerificationCode, BroadcastWeeklyNewsletter and ProcessPayment. You obviously don't want jobs that process payments to wait until newsletter jobs are complete.

How to Define Multiple Queues to Prioritize Jobs

First, you should know which connection you intend to use for all of these queues. Personally I am more acustomed to database or Amazon sqs connections:

// config/queue.php

'database' => [
    'driver' => 'database',
    'table' => 'jobs',
    'queue' => 'default',
    'retry_after' => 90,

And I will obviously define in the .env file that I will be using the database connection.

Now, whenever we dispatch any of the jobs, they will consume the default queue as indicated in the configuration above.

But here is the order of priority we want our imaginary jobs to be processed:

  • Highest priority - ProcessPayment
  • Medium priority - SendVerificationCode
  • Low (or default) priority - BroadcastWeeklyNewsletter

Then when we dispatch the jobs, we will define the queue names explicitly

use App\Jobs\{

// This job is sent to the connection's default queue...

// This job is sent to the connection's "payments" queue...

// This job is sent to the connection's "verification" queue...

Lastly we need to tell the workers the order of priority from first to last:

php artisan queue:listen database --queue=payments,verification,default

That's all. Now all your queue workers will process all payment jobs first before all the other jobs.

Similarly, all code verification jobs will be processed ahead of the newsletter jobs.

Updated 06:01 Jan 07 '22

If you like this content, please consider buying me coffee.
Thank you for your support!

Become a Patron!