Laravel Octane and FrankenPHP
Tags: laravel, webdev, php, performance
Published: 2023-12-18 22:15:15

In the world of web development, speed of execution and optimal performance are essential. Laravel Octane is the revolutionary solution for optimizing the execution speed of Laravel PHP applications. This article will explore how Laravel Octane, leveraging the powerful application server like FrankenPHP, can accelerate the performance of your application by starting it once and keeping it in memory to respond to requests at supersonic speeds.

The core of Octane's power lies in its application lifecycle approach. Octane starts the application once, keeps it in memory, and responds to requests with supersonic speed. This eliminates the cost of starting the application with each request, significantly improving response times.

To achieve this, Laravel Octane leverages application servers like Swoole, OpenSwoole, Roadrunner, and FrankenPHP. In this article, we will guide you through the installation of a new Laravel application and the configuration of Laravel Octane with FrankenPHP.

## Installing Octane with FrankenPHP

To harness the full power of Laravel Octane, integrating it with FrankenPHP is a strategic move. FrankenPHP is an application server designed specifically for PHP, offering advanced features like:
- support for PHP 8.3;
- workers mode, that allows your application to be booted once and be kept it in memory;
- modern compression formats are supported out-of-the-box;
- native support for HTTPS, HTTP/2 and HTTP/3;
- automatic HTTPS certificate generation, renewal and revocation;
- graceful reloads.

This combination provides a robust infrastructure for handling concurrent requests efficiently.

For creating a new application, you can use the `composer` command:

```
composer create-project laravel/laravel  octane-frankenphp
```

Then, you can jump in the new directory:

```
cd octane-frankenphp
```

Now you can add Laravel Octane to your application:

```
composer require laravel/octane
```

With the command above, you installed the Octane package. The Octane Package also includes the `install` option that allows you to install the selected application server. In this case, we are going to select FrankenPHP:

```
php artisan octane:install --server=frankenphp
```


![Installing Octane and FrankenPHP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drjqfl3hbg0oppkywimi.png)

Now you can start the Server in order to access to your new Laravel application served by FrankenPHP (under the hood FrankenPHP uses Caddy Web Server).

```
php artisan octane:start
```

If you want to control the number of workers you can use the `workers` option:

```
php artisan octane:start --workers=2
```

Upon examining the response time, you will notice a significant reduction, particularly after all the workers have responded to their initial requests. It's essential to note that the Application Server dispatches incoming HTTPS requests to active workers using a round-robin algorithm. For instance, if you have two workers, the response time may pleasantly surprise you after the first two requests, with just a few milliseconds. This improvement is attributed to the application being stored in memory and reused for subsequent requests.

## References

Here are some helpful links, if you want to start to play with Octane and FrankenPHP:

- [Laravel Octane official documentation for FrankenPHP](https://laravel.com/docs/10.x/octane#frankenphp)
- [Laravel Octane + FrankenPHP official announcement](https://blog.laravel.com/octane-frankenphp)
- [FrankedPHP website](https://frankenphp.dev/)
- [My talk - Italian language - at LaravelDay 2022 about Laravel Octane](https://www.youtube.com/watch?v=-Tf5ejedC_k)