Running Multiple Instances of Laravel Horizon on the Same Server

Sep 23, 2019 1 minute read

If you're like me you tend to have a staging and production instance of your code on the same server. Recently I've been running into an issue where my Laravel Horizon queues were not processing when I had multiple instances of Horizon on the same server. Here's how I fixed that issue:

In your config/horizon.php file you'll need to create a new environment for your staging environment. There should be one for local and production already but nothing for staging. Create that

1'staging' => [
2 'supervisor-staging' => [
3 'connection' => 'redis',
4 'queue' => ['queue.staging'], // Use a unique name for your staging queue
5 'balance' => 'simple',
6 'processes' => 3,
7 'tries' => 3
8 ],

Once that is done we'll want to use a different Redis database than our production application uses so we don't have any collisions with the queues. Head over to your config/database.php file and add some values under the redis option.

1'options' => [
2'prefix' => env('REDIS_PREFIX', 'prod:')
3],

The last step is to edit your .env file and add some references to your staging queues. You'll need to define a REDIS_QUEUE key/value pair as well as some other key/value pairs for Redis.

1REDIS_QUEUE=queue.staging
2
3HORIZON_PREFIX="staging:"
4REDIS_PREFIX="staging:"
5REDIS_DB=2

That should be all you need to separate your Laravel Horizon instances on the same server.