Master Laravel queue worker prioritization by implementing named queues. Learn to isolate critical tasks from background jobs for a scalable, responsive system.
Previously in this course, we explored Eloquent Caching Strategies to reduce database load. While caching improves read performance, background processing is the backbone of any high-traffic SaaS. Today, we advance our architecture by implementing Queue Worker Prioritization to ensure your most important domain events aren't stuck behind a massive batch of low-priority emails or report exports.
By default, Laravel processes jobs in a FIFO (First-In, First-Out) manner on a single default queue. In a production SaaS, this is a recipe for disaster. If a user triggers a batch job that generates 5,000 PDF reports, your critical "Send Password Reset Email" or "Process Payment Callback" jobs might sit in the queue for minutes.
To achieve a production-grade architecture, you must move from a single-queue mindset to a tiered queue hierarchy.
Instead of dispatching every job to the default pipe, categorize your jobs based on their business impact. We typically categorize them into three tiers:
You assign a queue to a job using the onQueue() method during dispatch or by defining it in the job class:
PHP#6A9955">// Dispatching with explicit priority ProcessPayment::dispatch($order)->onQueue('high'); #6A9955">// Or defining it within the Job class class GenerateMonthlyReport implements ShouldQueue { public $queue = 'low'; }
Defining queues is only half the battle. You must instruct your queue:work processes to consume these queues in a specific order. When you start a worker, the order of the queue names matters significantly.
Run the following command on your production server:
Bashphp artisan queue:work --queue=high,default,low
In this configuration, the worker will always check the high queue first. It will only move to the default queue if high is empty. If a new job arrives in high while the worker is busy with a default job, the worker will finish the current job and immediately jump back to the high queue.
| Priority | Queue Name | Examples |
|---|---|---|
| P0 (Critical) | high | Payments, Auth, Security tokens |
| P1 (Standard) | default | Notifications, Webhooks, Emails |
| P2 (Background) | low | Analytics, CSV exports, Data pruning |
In our running project, we need to ensure that when a user upgrades their plan, the billing sync is instant, while the "Welcome Email" series can wait a few seconds.
1. Define the Job
PHPnamespace App\Jobs\Billing; class SyncSubscriptionStatus implements ShouldQueue { public $queue = 'high'; #6A9955">// Always process billing syncs first public function handle() { #6A9955">/* ... */ } }
2. Deploy Specialized Workers On a high-traffic system, you shouldn't just rely on one worker command. Use a process manager like Supervisor to run multiple workers with different configurations:
INI; /etc/supervisor/conf.d/laravel-worker.conf [program:laravel-worker-high] command=php /var/www/html/artisan queue:work --queue=high --processes=2 [program:laravel-worker-default] command=php /var/www/html/artisan queue:work --queue=default,low --processes=4
By dedicating specific processes to the high queue, you guarantee that even if the low queue is flooded with millions of rows of analytics, your billing syncs remain isolated and performant.
default queue.high, default, and low tiers.docker-compose.yml or supervisor config to start a worker that prioritizes the high queue.low priority job, then a high priority job, and observing the logs to confirm the high one executes first.high queue that is constantly full, your low queue jobs will never run. Monitor your queue depths using tools like Advanced Queue Monitoring: Mastering Laravel Horizon.php artisan queue:work command. The worker will continue to listen only to the default queue, effectively ignoring your new high and low queues.Queue prioritization is about resource isolation. By partitioning your workload into named queues and configuring worker priority, you prevent background noise from degrading the user experience of your critical domain services.
Up next, we will handle Unique Job Patterns to prevent race conditions and duplicate processing for those critical domain events.
Master scalable file uploads in Laravel. Learn to stream directly to S3 and process heavy files asynchronously to keep your application fast and memory-efficient.
Read moreScale your Laravel infrastructure by moving beyond local Redis queues. Learn to integrate SQS or RabbitMQ for high-scale, durable message processing.
Queue Worker Prioritization
Custom Middleware Development
Database Connection Pooling
Handling Large Data Exports
Security Header Configuration
Database Sharding Concepts
Real-time Data Synchronization
Database Deadlock Prevention
Managing Third-Party API Integrations