Consent

This site uses third party services that need your consent.

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!

    More posts

    Mastering Fear: The Path to True Courage

    Inspired by Veronica Roth's quote, this post explores the true nature of courage. It challenges the myth of fearlessness, offering practical strategies for controlling fear and achieving freedom from its constraints.

    Streamlining Laravel Development with Homestead

    Laravel Homestead simplifies development environment setup using a pre-configured Vagrant box. Install Homestead, customize the Homestead.yaml file, and start developing. It's ideal for team development, managing multiple projects, and testing different PHP versions. Use it to streamline your Laravel development workflow.

    The Solitary Journey: Embracing Life's Solo Walks

    Inspired by a quote from "Mockingjay" by Suzanne Collins, this post explores the importance of solitary journeys in life. It discusses the necessity, benefits, and types of solo experiences, encouraging readers to embrace solitude for personal growth.