Skip to content
Steven Roland

Mastering Laravel Horizon: Supercharge Your Queue Management

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!

Support My Work

If you enjoy my content, consider supporting me through Buy Me a Coffee or GitHub Sponsors.

Buy Me A Coffee
or

More posts

Streamlining Code Style with Laravel Pint

Laravel Pint is a zero-configuration PHP code style fixer for Laravel projects. It offers basic usage for entire projects, targeted formatting for specific files, and inspection of changes. Suggested uses include pre-commit hooks, CI/CD integration, editor integration, and custom configurations. Best practices involve regular use, team adoption, version control of configurations, and gradual implementation for large projects.

The Art of Leaving: Growing Roots Before Taking Flight

Inspired by John Green's quote, this post explores the paradox of leaving: to truly appreciate departure, we must first establish deep connections. It encourages readers to grow roots while maintaining the courage to leave for personal growth.