What is Laravel Octane? How to use it with Laravel?

What is Laravel Octane? How to use it with Laravel?

Introduction

Laravel Octane is the most recent release from the Laravel Team. The use of Swoole or RoadRunner gives our Laravel application a boost in speed.

When a request is made, Laravel Octane merely caches the bootstrapped Laravel framework in RAM. Instead of reading the files from the hard drive and restarting the framework with each subsequent request, the framework is simply used from RAM that has already been bootstrapped. And that's the key concept behind the significant performance improvement of the framework.

Installation

Octane may be installed via the Composer package manager:

composer require laravel/octane

After installing Octane, you may execute the octane:install Artisan command, which will install Octane's configuration file into your application:

php artisan octane:install

Octane publish its configuration file, which looks like this:

return [

    /*
    |--------------------------------------------------------------------------
    | Octane Server
    |--------------------------------------------------------------------------
    |
    | This value determines the default "server" that will be used by Octane
    | when starting, restarting, or stopping your server via the CLI. You
    | are free to change this to the supported server of your choosing.
    |
    */

    'server' => env('OCTANE_SERVER', 'roadrunner'),

    /*
    |--------------------------------------------------------------------------
    | Force HTTPS
    |--------------------------------------------------------------------------
    |
    | When this configuration value is set to "true", Octane will inform the
    | framework that all absolute links must be generated using the HTTPS
    | protocol. Otherwise your links may be generated using plain HTTP.
    |
    */

    'https' => env('OCTANE_HTTPS', false),

    /*
    |--------------------------------------------------------------------------
    | Octane Listeners
    |--------------------------------------------------------------------------
    |
    | All of the event listeners for Octane's events are defined below. These
    | listeners are responsible for resetting your application's state for
    | the next request. You may even add your own listeners to the list.
    |
    */

    'listeners' => [
        WorkerStarting::class => [
            EnsureUploadedFilesAreValid::class,
        ],

        RequestReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            ...Octane::prepareApplicationForNextRequest(),
            //
        ],

        RequestHandled::class => [
            //
        ],

        RequestTerminated::class => [
            //
        ],

        TaskReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            //
        ],

        TickReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            //
        ],

        OperationTerminated::class => [
            FlushTemporaryContainerInstances::class,
            // DisconnectFromDatabases::class,
            // CollectGarbage::class,
        ],

        WorkerErrorOccurred::class => [
            ReportException::class,
            StopWorkerIfNecessary::class,
        ],

        WorkerStopping::class => [
            //
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Warm / Flush Bindings
    |--------------------------------------------------------------------------
    |
    | The bindings listed below will either be pre-warmed when a worker boots
    | or they will be flushed before every new request. Flushing a binding
    | will force the container to resolve that binding again when asked.
    |
    */

    'warm' => [
        ...Octane::defaultServicesToWarm(),
    ],

    'flush' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | Garbage Collection Threshold
    |--------------------------------------------------------------------------
    |
    | When executing long-lived PHP scripts such as Octane, memory can build
    | up before being cleared by PHP. You can force Octane to run garbage
    | collection if your application consumes this amount of megabytes.
    |
    */

    'garbage' => 50,

    /*
    |--------------------------------------------------------------------------
    | Maximum Execution Time
    |--------------------------------------------------------------------------
    |
    | (info) 0 means no maximum limit
    |
    */

    'max_execution_time' => 30,

    /*
    |--------------------------------------------------------------------------
    | Octane Cache Table
    |--------------------------------------------------------------------------
    |
    | While using Swoole, you may leverage the Octane cache, which is powered
    | by a Swoole table. You may set the maximum number of rows as well as
    | the number of bytes per row using the configuration options below.
    |
    */

    'cache' => [
        'rows' => 1000,
        'bytes' => 10000,
    ],

    /*
    |--------------------------------------------------------------------------
    | Octane Swoole Tables
    |--------------------------------------------------------------------------
    |
    | While using Swoole, you may define additional tables as required by the
    | application. These tables can be used to store data that needs to be
    | quickly accessed by other workers on the particular Swoole server.
    |
    */

    'tables' => [
        'example:1000' => [
            'name' => 'string:1000',
            'votes' => 'int',
        ],
    ],

];

Swoole

Swoole is a high-performance network framework for PHP that is built on an event-driven, asynchronous, non-blocking I/O coroutine programming architecture.

Designed for the development of large-scale concurrent systems. It is written in C/C++ and works as a PHP extension. Allowing PHP writers to create code more efficiently by utilising event-loops and fibers/coroutines.

Typically, PHP - Swoole Extension can be done via PECL:

pecl install swoole

Serving Your Application

php artisan octane:start

By default, Octane will start the server on port 8000, so you may access your application in a web browser via localhost:8000.

Checking The Server Status

You may check the current status of the Octane server using the octane:status Artisan command:

php artisan octane:status

Stopping The Server

php artisan octane:stop

Benchmark

Let’s see how Laravel Octane and Nginx + PHP-FPM

Laravel over Nginx + PHP-FPM

image.jpg

Laravel + Octane

Screenshot from 2022-09-23 22-58-37.jpg

Octane takes Laravel applications’ performance to a new level. However, it brings with it some considerations because of the way it works.

I'm excited for the future of Octane!. Are you?

For Laravel Octane Installation: https://github.com/happykaushik/octane-installation
For Laravel Optimization: https://blog.keytech.dev/optimizing-laravel-performance-how-to-use-octane-with-laravel-vapor
10 Packages to Increase Productivity: https://blog.keytech.dev/best-10-laravel-packages-that-can-help-increase-your-productivity

For more information on Laravel Octane, check out its official documentation

Did you find this article valuable?

Support Laravel Tips & Tutorials - KeyTech Blog by becoming a sponsor. Any amount is appreciated!