Laravel Horizon for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Laravel Horizon: Powerful Redis Queue Monitoring and Management in PHP

\n\n

In 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\n

Horizon 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\n

The 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\n

Installation

\n\n

Horizon 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\n
composer require laravel/horizon
\n\n

After installing the package, you need to publish its configuration files and assets:

\n\n
php artisan horizon:install
\n\n

This 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+.

\n\n

Next, you need to add the provider to config/app.php (if it was not added automatically):

\n\n
'providers' => [\n    // Other providers...\n    App\\Providers\\HorizonServiceProvider::class,\n],
\n\n

After 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\n
php artisan migrate
\n\n

Finally, 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:

\n\n
REDIS_HOST=127.0.0.1\nREDIS_PASSWORD=null\nREDIS_PORT=6379
\n\n

Quick Start: Minimal Working Example

\n\n

After installation and configuration, you can immediately launch Horizon and see it in action.

\n\n

Step 1: Start Horizon

\n\n

In the terminal, run the command:

\n\n
php artisan horizon
\n\n

You will see Horizon start several worker processes. By default, it creates three processes to handle jobs from the default queue.

\n\n

Step 2: Create a Queue Job

\n\n

Let'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                    

Recommendations