Laravel Horizon is a powerful tool for managing and monitoring Laravel's Redis queues. It provides a beautiful dashboard and code-driven configuration, allowing developers to easily monitor key metrics of their queue system. In this post, we'll explore how to set up Horizon, use its features, and leverage it for efficient queue management.
Getting Started with Laravel Horizon
First, install Horizon via Composer:
composer require laravel/horizon
After installation, publish Horizon's assets:
php artisan horizon:install
Configuring Horizon
Horizon's configuration file is located at config/horizon.php
. Here's an example configuration:
'environments' => [
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 10,
'tries' => 3,
],
],
'local' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default', 'high', 'low'],
'balance' => 'auto',
'processes' => 3,
'tries' => 3,
],
],
],
This configuration sets up different queue processing strategies for production and local environments.
Running Horizon
To start Horizon, run:
php artisan horizon
In production, you should use a process monitor like Supervisor to ensure Horizon runs continuously.
Dispatching Jobs
With Horizon configured, you can dispatch jobs as usual:
use App\Jobs\ProcessPodcast;
ProcessPodcast::dispatch($podcast);
To specify a queue, use the onQueue
method:
ProcessPodcast::dispatch($podcast)->onQueue('high');
Monitoring with the Horizon Dashboard
Access the Horizon dashboard at /horizon
. Here, you can monitor job throughput, runtime, and failures.
Suggested Usages
Email Queues: Process email sending in the background to improve application responsiveness.
Mail::to($user)->queue(new WelcomeEmail($user));
Data Processing: Handle large data processing tasks asynchronously.
ProcessLargeDataset::dispatch($dataset)->onQueue('low');
API Integrations: Manage external API calls without blocking user requests.
SyncWithExternalAPI::dispatch()->onQueue('api');
Scheduled Tasks: Use Horizon to monitor scheduled tasks executed via Laravel's scheduler.
// In App\Console\Kernel.php protected function schedule(Schedule $schedule) { $schedule->job(new GenerateReports)->daily(); }
Failed Job Handling: Utilize Horizon's interface to manage and retry failed jobs easily.
Best Practices
Queue Prioritization: Use multiple queues to prioritize jobs. For example:
'queue' => ['high', 'default', 'low'],
Balancing Strategies: Use the
balance
option to distribute jobs across workers:'balance' => 'auto',
Monitoring: Regularly check the Horizon dashboard to identify bottlenecks or issues.
Scaling: Adjust the
processes
value based on your server capacity and job load.Job Timeouts: Set appropriate timeouts to prevent long-running jobs from blocking the queue:
'timeout' => 60,
Laravel Horizon simplifies queue management and provides valuable insights into your application's background job processing. By leveraging its features and following best practices, you can build more efficient, scalable Laravel applications.
Remember to secure your Horizon dashboard in production environments and keep an eye on your Redis server's performance as your queue usage grows. Happy queuing with Laravel Horizon!