# queue-jobs-management > Laravel queue and job management best practices. Use when implementing background tasks, email processing, or asynchronous operations. - Author: tuna - Repository: TunahanAldmeir/laraxpose - Version: 20251220113406 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-07 - Source: https://github.com/TunahanAldmeir/laraxpose - Web: https://mule.run/skillshub/@@TunahanAldmeir/laraxpose~queue-jobs-management:20251220113406 --- --- name: queue-jobs-management description: Laravel queue and job management best practices. Use when implementing background tasks, email processing, or asynchronous operations. --- # Queue & Jobs Management Expert guidance on Laravel queues for asynchronous processing. ## When to Use Queues ✅ **Use queues for:** - Sending emails - Processing uploads (images, videos) - External API calls - Report generation - Data imports/exports ❌ **DON'T queue:** - Simple database inserts - Real-time operations - Critical user-facing actions ## Basic Job Structure ```php user->email) ->send(new WelcomeEmail($this->user)); } public function failed(Throwable $exception): void { // Log failure Log::error("Welcome email failed: {$this->user->email}"); } } ``` ## Dispatching Jobs ### Immediate Dispatch ```php SendWelcomeEmail::dispatch($user); ``` ### Delayed Dispatch ```php // Send after 10 minutes SendWelcomeEmail::dispatch($user) ->delay(now()->addMinutes(10)); ``` ### After Database Commit ```php // Only dispatch if transaction succeeds SendWelcomeEmail::dispatch($user) ->afterCommit(); ``` ## Queue Priority ### Multiple Queues ```php // High priority SendUrgentNotification::dispatch($user) ->onQueue('high'); // Low priority GenerateMonthlyReport::dispatch() ->onQueue('low'); ``` ### Worker Configuration ```bash # Process high queue first, then default, then low php artisan queue:work --queue=high,default,low ``` ## Error Handling ### Retry Strategy ```php class ProcessPayment implements ShouldQueue { public $tries = 5; public $backoff = [60, 120, 300]; // Wait 1m, 2m, 5m public function retryUntil(): DateTime { return now()->addHours(24); // Give up after 24h } } ``` ### Failed Jobs ```bash # View failed jobs php artisan queue:failed # Retry failed job php artisan queue:retry {id} # Retry all php artisan queue:retry all ``` ## Best Practices ### 1. Pass IDs, Not Models ```php // ❌ BAD: Model might be stale public function __construct(public User $user) {} // ✅ GOOD: Fetch fresh data public function __construct(public int $userId) {} public function handle() { $user = User::findOrFail($this->userId); } ``` ### 2. Use Supervisor in Production ```ini [program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /path/to/artisan queue:work redis --sleep=3 --tries=3 autostart=true autorestart=true user=www-data numprocs=4 redirect_stderr=true stdout_logfile=/path/to/worker.log ``` ### 3. Monitor with Horizon (Redis) ```bash composer require laravel/horizon php artisan horizon:install php artisan horizon ``` ### 4. Rate Limiting ```php use Illuminate\Queue\Middleware\RateLimited; public function middleware(): array { return [new RateLimited('api-calls')]; } ``` ## Critical Checklist ✅ Set retry count and timeout ✅ Use `afterCommit()` for transactional jobs ✅ Implement `failed()` method for logging ✅ Use Supervisor to keep workers running ✅ Monitor failed jobs regularly ✅ Use Redis for production queues ❌ Never use `sleep()` inside jobs ❌ Don't pass entire models, use IDs