Laravel Octane is the latest offering from the Laravel Team. It boosts our Laravel Application with supersonic speed by utilizing Swoole and RoadRunner. It bootstraps the Laravel framework dependency container, saves it in RAM, and thus cuts the bootstrapping process from each request and provides blazing-fast responses.
What is Octane?
Laravel Octane is a wrapper behind the high-performance servers, Swoole and RoadRunner. Swoole is a PHP extension, which, upon installing, provides us with a myriad of cool and nitty-gritty features such as coroutines, fibers, web-sockets, caching, and so on. On the other hand, RoadRunner is a high-performance application server, load-balancer, and process manager written in GoLang. Octane utilizes one or the other, depending on the developer itself. Nevertheless, it’s worth saying that by using the PHP Swoole extension, Octane gets much more handy features than with RoadRunner.
The idea is that you can get up and running in seconds with Octane if you choose RoadRunner as it’s just a GoLang binary file, and you need to download it, and you’re good to go. As for installing the PHP Swoole extension can become a complex and little cumbersome process, but the end result is way better as it gives you much more features than RoadRunner.
How does Octane work?
The developers love Laravel; even so, there is to raise an issue regarding the performance. We’re not saying that Laravel is slow, but at the very least, it’s not the best beast in the field. Laravel Octane tries to cut that issue in half.
First, let’s understand how Laravel Application is traditionally served under a web server. After a web-server(Apache or Nginx) gets a request, it then delegates the request to PHP-FPM, starting a new worker or reusing the available one.
A worker spawns the new process upon executing a PHP script. After which PHP will include all the files associated with the Laravel Project, and it’s going to read them from the hard disk, and reading from hard disk is always time and resource consuming. Then PHP will bootstrap the Laravel Framework and its dependency container, which most of the time takes much more time and resources than executing business-specific logic.
And finally, our business logic is executed. The idea is that creating new processes, including all those many files from the disk, bootstrapping framework is just time and resource-consuming, and it happens on each and every request. So then, what can Laravel Octane do about it?
Laravel Octane simply caches bootstrapped laravel framework in the RAM on the first request. Every request after it just uses the already bootstrapped framework from the RAM instead of reading the files from the hard disk and re-bootstrapping the framework. And that’s the core idea that boosts the framework performance so much.
Benchmarks.
“In a post-opcache, post-octane world – I see no reason to choose Lumen for a new project,” – Says Taylor Otwell, Creator of Laravel.
Lumen is a micro-framework based on Laravel, frankly speaking, it’s a Laravel framework without some of the components to make it fast. So Laravel with Octane as its server is way more performance than Lumen. Let’s see some real numbers, shall we?
I decided to stress test the Laravel application in 3 modes:
- Laravel with Octane
- Laravel with Apache WebServer(traditional approach)
- Laravel with its Built-In Server
Well, the results were as you might have imagined. For testing, I used well known, popular tool wrk, with the following configuration:
wrk -t1 -c50 ‘URL’
And the testing route was responding with just Hello World HTML, nothing fancy, but even with that, the result is quite interesting.
Source