Laravel Horizon — Redis Queue Monitoring and Management
In this tutorial, you will learn about Laravel Horizon. We cover key concepts, practical examples, and best practices to help you master this topic.
Laravel Horizon provides a beautiful dashboard and configuration system for Redis queues, offering metrics, failed job management, and automatic worker scaling.
What You'll Learn
By the end of this tutorial, you'll install Horizon, configure queue workers, monitor queues, set up notifications, deploy Horizon, and scale workers automatically.
Why Horizon Matters
Horizon simplifies Redis queue management with a real-time dashboard, job metrics, and configuration per queue. It replaces manual worker management with visual monitoring.
Real-World Use
A SaaS platform uses Horizon to manage 5 queue tiers (high, default, low, emails, reports). Horizon auto-scales workers and sends Slack notifications when queues grow.
Horizon Path
flowchart LR
A[Laravel Framework] --> B[Redis Queue]
B --> C[Horizon]
C --> D[Dashboard]
C --> E[Auto-Scaling]
C --> F[Notifications]
C --> G{You Are Here}
style G fill:#f90,color:#fff
Installation and Setup
Install Horizon and publish assets.
composer require laravel/horizon
php artisan horizon:install
php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
<?php
// config/horizon.php
"environments" => [
"production" => [
"supervisor-1" => [
"connection" => "redis",
"queue" => ["high", "default", "low"],
"balance" => "simple",
"minProcesses" => 1,
"maxProcesses" => 10,
"tries" => 3,
"timeout" => 60,
"nice" => 0,
],
"supervisor-2" => [
"connection" => "redis",
"queue" => ["emails"],
"balance" => "simple",
"minProcesses" => 1,
"maxProcesses" => 5,
"tries" => 1,
],
],
"local" => [
"supervisor-1" => [
"connection" => "redis",
"queue" => ["default"],
"balance" => "simple",
"processes" => 3,
"tries" => 3,
],
],
],
Running Horizon
Start and manage Horizon.
# Start Horizon (replaces queue:work)
php artisan horizon
# Pause queue processing
php artisan horizon:pause
# Resume processing
php artisan horizon:continue
# Check status
php artisan horizon:status
# Clear stalled jobs
php artisan horizon:clear
Dashboard Authorization
Secure the Horizon dashboard.
<?php
// In App\Providers\HorizonServiceProvider
use Laravel\Horizon\Horizon;
protected function gate(): void {
Gate::define("viewHorizon", function ($user) {
return in_array($user->email, [
"admin@example.com",
]);
});
}
Notifications
Configure queue alerts.
<?php
// config/horizon.php
"waits" => [
"redis:default" => 60,
"redis:high" => 30,
"redis:emails" => 120,
],
// Slack notification
Horizon::routeMailNotificationsTo("admin@example.com");
Horizon::routeSlackNotificationsTo("#ops", env("SLACK_HOOK_URL"));
Horizon::routeSmsNotificationsTo("+15551234567");
Common Mistakes
1. Not Securing Horizon Dashboard
Horizon routes must be restricted with authentication gates. Unauthorized access exposes queue data.
2. Over-Minimizing Min/Max Processes
Setting minProcesses too low causes backlog during surges. Set based on expected load.
3. Ignoring Wait Time Metrics
Long wait times indicate insufficient workers. Monitor the waits configuration.
4. Running Horizon Without Supervisor
Horizon crashes need automatic restart. Use Supervisor or systemd for Process management.
5. Not Configuring Queue-Specific Tries
Different queues need different retry policies. Configure per supervisor in horizon.php.
Practice Questions
1. What does Horizon replace?
Horizon replaces manual queue:work processes with a supervisor-managed system.
2. How does auto-scaling work?
Balance strategies (simple, auto, false) control how workers distribute across queues.
3. How do you authorize Horizon access?
Define a viewHorizon gate in HorizonServiceProvider.
4. What are wait time notifications?
Alerts triggered when a queue wait time exceeds the configured threshold.
5. Challenge: Configure Horizon for multi-tier queue processing.
<?php
"production" => [
"supervisor-critical" => [
"connection" => "redis",
"queue" => ["critical"],
"balance" => "auto",
"minProcesses" => 2,
"maxProcesses" => 20,
"tries" => 5,
],
"supervisor-bulk" => [
"connection" => "redis",
"queue" => ["bulk"],
"balance" => "simple",
"minProcesses" => 1,
"maxProcesses" => 3,
"tries" => 1,
"timeout" => 600,
],
],
FAQ
Mini Project: Horizon Production Setup
Complete Horizon production configuration.
<?php
"production" => [
"supervisor-web" => [
"connection" => "redis",
"queue" => ["high", "default"],
"balance" => "auto",
"minProcesses" => 2,
"maxProcesses" => 8,
"tries" => 3,
"timeout" => 60,
],
"supervisor-worker" => [
"connection" => "redis",
"queue" => ["low", "bulk"],
"balance" => "auto",
"minProcesses" => 1,
"maxProcesses" => 4,
"tries" => 1,
"timeout" => 300,
],
],
What's Next
Laravel Broadcasting Laravel Queues Deep Laravel Notifications
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro