setting up multiple horizons servers
Table of Contents
This time, I gonna write about setting up multiple horizons servers. Before you continue reading the article you should know what’s Laravel Horizon and how to setup Laravel+Horizon. If you still haven’t, go read the official documentation.
The article focus on how multiple Horizon is working and will try to explain to be easier setup for your own. The idea is when we have lots of workers that handle tons of process we gonna separate the worker process to different servers. Let’s take a look at the following diagram.
We will have an application server which sends queue to the queue server (Redis). And there are three Horizon servers which handle the queues which we define which queue to pick up. Let’s try to implement the above diagram.
(I will not go detail of how to set up step by step of Laravel and Horizon).
First, create a new Laravel app call app . And setup three horizon servers. I will name like the following horizon-one , horizon-two and horizon-three . Inside all those Horizon servers, setup your app Laravel application. After installing your application on all three Horizon servers, inside config/horizon.php add the following environment config.
One notice here, even we have the multiple servers like app , horizon-one , horizon-two , horizon-three we are still using the same code base.
## /*
## | Horizon Environement
|
| This value determine Horizon's environment!
|
*/
'env' => env('HORIZON_ENV'),
The above environment will determine Horizon’s environment. Normally, Horizon checks the default application env which is not an ideal usage for us. Since we are creating multiple Horizon servers with the same code base and application might use the same environment. After setting up the Horizon’s environment, let’s continue another one.
Let’s imagine, you have three queue name notification , caching and default . Which queue from the application server. In that case, notification queue will handle by horizon-one server, caching queue will handle by horizon-two and default queue will handle by horizon-three .
In the real world, there can be multiple queues that handle by one horizon server.
Inside the config/horizon.php, add the following config after local block like the following
'local' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 3,
'tries' => 3,
]
],
'horizonOne' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['notification'],
'balance' => 'simple',
'processes' => 3,
'tries' => 3,
]
],
'horizonTwo' => [
'supervisor-2' => [
'connection' => 'redis',
'queue' => ['caching'],
'balance' => 'simple',
'processes' => 3,
'tries' => 3,
]
],
'horizonThree' => [
'supervisor-2' => [
'connection' => 'caching',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 3,
'tries' => 3,
]
],
Inside the horizon-one horizon-two and horizon-three Laravel env , please don’t forget to add HORIZON_ENV which belong to which one. And yeah, that’s it!
Don’t forget to make connectable between horizon servers and redis server. And redis server with application server.
Comments, if you are not clear about the process. I will try to respond as soon as possible. Clap 👏 if this post helps you.
Comments (0)
What are your thoughts on "setting up multiple horizons servers"?
You need to create an account to comment on this post.