Hey Folks, I Hope you all are doing well. Today, I am going to talks a bit about Queue management with Laravel Horizon and Redis. Though you may feel its too late to talk about but I would like to add this as a refresher or may be a guide for the people who are still away from it.
If you are only interested in setting up the jobs the old school way, you can check my old article here
(Note : Redis don’t support windows (at least at the time of writing this article) So, If you are into windows you will have to setup WSL for further follow up or you will need to setup a remote Redis cluster and use that. otherwise, you are out of luck as Redis does not support windows platform yet and Horizon don’t support database driver.)
So, Enough of intro and lets begin,
What is Horizon?
Official Documentation :
Horizon provides a beautiful dashboard and code-driven configuration for your Laravel powered Redis queues. Horizon allows you to easily monitor key metrics of your queue system such as job throughput, runtime, and job failures.
Summary : Laravel Horizon is a tool that provide you with solid interface to monitor your jobs and queues. (Add-on charts and real time tracking of any events or jobs statuses)
What is Redis?
Wikipedia : Redis is an in-memory data structure project implementing a distributed, in-memory key–value database with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indexes.
Summary : An In memory data storage structure that stores the values as key value pair.
Install Laravel Horizon :
Installation of Laravel horizon is pretty much straightforward just like any other composer package. just run the below command to install horizon.
composer require laravel/horizon
Once Installed, you will need to publish its assets/Config which you can do by executing the below command.
php artisan horizon:install
The config provide you with many options which you can check here with the official docs.
Install redis :
Redis can be configured as a simple service like MySql on your local system/server which will work just like you are using the remote Redis cluster. I wont cover that portion here and we will stick with setting Redis as local service.
Method 1 : Install redis-server as package.
> sudo apt update
> sudo apt install redis-server
and if everything goes well, you are done.
Note : To run Redis you will need to install php-redis extension. so if you don’t have that, grab it with below command.
> sudo apt install php7.3-redis // update the php version to your
Method 2 : Manually compiling the binaries :
To compile the Redis locally, just follow the below steps as mentioned in its official docs as,
$ wget https://download.redis.io/releases/redis-6.0.9.tar.gz
$ tar xzf redis-6.0.9.tar.gz
$ cd redis-6.0.9
Just To explain : Its just copy the remote gzip binaries to local system ( Consider the folder path where you run this command as this directory will be used to store redis app data ) and extract to folder
redis-6.0.9 and finally we switch to that directly and run make command that will trigger the build for redis binaries.
Starting & Stopping Redis server.
If you have installed Redis with Method 1, you can simply use the service utility as,
// Start redis
> sudo service redis-server start// Stop redis
> sudo service redis-server stop
If you have followed the method 2, to run the redis-server simply switch to installed folder and then run src/redis-server command.
cd > <folder where we have zip extracted>/redis-<version>> src/redis-server
TIP : when you have manually compiled the redis binaries and ran it from compile folder, by default it will run in foreground so to avoid that, you can simply append & sign at the end and run it as background job.
Plus you can use the same approach for all your other programs. 😅
Note : when you run your binary as background job, you will have to use the kill approach to terminate the background running process.
Assuming you have already published the horizon assets with artisan command horizon:install, you can now run the horizon with below command.
> php artisan horizon
If you have followed everything well just visit the browser URL where your app is running with /horizon as path. In my case its
Congratulations, You have successfully configured Laravel Horizon! 😀😃
Now, try dispatching the jobs to your queue and see the horizon in action.
IMP note :
- For local environment, Horizon directly provide access to dashboard which is not recommended for production setup, so when you deploy to production please update the gate method on HorizonServiceProvider.php to specify the authenticated user you want to allow access to.
- If you don’t want to use authenticate you will explicitly need to specify the user model as null argument on gate method.
With Laravel horizon you can review/ Monitor/ Manage the job process granularly and there are tons of option available which I will cover in next article. Laravel documentation here provides an absolute summary to them which you can check.
I hope someone find this helpful. Thank you for your time 😊😊
See you in next article.👋