GDPR-compliant. CRMs. ERPs. System Integrations. More? Get in touch. 

Scaling Laravel Applications with Octane

Chris
Managing Director, PHP Developer
Updated:
Scale Laravel with octane.

Laravel Octane is a high-performance HTTP server built on top of the Laravel framework. It provides a straightforward way to scale Laravel applications. Octane uses Swoole, RoadRunner, or FrankenPHP as its underlying engine, enabling it to handle thousands of concurrent connections with ease.

Using Octane comes with many advantages, including improved performance, reduced resource consumption, and the ability to scale horizontally. However, there are some caveats to consider before deploying Octane in production. In this article, we explore the capabilities, benefits, and trade-offs of using Laravel Octane to scale a Laravel application.

„Laravel Octane can improve the performance of your application by up to 30% -- without any changes to hardware or infrastructure. It eliminates the framework boot overhead by keeping your application in memory across requests.”

How Does Laravel Octane Work?

Laravel Octane accelerates the delivery of Laravel applications by booting your application once and keeping it in memory for all subsequent requests. This eliminates the framework overhead that normally occurs on every single request.

Under the hood, it uses Open Swoole, Swoole, RoadRunner, or FrankenPHP to boot and serve the application.

  • Swoole and Open Swoole are PHP extensions that allow your application to handle multiple requests concurrently, significantly boosting performance for high-traffic websites. Swoole requires compiling PHP with the extension, while Open Swoole serves as a drop-in replacement for easier deployment.

  • RoadRunner takes a different approach. Rather than being a PHP extension, it is a process manager specifically designed for running PHP applications. It acts as a replacement for the standard PHP-FPM (FastCGI Process Manager) and can deliver performance improvements without requiring changes to your PHP code.

  • FrankenPHP is a relatively new contender. It is another server option for the Laravel Octane framework, designed for high-performance PHP applications. Similar to Swoole and Open Swoole, it uses a concurrency model for handling multiple requests simultaneously. FrankenPHP is built on top of the Caddy web server.
    April 2024: The FrankenPHP 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 that emerged in late 2021 due to disagreements within the Swoole developer community.

Today, Open Swoole offers better documentation but has a smaller development team. This is worth considering when starting a larger project.

Benefits of Using Octane to Scale Laravel Applications

Octane speeds up Laravel applications by caching data and objects in memory. Even without any changes to hardware or infrastructure, Laravel Octane can improve your application's performance by up to 30%. This gain is achieved by eliminating disk reads, process forks, and other overhead that occurs when the framework boots on every request. As a result, Octane can handle thousands of concurrent connections with ease, making it ideal for applications that receive heavy traffic or need to be highly available.

This cost-effective approach is appealing to businesses, startups, and enterprises alike.

In the best-case scenario, you may not even need to make changes to your codebase. However, this depends heavily on how your application is structured.

Potential Caveats of Laravel Octane

If you are considering using Octane to scale your Laravel application, there are a few things to keep in mind. Octane is not compatible with all Laravel applications. In particular, it does not work with applications that rely on session state or certain types of middleware. Furthermore, while Octane can improve performance, it will not necessarily solve all performance problems. If your application is already struggling to keep up with traffic, Octane alone may not be enough. In that case, you will need to consider additional options such as horizontal scaling.

Necessary Refactoring

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

Other changes that may be required involve the dependency injection container. We recommend reading the official Laravel documentation on this topic.

How Are Code Changes Handled?

Octane provides a convenient way to automatically watch your codebase for changes and restart the server without manual intervention. This means you can develop more efficiently by making changes in place, rather than having to stop and start the entire application instance every time you update your codebase.

Alternatives to Laravel Octane

Laravel Octane is a powerful tool for scaling Laravel applications, but it is not the only option. Depending on your needs, other approaches may be more suitable. Here are some alternatives to consider:

More Hardware = More Performance

Just to mention it: we do not recommend this approach. It is the simplest, least cost-effective, and least future-proof method of scaling -- simply throwing more hardware at the problem. The obvious downsides include higher running costs, the need to migrate to a different server, and the inability to scale on demand.

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 application across multiple instances sharing a single MySQL database. All of these servers would use an Amazon Relational Database Service (RDS).

Scaling with AWS has been around for some time and works well. However, costs can be opaque. Compared to Octane, this approach follows the same principle but is not as efficient. One clear advantage is that you can scale your existing code as-is, with no Octane-specific changes required.

How to Set Up Laravel Octane

In this example, we start with a fresh Laravel application.

Laravel new octane.

Next, 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.

That is all for the installation. Now we add some measurements to the Laravel app.

Start Laravel Octane with the following command:

php artisan octane:start --port=8001

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

wrk -t4 -c50 http://localhost:8001
Laravel Octane Benchmark.

Conclusion

Laravel Octane is an excellent way to scale your Laravel application. It is easy to use and can improve performance without requiring changes to your codebase. However, it is not compatible with every application and may not be sufficient to rescue an application that is already under heavy strain. If you are considering Octane, make sure to do your research and weigh all your options before making a decision.

Once you have Laravel Octane in place, look out for the "Laravel Octane Compatible" badge on packages you add to your project. This indicates that the package has been tested to work with Laravel Octane out of the box, saving you time on configuration.

Laravel Octane compatible badge for high-performance applications.

Credits to Bill Jelen for the great image.