Laravel Horizon is Rising on the Horizon
At FastComet, we’re huge fans of the Laravel Framework, running a team of PHP devs occasionally working on large, long-term projects. Its vast feature set, extreme flexibility, and expressive object-oriented design make development delightful and allows us to deliver high-quality products that help us improve our service even more.
And while the wait for the long-anticipated Laravel 5.5 (which is going to release soon in August 2017) is not over yet, Taylor Otwell has something special for all the Laravel Fans out there. At Laracon 2017, which was held a couple of weeks ago, Taylor introduced the latest package in the Laravel world. Similar to Cashier, Scout, and Passport before it, Horizon is an open-source package which you can bring into your apps but isn’t distributed with the core.
What Is Laravel Horizon?
Laravel Horizon is a package which turbocharges Laravel Redis queues with a truly beautiful dashboard UI and code-driven configuration system. It provides you control, insight, and analytics into the number of queues and queue workers you have, your failed jobs, and aims to simplify your job throughput. Horizon makes it easy to configure and manage your queues and see how they’re doing. Best of all Horizon is available for free for the entire Laravel community and is open sourced on GitHub.
According to Taylor Otwell, the creator of Laravel, here’s the background of why he built Laravel Horizon:
“I built Horizon to scratch my own itch. I need deep insight into my queue throughput, performance, wait times, failures. And, when a customer has a problem, Horizon allows me to quickly find the job that failed.”
Here are some of the key features of Laravel Horizon that we are going to deep dive in:
- Code-Driven Configuration
- Dashboards and Metrics
- Queue monitoring
- Failed Job Management
- Open-Source Software
Queue Balancing Options: Simple and Auto-Balancing
Horizon can automatically balance your queue worker processes across your queues depending on the workload of those queues. For example, if your “default” queue is empty, but your “notifications” queue gets really busy, Horizon can automatically allocate spare workers to your “notifications” queue to help those jobs get processed quickly. Once the queue has caught up, Horizon will make sure the processes are re-distributed fairly across all queues.
Queue balancing describes strategies to handle how Horizon splits resources between two queues.
- Simple – splits the processes between the two, regardless of the workload
- Auto – auto-balances your workers across queues based on the number of remaining jobs and average wait time on each queue. When a queue gets busy, Horizon can intelligently see if any other workers are available and move them to queues that need help.
Laravel Horizon comes with a code-based configuration, just like you’re used to with any other Laravel apps and components, which gives your team the opportunity to collaborate easily while dealing with the queues.
It allows you to set queue schedule, priority, timeout or any other setting with the help of
queue:work Artisan command. In other words, you can tell Horizon how many supervisors to run and for each define which connection they’ll use, which queues they should operate on, which mechanism to use for balancing the work, and the maximum number of processes they can spin up. The configurations are set in the
config/horizon.php file and can be executed by the
php artisan horizon command.
Horizon makes it easy to define all of these settings uniquely for each environment.
The Horizon dashboard is a beautiful, single page application build using Vue.JS. See what your queue is doing in real-time, insights into queue workloads, recent jobs, failed jobs, job retries, throughput and runtime metrics, and process counts. Authentication to the dashboard is controlled by a simple callback registered via
Horizon::auth, allowing you full control over access to the dashboard.
As an added bonus Horizon includes a Metrics page where throughput and average runtime trends can be viewed for specific types of tasks or queue workers with nicely drawn charts.
These statistics can be aggregated using the
horizon:snapshot command, which can be scheduled to run as often as every minute using Laravel’s built-in scheduler. This allows you to quickly find performance degradations after a deployment.
Failed Job Management
Most people that have used Laravel queues have had to SSH into a server via artisan commands and often required manually diving into the framework’s failed_jobs table. Thankfully, Horizon provides a clear and detailed interface for reviewing and retrying your failed jobs (yeah, we all have them). It will make it easy to dive into the details, retry failed jobs and even search failed jobs and retry specific jobs.
For each failing job, Horizon tracks the stack trace, tags and all of the relevant data, making it easy to understand the reason for the failure. You can also choose to retry a failed job after resolving whatever caused it to fail. It’s truly wonderful to have the recent job retries displayed directly on the failed job detail page.
Even if you’re not monitoring a tag, you can search the “Failed Jobs” list for any tags – allowing you to debug after the fact.
Failed jobs are retained for seven days (configurable) and the ability to search their tags is retained for 48 hours. All of this metadata is stored directly in Redis.
Horizon can notify you when one of your queues is becoming overloaded with jobs, allowing you to quickly increase queue capacity and keep your application running smoothly. It sends both Slack and SMS notifications when one of your queues is too full.
You can easily configure wait time thresholds to determine when a notification should be sent. Within the Horizon configuration file, you can set a number of seconds for what should be considered a “long wait time” for each of our queues. This means you are always aware of when your queues need more workers. Once you get the notification, Horizon’s code-driven configuration allows you to make a quick configuration change to add more workers.
Installing Laravel Horizon
Laravel Horizon is now available in beta. It can be downloaded from the Laravel Horizon webpage. Note that due to the nature of the setup, Laravel Horizon requires the use of Redis, which can only be set on our VPS and Dedicated Server plans. These hosting packages provide you with root access and complete control over the operating environment.
Install Horizon directly in your application through the following Composer command:
composer require laravel/horizon
It looks like Laravel Horizon will exit beta alongside the release of Laravel 5.5 which is expected to be sometime towards the next couple of weeks until the end of August. Supposedly, there are few more adjustments to come and possibly have some new features being added in the time leading up to the official release. But even as it stands right now, Laravel Horizon is pretty amazing. We, at FastComet, are truly excited for whatever future improvements are on the horizon for the framework.
The latest tips and news from the industry straight to your inbox!
Join 30,000+ subscribers for exclusive access to our monthly newsletter with insider cloud, hosting and WordPress tips!