DEV Community

Cover image for Scaling Laravel Applications with Octane
Chris Wolf
Chris Wolf

Posted on • Updated on • Originally published at laramate.de

Scaling Laravel Applications with Octane

Laravel Octane is a high performance HTTP server built on top of the Laravel framework. It provides a simple, elegant way to scale Laravel applications. Octane uses Swoole, Roadrunner or FrankenPHP as its underlying engine, so it's able to handle thousands of concurrent connections with ease.

There are many benefits to using Octane, such as improved performance, reduced resource usage, and the ability to scale horizontally. However, there are also some caveats to be aware of before using Octane in production. In this article, we'll discuss the opportunities, benefits and caveats of using Laravel Octane to scale a Laravel application.

TL;DR

Laravel Octane is a high performance HTTP server built on top of the Laravel framework. Octane uses Swoole or Roadrunner as its underlying engine, so it's able to handle thousands of concurrent connections with ease. There are many benefits to using Octane, such as improved performance, reduced resource usage, and the ability to scale horizontally. But there are also some caveats to be aware of before using it in production. In this article, we'll discuss the opportunities, benefits and caveats of using it to scale a Laravel application.
How does Laravel Octane work?

Laravel Octane speeds up the serving of Laravel applications. It does that by booting your app once and then keeping it in memory to speed up all subsequent requests. By doing this, the overhead of the Framework is being stripped out.

Under the hood, it uses Open Swool, Swool, Roadrunner or FrankenPhp to boot the app and serve it.

  • Swoole and Open Swoole are PHP extensions. They allow your PHP application to handle multiple requests concurrently, which can significantly boost performance for high-traffic websites. However, Swoole requires compiling PHP with the extension, while Open Swoole is a drop-in replacement for easier deployment.
  • Roadrunner is a different approach. It's not a PHP extension but a process manager specifically designed for running PHP applications. It acts as a replacement for the standard PHP-FPM (FastCGI Process Manager) and can offer performance improvements without requiring changes to your PHP code.
  • FrankenPHP is a relatively new contender. It's another server option for the Laravel Octane framework, designed for high-performance PHP applications. Similar to Swoole and Open Swoole, it utilizes a concurrency model for handling multiple requests simultaneously. FrankenPHP is built on top of the Caddy web server. Update April 2024: FrankenPHP's Octane integration is in beta and should be used with caution in production.

The difference between Swoole and Open Swoole

Open Swoole is a fork of the Swoole framework, which started in late 2021 because of some disagreement inside the developing community of Swoole.

Today, Open Swoole has a better documentation but a smaller development team. Which might be to be considered when starting a larger project.
The benefits of using Octane to scale Laravel applications

Octane speeds up Laravel applications by caching data and objects in memory. So even with no other alterations to hardware or infrastructure, Laravel Octane can improve the performance of your app by up to 30%. This gain is achieved by eliminating the disk reads, process forks, and other overhead associated that happens when the framework is booted up on every request. That way, Octane can handle thousands of concurrent connections with ease. This makes it ideal for applications that receive a lot of traffic or need to be highly available.

On top on that, this cost-effective way can be expected to be liked by any business, start-up or enterprise.

In a best-case scenario, you don't even need to make changes to your codebase. But this depends heavily on how your application is structured.

Potential caveats of using Laravel Octane

If you're thinking of using Octane to scale your Laravel application, there are a few things to be aware of. Octane is not compatible with all Laravel applications. In particular, it doesn't work with applications that use session state or certain types of middleware. Finally, while Octane can improve performance, it won't necessarily solve all your performance problems. If your application is already struggling to keep up with traffic, Octane may not be enough to save it. In that case, you'll need to consider other options, such as horizontal scaling.
Neccessary refactorings

If your application uses session state or certain types of middleware, you'll need to make some changes to your code before you can use Octane. In particular, you'll need to add support for Swoole's coroutine-based sessions and remove any middleware that isn't compatible with Swoole.

Other changes that needs to be made, includes the dependency injection container. I suggest, you check the official laravel documentation on this.

How are code changes are handled?

Octane provides a convenient way to automatically watch your codebase for changes and restart the Octane server without any manual interventions. This means that you can make developments more easily by changing codes in one place instead of having to stop/start an entire application instance each time there's something new in your codebase.
Alternatives to Laravel Octane

Laravel Octane is a great tool for scaling Laravel applications, but it's not the only option. There are other options available that may be more suitable for your needs. Here are some alternatives to consider:

More hardware = more performance

Just to mention it, we do not recommend this. This is the easiest, the least cost-effective and the least future-proof method of scaling: Just throw more hardware-performance at your project. This has the obvious downsides of higher running-costs, the need to migrate to different server and the fact that you are not able to scale on demand with this setup.
Nginx Load Balancing with or without AWS

To build a scalable web application with Laravel on Amazon Web Services, AWS can be used to automatically scale the app to several instances that share one MySQL database. All of these servers would use one Amazon Relational Database Service (RDS).

Scaling with AWS has been around for some time and it works well. But, it costs can be intransparent. Compared to Octane, this goes with the same principle of how it works, but it is not as effective as Octane would be. A benefit is definetly that you can scale your existing code as-is. No Octane-specific changes needed.

How to setup Laravel Octane

For this example, we start with a new Laravel app.

Laravel new octane

New Laravel Project setup in CLI

Next, we install Laravel Octane with FrankenPHP. If FrankenPHP is not yet installed on the system, the artisan Command will handle it for you.

composer require laravel/octane --dev

php artisan octane:install

Laravel Octane Installation

Laravel Octane Installation.

Thats all for the installation. Now, we add a couple of measurements to the Laravel app.

Now, you can start Laravel Octane:

php artisan octane:start --port=8001

Use wrk (can be installed via brew) to benchmark Laravel Octane:

wrk -t4 -c50 http://localhost:8001

Laravel Octance Benchmark

Conclusion

Laravel Octane is a great way to scale your Laravel application. It's easy to use and can improve performance without needing to make changes to your codebase. However, it's not compatible with all applications and may not be enough to save an already struggling app. If you're considering using Octane, be sure to do your research and consider all your options before making a decision.

When you have Laravel Octane in place, watch out for the badge "Laravel Octane Compatible" on packages you add to your project. This means that the package is tested to work with Laravel Octane out of the box and will save you time configuring it to work properly.

Top comments (0)