Laravel Queues: DB vs Redis vs Horizon Explained
Laravel Queues allow background processing of tasks like emails, notifications, and image processing. Choosing the right driver depends on performance needs and project scale.
Step 1: Enable queues in.env by setting: QUEUE_CONNECTION=database or redisStep 2: Database Driver
Stores jobs in a
jobs table. Easy to set up and works on shared hosting.Step 3: Create jobs table with:
php artisan queue:table && php artisan migrateStep 4: Redis Driver
Much faster than DB. Ideal for real-time or high-volume jobs. Requires Redis server installed.
Step 5: Install Redis via:
composer require predis/predis or use PHP extension.Step 6: Queue a job using:
dispatch(new SendEmailJob($user))Step 7: Start the worker:
php artisan queue:workStep 8: Horizon is Laravel's dashboard for Redis queues. Offers monitoring, retry, stats, and auto-scaling.
Step 9: Install Horizon:
composer require laravel/horizonStep 10: Publish config:
php artisan horizon:install and run php artisan horizonStep 11: Access dashboard at:
/horizon (setup route/middleware)Step 12: Horizon provides worker monitoring, failed jobs, retry buttons, and tagging support.
Step 13: For production, run Horizon via:
php artisan horizon with process monitor like Supervisor.Step 14: Use DB for small apps. Redis for speed. Horizon for full visibility and control.
Step 15: Always monitor failed jobs using:
php artisan queue:failed and queue:retry