Laravel Horizon: Powerful Redis Queue Monitoring and Management in PHP
\n\nIn modern PHP web development, asynchronous task processing through queues has become the de facto standard. Laravel provides built-in queue support, but when a project grows, managing dozens of workers, tracking their performance, and diagnosing issues becomes difficult. This is where Laravel Horizon comes to the rescue — a powerful tool for monitoring and managing Redis queues.
\n\nHorizon is not just a monitoring dashboard. It is a full-fledged queue management system that provides a beautiful web interface for tracking all aspects of queue operation: number of processed jobs, execution time, number of errors, worker status, and much more. Unlike the standard approach where you manually start workers, Horizon manages them automatically, distributing load across multiple processes and balancing them depending on the current workload.
\n\nThe key advantage of Horizon is its integration with Redis. It uses Redis not only as a queue driver but also as a storage for metrics and worker state. This makes it incredibly fast and scalable. You can run Horizon on multiple servers, and it will collect data from all workers into a single control panel. This is the ideal solution for a production environment where every millisecond matters.
\n\nInstallation
\n\nHorizon installation is done via Composer — the standard package manager for PHP. Make sure you have Composer installed and your project uses Laravel 5.5+.
\n\ncomposer require laravel/horizon\n\nAfter installing the package, you need to publish its configuration files and assets:
\n\nphp artisan horizon:install\n\nThis command will create the configuration file config/horizon.php and register the Horizon service provider. It will also create a routes file routes/horizon.php if you are using Laravel 8+.
Next, you need to add the provider to config/app.php (if it was not added automatically):
'providers' => [\n // Other providers...\n App\\Providers\\HorizonServiceProvider::class,\n],\n\nAfter that, you need to run migrations to create the tables required for Horizon to work (although the main logic uses Redis, some data is stored in the database):
\n\nphp artisan migrate\n\nFinally, you need to configure Redis. Make sure you have the Redis server installed and running, and that the correct connection parameters are specified in the .env file:
REDIS_HOST=127.0.0.1\nREDIS_PASSWORD=null\nREDIS_PORT=6379\n\nQuick Start: Minimal Working Example
\n\nAfter installation and configuration, you can immediately launch Horizon and see it in action.
\n\nStep 1: Start Horizon
\n\nIn the terminal, run the command:
\n\nphp artisan horizon\n\nYou will see Horizon start several worker processes. By default, it creates three processes to handle jobs from the default queue.
Step 2: Create a Queue Job
\n\nLet's create a simple job that simulates a long-running operation:
\n\n<?php\n\nnamespace App\\Jobs;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Foundation\\Bus\\Dispatchable;\nuse Illuminate\\Queue\\InteractsWithQueue;\nuse Illuminate\\Queue\\SerializesModels;\n\nclass ProcessPodcast implements ShouldQueue\n{\n use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;\n\n protected $podcastId;\n\n public function __constru