LaraflareDocsFrameworkPackagesReleases13.x

Docs / Packages / Octane

Octane

Introduction

Octane (@laraflare/octane) supercharges your application's performance by serving your application using a long-running application server: Node's own HTTP server, answering from one worker process per CPU. Octane boots your application once, keeps it in memory, and then feeds it requests at supersonic speeds.

Octane is how a Laraflare application is served on a server, a hosting platform or in a container: a new application requires it, and its npm start script runs octane:start --server=node. See the deployment documentation.

Note

Deployed to Cloudflare, the application runs as a Worker, which is its own server: Octane is not used there.

Installation

A new application already includes Octane. To add it to an existing application, install it with npm:

npm install @laraflare/octane

After installing Octane, you may publish Octane's configuration file into your application, and set the OCTANE_SERVER environment variable in your .env file to node, the server Laraflare runs on:

npx artisan vendor:publish --tag=octane-config
OCTANE_SERVER=node

Note

The octane:install Artisan command offers only Octane's other servers, which do not serve a Laraflare application (below). Publish the configuration file as shown above instead.

Server Prerequisites

The Node server needs nothing beyond Node 22 or newer, which your application already requires.

Note

Octane's other servers (RoadRunner, Swoole, and one built on Caddy) run workers of another language's runtime, so they cannot serve a Laraflare application: their commands, such as octane:roadrunner and octane:swoole, stop at their own checks. Everything on this page is about the node server.

Serving Your Application

The Octane server can be started via the octane:start Artisan command. By default, this command will utilize the server specified by the server configuration option of your application's octane configuration file:

npx artisan octane:start

By default, Octane will start the server on port 8000, so you may access your application in a web browser via http://localhost:8000. The server serves the files in your application's public directory before handing a request to your application.

Keeping Octane Running in Production

If you are deploying your Octane application to production, you should use a process monitor such as systemd or pm2 to ensure the Octane server stays running. A sample systemd unit for Octane might look like the following:

[Unit]
Description=example-app
After=network.target

[Service]
WorkingDirectory=/home/forge/example.com
ExecStart=/usr/bin/npx artisan octane:start --server=node --host=127.0.0.1 --port=8000
Restart=always
User=forge

[Install]
WantedBy=multi-user.target

The deployment documentation shows the same with npm start and pm2.

Serving Your Application via HTTPS

By default, applications running via Octane generate links prefixed with http://. When the Node server sits behind a proxy that terminates TLS, the request's scheme is taken from the X-Forwarded-Proto header, so a proxy that sends X-Forwarded-Proto: https makes your application prefix all generated links with https://.

Note

The OCTANE_HTTPS environment variable, used within your application's config/octane.ts configuration file, is applied by the request handling of Octane's other servers. The Node server does not read it: have your proxy send the X-Forwarded-Proto header, as in the Nginx example below.

Serving Your Application via Nginx

In production environments, you may serve your Octane application behind a traditional web server such as Nginx or Apache. Doing so will allow the web server to serve your static assets such as images and stylesheets, as well as manage your SSL certificate termination.

In the Nginx configuration example below, Nginx will serve the site's static assets and proxy requests to the Octane server that is running on port 8000:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    server_tokens off;
    root /home/forge/domain.com/public;

    charset utf-8;

    location / {
        try_files $uri @octane;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/domain.com-error.log error;

    location @octane {
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_pass http://127.0.0.1:8000;
    }
}

Watching for File Changes

Since your application is loaded in memory once when the Octane server starts, any changes to your application's files will not be reflected when you refresh your browser. For example, route definitions added to your routes/web.ts file will not be reflected until the server is restarted.

Note

The --watch flag of octane:start is passed to Octane's other servers only; the Node server does not watch your files. During local development, run your application with npm run dev or npx artisan serve (installation) instead, and restart Octane, or reload its workers, after changing your code.

Specifying the Worker Count

By default, Octane will start an application request worker for each CPU core provided by your machine. These workers will then be used to serve incoming HTTP requests as they enter your application. You may manually specify how many workers you would like to start using the --workers option when invoking the octane:start command:

npx artisan octane:start --workers=4

Specifying the Max Request Count

To help prevent stray memory leaks, Octane gracefully restarts any worker once it has handled 500 requests. To adjust this number, you may use the --max-requests option:

npx artisan octane:start --max-requests=250

A new worker is started first, and the old one finishes the requests it holds and exits once the new one is listening, so no request is dropped.

Specifying the Max Execution Time

The max_execution_time option in your application's config/octane.ts configuration file sets the maximum number of seconds an incoming request is allowed to execute before being terminated, 30 by default:

max_execution_time: 30,

Note

Octane's other servers apply this limit. The Node server does not: a request runs until it finishes, so put a limit on slow work yourself (an HTTP client's timeout, or a queued job).

Reloading the Workers

You may gracefully restart the Octane server's application workers using the octane:reload command. Typically, this should be done after deployment so that your newly deployed code is loaded into memory and is used to serve to subsequent requests:

npx artisan octane:reload

Each worker is replaced in turn, the next once the one before has finished its requests.

Stopping the Server

You may stop the Octane server using the octane:stop Artisan command:

npx artisan octane:stop

Checking the Server Status

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

npx artisan octane:status

Dependency Injection and Octane

Since Octane boots your application once and keeps it in memory while serving requests, there are a few caveats you should consider while building your application. For example, the register and boot methods of your application's service providers will only be executed once when the request worker initially boots. On subsequent requests, the same application instance will be reused.

