I stand up for children in need. Please join me in helping this family.

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

The Essence of Living: Finding What Matters Most

Lauren Oliver's quote from "Delirium" encapsulates life's purpose as finding, holding onto, and fighting for what truly matters. It emphasizes the importance of identifying core values and actively committing to them in the face of life's challenges.

Bridging Generations: Wisdom from J.K. Rowling's "Harry Potter"

J.K. Rowling's quote from "Harry Potter and the Order of the Phoenix" addresses the generation gap, emphasizing the responsibility of older generations to remember their youth. It highlights the importance of empathy in bridging generational divides.

Embracing Life's Fullness: The Art of Living Boldly

Inspired by Oscar Wilde's quote, this post explores the art of living life to its fullest. It offers insights on embracing new experiences, overcoming fears, and cultivating a rich, sensory-filled life, encouraging readers to live boldly and authentically.