Laravel Octane is the latest Laravel open-source package, introduced by Taylor Otwell during his Laracon Online Talk last May 12th, 2021.
The new Laravel package is a bigger and better version of Laravel. It’s powered by Roadrunner and Swoole, where you can store your Laravel server and website to let it handle all requests and boot-up processes. It’s faster, has a bunch of new features, and might be the future of Laravel web development.
This guide will go over everything you need to know about Laravel Octane. What it is, why it matters, and how you can get started.
What is Laravel Octane?
Laravel Octane is an open-source package that boosts your Laravel application performance through stateful PHP programming.
The package itself comes with dependencies and libraries for new features (mentioned below) and compatibility with high-performance servers such as RoadRunner and Swoole.
Laravel VS Laravel Octane
Laravel Octane runs on top of Laravel and PHP version 8.0 and above. It’s an addition to standard Laravel and is not a separate language.
When comparing performance, standard Laravel apps can process up to 500 requests per second at best, while Laravel Octane easily passes 2,000 requests alone. While the data across numerous sources for this will vary somewhat, the consensus is that Laravel Octane performs at least 10x better than traditional Laravel – and that’s based on conservative estimates.
Laravel Octane operates differently from traditional Laravel apps when it comes to servers as well. Octane has its own server through Swoole and RoadRunner, which Nginx and Apache should redirect to for incoming traffic.
However, another key difference between standard Laravel and Laravel Octane is in PHP usage. Laravel Octane changes your PHP stack into a semi-stateful PHP framework.
NOTE: Some sources will outright say that Laravel Octane uses a stateful PHP framework. But, since PHP is naturally stateless and most PHP developers are used to programming stateless frameworks, the Laravel Team made Laravel Octane only partially stateful. PHP libraries are also not ready to program Laravel in a completely stateful framework.
Before proceeding with the article, one of the biggest factors you’ll need to consider before trying out Laravel Octane is the fact that you’ll have to learn stateful programming.
Stateful PHP vs Stateless PHP
Let’s try to break down the difference between stateful and stateless PHP without being too technical.
Stateless PHP has servers that can act independently from a user or client’s request. The servers store user information and open the info file, send the requested query to the user or server, and then close the info file. This simplifies the design process because the info files are only opened when needed and will always be up to date for every new request.
On the other hand, Stateful PHP utilizes servers that act together with a client or user. The servers store user information and actively remembers this data as if the info file is open throughout the entire session. This makes the user-side process faster since operations like opening and closing information files no longer need to happen.
Real-life Example of Stateful vs Stateless PHP
It’s easier to understand the differences between these two with a real-life example. So, here’s a simple analogy:
You’re logging into your RunCloud account to access your personal dashboard. This dashboard contains personal information that only your account has access to. How does RunCloud know that you have access to the dashboard?
Stateless Servers | Stateful Servers |
The server stores a record signifying that you just logged into your RunCloud account. When you open your dashboard, the RunCloud server opens the record and sees that you logged in with the correct credentials, then closes the record again. This process repeats every time you go from page to page. | The server recognizes that you just logged into your RunCloud account and will keep this information open until you logout or end your session. During your entire session, even as you move from page to page, the RunCloud server will remember that you logged in correctly and won’t have to open and close the records to verify this. |
Aside from the contrasts in how these two work, the way of coding and thinking of stateful vs stateless PHP is also different. If you’re thinking of picking up Laravel Octane, you’ll have to dedicate some time to adapting and learning this new framework.
Why is Laravel Octane Stateful and Why Is It Faster?
Laravel Octane needed to adapt a stateful framework because it’s faster. Traditionally, the standard Laravel process creates a PHP worker for every incoming request (like opening a page). This means starting up an individual PHP process that needs to boot and prepare the framework before serving that one request.
To put that into context, that PHP worker would need to boot the framework and all of the service providers it uses. It would have to register those services within that container and then boot the providers. After that, the request goes through a list of middleware classes, hitting your controller, then finally rendering a view that opens up on your computer (opening a page).
With Laravel Octane, all the HTTP requests share the same booted framework. This means that once the framework is booted (when the website is opened for the first time), any request after that will make use of the framework that’s already been booted. Laravel Octane will not have to boot a new framework for every request, thereby decreasing the workload. This is why Laravel Octane is so much faster than traditional Laravel.
The reason why Laravel Octane can do this effectively is because of high-performance servers like Swoole and RoadRunner.
Swoole VS RoadRunner
Swoole and RoadRunner are two high-performance PHP servers that can manage PHP processes across strenuous and large-traffic scenarios.
Laravel Octane lets you choose between running Swoole or RoadRunner for your website.
Swoole is firstly a PHP extension, which means you’ll need to compile it on top of your PHP build during the setup. If you can get past that, Swoole is faster than RoadRunner and adds some custom PHP Laravel-related features for development. On the downside though, Swoole does not work with Xdebug, the most popular debugging tool for PHP. There are also cases where monitoring tools like New Relic and Datadog don’t work reliably with Swoole because it’s incompatible with Swoole’s co-routine and CLI environment.
RoadRunner, alternatively, works as a replacement for PHP-FPM. It doesn’t need to be compiled with your PHP files, making it easier to deploy and install on your Laravel website from the start. The only downside to RoadRunner is that it’s not as fast as Swoole and doesn’t come with Swoole’s PHP features. Other than that, it works right out of the box, and really well, if you want immediate performance improvements without modifying your PHP files.
Should You Use Laravel Octane?
So, since speed is the number one key metric for a better-performing website, should you update your PHP version and start installing Laravel Octane?
The short answer is that it will depend on your needs. ‘Yes’ if your website is struggling with performance, and ‘No’ if you can help it.
Laravel Octane requires PHP version 8.0 and the adoption of a stateful programming convention.
- There are tons of websites that run on older versions of PHP that will have to update their system just to use Laravel Octane.
- If your development team isn’t familiar with using a stateful framework, they will also have to dedicate some time to figuring that out before adopting Laravel Octane.
If these two issues are not a problem, then we can wholeheartedly recommend the improvement. Outside of that, unless your site is crawling and struggling from the magnitude of processes your server is handling, you may not need to upgrade to Laravel Octane.
Laravel Octane Key Features
To help you weigh your choices better, we’ve listed some key features from Laravel Octane, besides the obvious performance benefits. Most of these features will only apply to Laravel Octane that’s running with Swoole, so keep that in mind while reading through the list.
Octane Workers
PHP workers are traditionally created every time an incoming request is received. With Laravel Octane, multiple workers are created from the start making it faster for the server to immediately service requests as they are received.
You can even specify how many workers you want to begin with, as well as the number of workers you want to initialize per CPU core. Having multiple workers helps because if one worker is busy with a task, the other worker can start servicing other requests from the user without any delays. This is how Swoole and RoadRunner increase performance by large margins.
Here’s the code for that through Artisan:
php artisan octane:start --workers=4
If you’re using Swoole, you can also specify the number of task workers with this command. We’ll talk more about task workers below.
php artisan octane:start --workers=4 --task-workers=6
Be careful about how many workers you’ll initialize per CPU core though. The use case for this will vary from site to site and will depend on how intensive your processes are. We don’t recommend reconfiguring the Octane worker settings unless you know what you’re doing.
Concurrent Tasks
Laravel Octane also allows you to perform concurrent tasks, meaning these tasks will be done at the same time and not after each other. You can do this through task workers, which will only be available depending on the number you set with the octane:start command.
php artisan octane:start --workers=4 --task-workers=6
These task workers are separate from the web workers that receive web requests. If you want to process a task and keep a web worker busy, you can defer the task to a task worker instead.
This feature is only available for Swoole.
Ticks or Intervals
Ticks make operations execute repeatedly after a specific number of seconds. These are also referred to as internals just like the setInterval method in JavaScript. Here’s what the command looks like.
Octane::tick('simple-ticker', fn () => ray('Ticking...'))
->seconds(10);
Where:
- ‘simple-ticker’ represents the name of the ticker as a string
- fn () => ray(‘Ticking…’) is the callable that is executed after every interval
As of writing, there is no command to stop these ticks from running, so keep that in mind when using this feature.
This feature is only available for Swoole.
Octane Cache
The Laravel Octane cache driver is powered by Swoole tables, which we’ll also talk about below.
The cache driver reads and writes up to 2 million operations per second, making it a fast and excellent upgrade from your current caching system. It’s an in-memory cache, meaning the data will flush out when the server is restarted, so keep that in mind. This is the command you’ll need to activate the cache:
Cache::store('octane')->put('framework', 'Laravel', 30);
Another feature that’s related to Octane caching is being able to set a cache interval. This means you can refresh the data in the octane cache at given intervals. Here’s how you can do that.
use Illuminate\Support\Str;
Cache::store('octane')->interval('random', function () {
return Str::random(10);
}, seconds: 5);
This feature is only available for Swoole.
Octane Tables
Swoole tables are extremely fast and provide a huge boost of performance to your server. This is because Swoole tables can be accessed by all the workers on the server, making operations and data retrieval all the faster. The only caveat to this is that all the data in the tables will be lost when the server restarts.
Tables are defined within the table configuration array inside your application’s octane config file. The file will have a default table of 1,000 rows, but you can reconfigure that to your needs. It should look something like this:
'tables' => [
'example:1000' => [
'name' => 'string:1000',
'votes' => 'int',
],
],
The code for accessing a table goes through the Octane::table method.
use Laravel\Octane\Facades\Octane;
Octane::table('example')->set('uuid', [
'name' => 'Nuno Maduro',
'votes' => 1000,
]);
return Octane::table('example')->get('uuid');
The Swoole table only supports string, int, and float for the column types.
This feature is only available for Swoole.
How to Install Laravel Octane
Now that you know the benefits of using Laravel Octane as well as its key features, it’s time to teach you how to install it. Thankfully, since Laravel Octane is just a package, the installation process is the same as installing a dependency.
NOTE: Laravel Octane needs PHP version 8.0+ to work.
You can install Laravel Octane using the following command through the Composer package manager:
composer require Laravel/octane
This will download the Laravel Octane package. Once that’s done, install it with the help of the following command.
php artisan octane:install
The command will generate a configuration file config/octane.php. When you’re done installing Octane, it’s time to choose between RoadRunner and Swoole. Remember, RoadRunner is the easiest one to install as it works on top of your Laravel site, but it’s less powerful than Swoole. On the other hand, Swoole is a PHP extension which means you’ll have to compile it together with your PHP files and have it run with them.
RoadRunner
You can choose Roadrunner if you don’t want to install any other application packages and don’t need the extra features that come with Swoole.
- To start with RoadRunner, open up the config file that was generated when we installed Laravel Octane.
- In the file, replace the ‘server’ key into ‘roadrunner’.
After that, run this command through composer:
composer require spiral/roadrunner
This will install RoadRunner to your server.
Swoole
As mentioned before, Swoole is much more powerful than RoadRunner and comes with some extra PHP features. Here’s how you can install Swoole:
- Open your PECL and run the command: pecl install swoole
- While the installation is happening, you’ll be asked if you have support for certain technologies within Swoole. You can safely stick to default values here and proceed.
Once Swoole is done installing, we can proceed with the next step and run the server.
Running Laravel Octane
With either Swoole and RoadRunner installed, you can now launch Laravel Octane with the following command:
php artisan octane:start
After Action Report — Install Laravel Octane on RunCloud
Laravel Octane can be a game changer for your website, or it can be completely unnecessary — in the end, it all depends on your needs.
There is a learning curve to mastering Laravel Octane, but the benefits of implementing Laravel Octane can be tremendous.
Thinking about installing Laravel Octane on your RunCloud server? Follow our full step-by-step guide here!
What are your thoughts on Laravel Octane? Did you try to implement it? Let’s talk in the comments below! 💬