In light of this, you should take special care when injecting the application service container or request into any object's constructor. By doing so, that object may have a stale version of the container or request on subsequent requests.

Laraflare handles the framework's own state between requests. However, it does not always know how to reset the global state created by your application. Therefore, you should be aware of how to build your application in a way that is Octane friendly. Below, we will discuss the most common situations that may cause problems while using Octane.

Container Injection

In general, you should avoid injecting the application service container or HTTP request instance into the constructors of other objects. For example, the following binding injects the entire application service container into an object that is bound as a singleton:

import type { Application } from '@laraflare/framework';
import { Service } from '../Service';

/**
 * Register any application services.
 */
register(): void {
  this.app.singleton(Service, (app: Application) => {
    return new Service(app);
  });
}

In this example, if the Service instance is resolved during the application boot process, the container will be injected into the service and that same container will be held by the Service instance on subsequent requests. This may not be a problem for your particular application; however, it can lead to the container unexpectedly missing bindings that were added later in the boot cycle or by a subsequent request.

As a work-around, you could either stop registering the binding as a singleton, or you could inject a container resolver closure into the service that always resolves the current container instance:

import { Container, type Application } from '@laraflare/framework';
import { Service } from '../Service';

this.app.bind(Service, (app: Application) => {
  return new Service(app);
});

this.app.singleton(Service, () => {
  return new Service(() => Container.getInstance());
});

The global app helper and the Container.getInstance() method will always return the latest version of the application container.

Request Injection

In general, you should avoid injecting the application service container or HTTP request instance into the constructors of other objects. For example, the following binding injects the entire request instance into an object that is bound as a singleton:

import type { Application } from '@laraflare/framework';
import { Service } from '../Service';

/**
 * Register any application services.
 */
register(): void {
  this.app.singleton(Service, (app: Application) => {
    return new Service(app.make('request'));
  });
}

In this example, if the Service instance is resolved during the application boot process, the HTTP request will be injected into the service and that same request will be held by the Service instance on subsequent requests. Therefore, all headers, input, and query string data will be incorrect, as well as all other request data.

As a work-around, you could either stop registering the binding as a singleton, or you could inject a request resolver closure into the service that always resolves the current request instance. Or, the most recommended approach is simply to pass the specific request information your object needs to one of the object's methods at runtime:

import type { Application } from '@laraflare/framework';
import { Service } from '../Service';

this.app.bind(Service, (app: Application) => {
  return new Service(app.make('request'));
});

this.app.singleton(Service, (app: Application) => {
  return new Service(() => app.make('request'));
});

// Or...

service.method(request.input('name'));

The global request helper will always return the request the application is currently handling and is therefore safe to use within your application.

Note

It is acceptable to accept the Request instance in your controller methods and route closures.

Configuration Repository Injection

In general, you should avoid injecting the configuration repository instance into the constructors of other objects. For example, the following binding injects the configuration repository into an object that is bound as a singleton:

import type { Application } from '@laraflare/framework';
import { Service } from '../Service';

/**
 * Register any application services.
 */
register(): void {
  this.app.singleton(Service, (app: Application) => {
    return new Service(app.make('config'));
  });
}

In this example, if the configuration values change between requests, that service will not have access to the new values because it's depending on the original repository instance.

As a work-around, you could either stop registering the binding as a singleton, or you could inject a configuration repository resolver closure to the class:

import { Container, type Application } from '@laraflare/framework';
import { Service } from '../Service';

this.app.bind(Service, (app: Application) => {
  return new Service(app.make('config'));
});

this.app.singleton(Service, () => {
  return new Service(() => Container.getInstance().make('config'));
});

The global config will always return the latest version of the configuration repository and is therefore safe to use within your application.

Managing Memory Leaks

Remember, Octane keeps your application in memory between requests; therefore, adding data to a statically maintained array will result in a memory leak. For example, the following controller has a memory leak since each request to the application will continue to add data to the static data array:

import { Str, type Request } from '@laraflare/framework';
import { Service } from '../../Service';

/**
 * Handle an incoming request.
 */
index(request: Request): Record<string, unknown> {
  Service.data.push(Str.random(10));

  return {
    // ...
  };
}

While building your application, you should take special care to avoid creating these types of memory leaks. It is recommended that you monitor your application's memory usage during local development to ensure you are not introducing new memory leaks into your application.

Concurrent Tasks

You may execute several operations and collect their results using Octane's concurrently method. You may combine this method with array destructuring to retrieve the results of each operation:

import { Octane } from '@laraflare/octane/Facades/Octane';
import { User } from '../Models/User';
import { Server } from '../Models/Server';

const [users, servers] = await Octane.concurrently([
  () => User.all(),
  () => Server.all(),
]);

Note

Octane runs these tasks in Swoole's "task workers", which the Node server does not have: there, concurrently runs the tasks one after another in the worker handling the request and answers their results in the same shape. To run independent work at the same time, see concurrency.

The Octane Cache

Your application's config/cache.ts configuration file defines an octane cache store, which uses the Octane cache driver:

import { Cache } from '@laraflare/framework';

await Cache.store('octane').put('framework', 'laraflare', 30);

Note

Octane's cache is powered by Swoole tables, which share their data between all of a server's workers. The Node server has no Swoole tables, so the octane store is kept in each worker's own memory: a value is seen only by the worker that stored it, and it is gone when the worker is replaced. Use a shared store, such as the database or Redis, for data every worker must see.

© 2026 Hugh Smith. Adapted from Laravel's documentation, Copyright (c) Taylor Otwell, used under the MIT License.