Laravel Horizon: queue monitoring + configuration
In this video I give you an overview of Laravel Horizon, a package that provides a beautiful dashboard and code-driven configuration for your Laravel powered Redis queues.
Everything I said
Welcome back to the What Is Laravel series. I'm your host, Aaron Francis. Let's dive right in. Laravel Horizon, gotta be one of my favorite packages in the Laravel ecosystem. It is a first-party package, so it's written and maintained by the Laravel team.
It's been around since Laracon 2017, I think, so it's been around for a long time now. And what it does is it provides you a beautiful dashboard for monitoring your queue workers. It also gives you a code-driven way to configure the number of workers, the balancing strategy, the, the processes, and so your entire queue system can be wrapped up and put into this Horizon configuration
file. Now, importantly, this only works with Redis-backed queues. So if you're using a database-backed queue or an SQS-backed queue, it's not gonna work with that, and so that's one maybe drawback? But I pretty much use Redis-backed queues for everything. If you're coming from a different ecosystem, this is very similar to Sidekiq over in the Ruby world, or I think it's called Oban over in the Elixir world.
And so if you're coming from a different spot, that probably sounds more familiar to you than Horizon, but they're all basically the same thing, except Horizon is free and open source. So I'm gonna show you how it works here, and then we'll talk a little bit more at the end. I've got a very basic Laravel application here. If we open it in the browser, we see it's just this Invite a User, and this doesn't really do anything except queue up a job.
So this is the application that we're working with. I'm gonna go ahead and do composer require laravel/horizon. That's all it takes. It's gonna pull the package into my application, and then you can see that we are left with... We're left with a lot of Horizon commands. We're not gonna cover all of them, but the first thing I am gonna do is say php artisan horizon:install.
And what that's gonna do, it's gonna publish the assets, the service provider and, importantly, the configuration. We'll go over the configuration here in a second, but I wanna show you that in the matter... in, in a matter of 30 seconds here, we now have a full-on, if we hit in the browser, we now have a full-on queue dashboard. Monitoring, metrics, pending jobs, completed jobs, silenced jobs, failed jobs.
All of this lives inside of our application. It's being served by this Laravel Horizon package, so all of this, all of this Chrome, all of these assets, all of this logic is inside that Laravel Horizon package. Now it's inside of our app. Now, this dashboard is totally open to the public, where everybody can see it, in local development only.
By default, in production, nobody can see this, and you have to modify the authorization gate to allow people to look at this in production, which is kind of important. That's kinda what you want. So let me show you how that works, and then we'll dive in a little bit deeper. To modify the authorization, we're gonna look at the Horizon service provider which has been published into our application.
So you can see that it lives inside of our app provider's directory. That was put there by the Horizon install command. First thing you'll see is this looks interesting, but we're gonna skip that for now. So we'll come back to it. Pay attention. We're gonna jump down here to this view Horizon gate, and by default, it's just saying, "Great, put some people's emails here, and we'll check to see if they're allowed to use it." So we could put aaron@example.com, please send me some emails, um, and this would allow me to use this
in production. If, if you aren't, um... if you don't have a user login system and you wanna limit by IP only, I think you can say equals null, and then you don't have to check the user, and you can do anything that you want down here. It's just a function. You can do anything you want to decide if this person is able to view the Horizon dashboard.
Scrolling back up to this very interesting thing, Horizon also comes with notifications. So if a queue is overwhelmed or backed up or jobs are taking a really long time to complete, you can just route notifications to many different channels, just like that. All you have to do is put the information in here.
With SMS, you would have to set up... You would have to set up your SMS provider, but this uses Laravel's notification system under the hood, so it's pretty... It's pretty straightforward. Let's look at the configuration file. Before we look at this configuration file, I have to warn you, it's pretty robust, which is a good thing. We wanna be able to configure it exactly how we want. I'm not, however, going to show you every single option.
There's too much right here. But I can show you, uh, a few of the interesting ones. You can serve the Horizon dashboard from a subdomain. So if you don't want it on your main domain, you want it on, like, app dot or auth or admin dot, you could do that. You can also change the path where it is served. So it doesn't have to be /horizon. It can be slash whatever else. Pretty cool.
Not what I'm here to show you. I am here to show you this environments key. So in this environments key, you've got production and you've got local, and this is where you get to define, per environment, you get to define how many workers, what the balancing strategy is, what queues are being worked. So inside of these keys, you've got something called supervisor1.
Doesn't need to be called that. It could be called foo1. It could be called anything you want. We'll leave it at supervisor. In this context, supervisor just means, um, a collection of processes, a group, a bundle. That's it. Nothing more fancy than that. The reason you can have multiple is, let's say you want to have- Um, this supervisor, this group of processes, this configuration, you just want it to process the default queue and maybe the high queue.
So maybe you have a high and a low queue, and you need more throughput on the high queue than you do on the low queue. This is a great place to do that. In fact, let's go ahead and turn that up to 15. We'll turn this down to 3 'cause we don't super care about it, and then we'll say, for this supervisor, all I want you to process is, in fact, just process the low queue.
That's all that I want you to care about. Three processes is fine. I don't really care about that so much. Now, you can say, you can say I want to balance the default is auto, which is good. I like that. I think the other options are simple, and then I think false is an option. But since auto is the default, I usually leave that off.
And what this is gonna do is this is gonna allocate processes based on how deep the queues are. So if your high queue is totally slammed and your default queue is empty, it's gonna move a bunch of processes over to process the high queue. You can say, "Hey, don't, don't leave me totally high and dry." Um, let's just say that the min- the minimum number of processes working on a single queue, let's make that two.
So you can move 13 over to the high queue, but leave two on to process the default queue. Now, you can also have something, I think it's auto-scaling strategy. Let's... I think that's right. Let's check the notes. Auto-scaling strategy. That is c- correct. You've got time, and you've got size.
And so if you have some jobs that take a really long time, you can say, "Actually, let's, let's balance based on how long we think it will take to empty out the queue instead of balancing based on raw number of jobs." So you can change that if you want. And then down here, it's gonna say, how many processes do you wanna shift at a time?
In our case, we're saying one process every three seconds. And so there's so much here that you can configure to match your exact setup, and you can find the balance between throughput and absolutely hammering your server. Um, this is where I think Horizon shines the best, is putting all of this in version control and then putting all of this behind a single command.
So you can have 50 processes, but for you to start it, all you have to do is run PHP Artisan Horizon. In fact, let's go ahead and do that right now. So if we run PHP Artisan Horizon, that's it. That's all you have to do. Now, it is running under the hood. It's running up to, what did we say, 15 plus 5 I think.
So it's running up to 20 processes, watching those queues for us, and this, this is the only command that we have to keep alive. This is it. I'm gonna show you these commands in a second, but first, let me show you configuration from a real app that I've worked on so you can see some of the nuances there. This is the configuration from an app that I have worked on. You see we have a supervisor short, supervisor long.
In the connection, one is on Redis, and one is on Redis Long. That makes enough sense. Timeout is 90. Timeout is 1200. And you see this one processes the high and the default queues, and this one processes the long and the scraper's queue. So these jobs take a whole lot longer, and so we give it a much longer timeout, and if- and just a few fewer processes than the ones that need to have a lot higher throughput.
Starting Horizon is easy. Like, we just looked at PHP Artisan Horizon. This is the command that you would, um, that you would wanna keep alive. If you're running on Forge, it's a simple toggle. You just say, "Keep Horizon alive." If you're running somewhere else, you'd need to set up a- actual supervisor to make sure that this stays alive because when this command goes down, all of your key workers go down.
And y- in fact, you wanna roll this. You wanna terminate this gracefully when you are deploying a new version of your application. You can do that by saying PHP Artisan Horizon Terminate. So you send the terminate signal, and over here it stops. And this is really important when you deploy new code because this is a long-running process.
It is gonna put your application in memory, and so the last thing you probably wanna put in your deploy script is Horizon:terminate, and then let Supervisor bring that back to life, and it's gonna load your new application into memory. So we'll go ahead and start this again. And over here, I'm just gonna run PHP Artisan, I think it's invite.
Um, let's grep for... I think it's invite. Yeah, PHP Artisan Invite Users. And this is gonna put 1,000 jobs on the queue, and we should start to see that working. So this, this job sleeps for two seconds to simulate, you know, calling out to an API or something. But you can see that we're, we're churning through these jobs, and if we look in the browser, if we look in the browser, you'll see we're running three processes on the default,
and we've got some failed jobs. So you can click into the failed jobs and see, okay, well this failed to send. In fact, let me just, let me just retry that guy. And if you click Retry from the dashboard, it's gonna put it back in the queue, and you're gonna get, um, you're gonna get this, the retry down here. It shows that it's pending. It's waiting. So let's, uh, let's go ahead and kill...
Let's kill Horizon again and hop back to the browser. And if we look in the browser, you see that it is inactive, and we're gonna come over to, we're gonna come down here to Local, and let's change the max processes to, let's go crazy. Let's make it, uh, let's make it 25. And if we start this again- I bet those are gonna churn through a lot faster.
We see we're now running 25 processes, and it gives us a little bit, it gives us a little bit of information about the queues themselves. You can see we've got 800, 700, um, jobs in this default queue. And so this gives you a nice overview of not only your queues, but also the supervisors that are running.
And if we check back here in local, we just have the one supervisor, so that checks out. That is correct. And you can see this is just totally churning through these jobs now. You can check the completed jobs to look in there and see s- a little bit of information. If you have a bunch of jobs that you don't want mucking up your completed jobs view right here, you can add those to silenced jobs and those won't show up.
Sometimes that's third-party packages running jobs or, um, notifications or something like that, that you don't really care to see. You can put, you can put those in the silenced jobs. Let me show you a few of the Horizon commands before we go. PHP artisan Horizon. Let's start with status. You can see Horizon is running, which is good. Then you can say list, and this will show all of the machines and the supervisors that are running.
You could also do PHP artisan supervisors, and that's gonna show you, um, a little bit more detail about each individual supervisor. You can also pause. So if you're like, if your server's getting totally hammered by something, you can say, "Whoa, whoa, let's not, let's not work with any queued jobs at the moment." So you can log in or you can SSH in, pause the workers, and everything pauses.
One command, and you could potentially pause hundreds of workers. And then after you fix whatever is broken, it's not resume, it's continue. After you fix it, you can just say, "All right, we're good to go. Back to work, people." And finally, remember, the one that you want to run when you're deploying is PHP artisan Horizon terminate.
There you go, and Horizon will stop working. Speaking of, it is time to terminate this video. Pretty proud of that one. Um, why would you, why would you maybe not use Horizon? I think potentially the best reason to not use Horizon is you're using SQS as your, as your queue driver, and therefore you can't use Horizon.
If you're already using Redis as your queue driver, I don't see a good reason not to use it, frankly. Free, open source, first party, battle tested, sh- extremely powerful, makes your life extremely convenient. So give Horizon a try. If you wanna watch a video about Laravel Octane in this same style, I'll put that here. Until the next time, see ya.