Navigation

Laravel

How to Prioritize Jobs on a Queue in Laravel

Learn Laravel queue prioritization with onQueue() method. Process high-priority jobs first using named queues and queue workers for better performance.

Table Of Contents

How to Prioritize Jobs on a Queue in Laravel

Problem: Your Laravel application processes various jobs, but some are more time-sensitive than others. For example, a password reset email must be sent immediately, while a weekly newsletter can wait. You need a way to process high-priority jobs before low-priority ones.

Solution: The simplest way to prioritize jobs is by creating separate queues and telling your queue worker to process them in a specific order.

First, dispatch jobs to named queues using the onQueue() method:

// Dispatch a critical job to the 'high' queue
ProcessPayment::dispatch($payment)->onQueue('high');

// A regular job can go to the 'default' queue
SendWelcomeEmail::dispatch($user); // No onQueue() needed

// A less important job goes to the 'low' queue
GenerateAnalyticsReport::dispatch()->onQueue('low');

Next, run the queue worker with a comma-separated list of queues, in order of priority:

php artisan queue:work --queue=high,default,low

How It Works

When you provide a prioritized list of queues to the queue:work command, the worker will always check for jobs in that specific order.

  1. It first looks for jobs in the high queue. It will continue processing jobs from this queue until it's completely empty.
  2. Only when the high queue is empty will the worker move on to check the default queue.
  3. Finally, if both high and default queues are empty, it will start processing jobs from the low queue.

This strategy ensures that your most critical tasks are always handled first, without being delayed by a large number of less important jobs. You can name your queues anything you like and add as many as you need to fit your application's logic.

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Laravel