Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I configure Laravel Horizon for queue workers
    Laravel Horizon queue workers configure Laravel Horizon Horizon installation worker settings job distribution Laravel queue management


How do I configure Laravel Horizon for queue workers


To configure Laravel Horizon for queue workers, follow these steps:

Step 1: Install Laravel Horizon

1. Install Horizon Package:
- Run the following command to install the Horizon package:

bash
     composer require laravel/horizon
     

- Then, publish Horizon's assets:
bash
     php artisan horizon:install
     

Step 2: Configure Horizon

1. Configure Horizon Configuration File:
- Open the `config/horizon.php` file and configure the worker settings. For example:

php
     'environments' => [
         'production' => [
             'worker-supervisor' => [
                 'connection' => 'redis',
                 'queue' => ['default'],
                 'balance' => 'simple',
                 'maxProcesses' => 10,
                 'maxJobs' => 0,
                 'memory' => 512,
                 'tries' => 3,
                 'nice' => 0,
                 'timeout' => 600,
             ],
         ],
     ],
     

Step 3: Define Worker Queues

1. Define Worker Queues:
- Define the queues for each worker server. For example, if you have two worker servers, you can define two queues:

php
     'environments' => [
         'production' => [
             'worker-supervisor-1' => [
                 'connection' => 'redis',
                 'queue' => ['queue-1'],
                 'balance' => 'simple',
                 'maxProcesses' => 5,
                 'maxJobs' => 0,
                 'memory' => 512,
                 'tries' => 3,
                 'nice' => 0,
                 'timeout' => 600,
             ],
             'worker-supervisor-2' => [
                 'connection' => 'redis',
                 'queue' => ['queue-2'],
                 'balance' => 'simple',
                 'maxProcesses' => 5,
                 'maxJobs' => 0,
                 'memory' => 512,
                 'tries' => 3,
                 'nice' => 0,
                 'timeout' => 600,
             ],
         ],
     ],
     

Step 4: Run Horizon

1. Run Horizon:
- Run the Horizon command with the desired worker server:

bash
     php artisan horizon --worker=worker-supervisor-1
     

- Alternatively, you can specify multiple workers:
bash
     php artisan horizon --worker=worker-supervisor-1,worker-supervisor-2
     

Step 5: Monitor Horizon

1. Monitor Horizon:
- Access the Horizon dashboard to monitor the status of your queues and workers:

bash
     php artisan horizon
     

Additional Tips

- Environment Variables:
- Use environment variables to configure Horizon settings. For example, you can set the `HORIZON_ENV` variable to specify the environment for Horizon:

makefile
    HORIZON_ENV=production
    

- Worker Configuration:
- Configure the worker settings based on your application's requirements. For example, you can set the `balance` strategy to `simple` or `auto`:

php
    'balance' => 'simple',
    

- Error Handling:
- Ensure that you handle errors properly in your job classes. Laravel Horizon provides features like job retries and failed job handling:

php
    public function handle()
    {
        // Job logic
    }

    public function failed()
    {
        // Error handling logic
    }
    

By following these steps, you can configure Laravel Horizon to manage your queue workers efficiently and distribute jobs evenly across multiple worker servers. This ensures that your application remains responsive and scalable even under heavy load.

Citations:
[1] https://laracasts.com/discuss/channels/laravel/laravel-horizon-queue-worker-configuration
[2] https://stackoverflow.com/questions/74707290/laravel-queues-horizon-distribute-jobs-evenly-across-multiple-worker-servers
[3] https://laravel.com/docs/11.x/horizon
[4] https://wpwebinfotech.com/blog/laravel-horizon/
[5] https://github.com/laravel/horizon/issues/343