Back to all articles
Laravel Insights Dec 16, 2025 โˆ™ 1 min read

Mastering Laravel Octane for High Performance

Learn how to use Laravel Octane to supercharge your application's performance with practical examples and best practices.

A graphic representing Laravel Octane boosting application speed, with an engine and performance gauges.

Mastering Laravel Octane: Boosting Performance with Long-Running Workers

Learn how to use Laravel Octane to supercharge your application's performance with practical examples and best practices.

In the traditional PHP world, every incoming web request triggers a familiar cycle: the application boots up, processes the request, sends a response, and then shuts down entirely. This stateless, share-nothing architecture is simple and robust but comes with a performance cost. The overhead of bootstrapping the entire Laravel framework on every single request can become a significant bottleneck for high-traffic applications.

Enter Laravel Octane, a first-party package that revolutionizes how Laravel applications are served. Octane supercharges your application by using high-performance application servers like Swoole, RoadRunner, or FrankenPHP. It boots your application once, keeps it warm in memory, and then feeds it requests at supersonic speeds. This eliminates the repetitive bootstrapping cost, leading to a dramatic increase in requests per second and a significant reduction in latency.

This guide will walk you through mastering Laravel Octane, from understanding its core concepts to implementing it in your projects. We'll cover installation, configuration, practical coding considerations, and best practices to help you unlock a new level of performance for your Laravel applications.

How Does Laravel Octane Work?

Unlike the traditional PHP-FPM model, Octane leverages asynchronous, event-driven servers to run your Laravel application as a long-running process. Here’s the fundamental difference:

  • Traditional PHP-FPM: Each request starts from scratch. It loads all config files, registers service providers, and bootstraps the entire framework.
  • Laravel Octane: The application is booted only once when the Octane server starts. A pool of "workers" is created, and each worker holds a "warm" instance of your application in memory. When a request comes in, it's handed off to an available worker, processed, and a response is sent. The application instance remains in memory, ready for the next request.

This shift results in several key benefits:

  • Drastic Performance Boost: By eliminating boot-up overhead, applications can handle hundreds or even thousands of requests per second.
  • Reduced Latency: Responses are faster because the application framework, including the service container and route registrations, is already loaded.
  • Advanced Features: Depending on the server you choose (like Swoole), you can unlock advanced capabilities such as concurrent tasks, ticks, and high-speed caching.

Getting Started: Installation and Setup

Integrating Octane into your project is a straightforward process.

Step 1: Install Octane

First, pull the Octane package into your Laravel project using Composer.

composer require laravel/octane

Step 2: Run the Install Command

Next, execute the octane:install Artisan command. This will publish the octane.php configuration file to your config directory and help you choose an application server.

php artisan octane:install

During installation, Octane will prompt you to choose which server you want to use: Swoole, RoadRunner, or FrankenPHP. For this guide, we'll focus on Swoole and RoadRunner, two popular and powerful choices.

  • Swoole: A PHP extension written in C++ that enables asynchronous programming. It's feature-rich and offers things like concurrent tasks and built-in tables. You'll need to install the swoole PECL extension on your server.
  • RoadRunner: A high-performance PHP application server written in Go. It communicates with PHP workers over pipes and is generally easier to set up since it doesn't require a separate PHP extension. Octane will download the RoadRunner binary for you.

Step 3: Start the Octane Server

Once installed and configured, you can start the Octane server with a simple Artisan command.

# By default, Octane uses the server defined in your config/octane.php file
php artisan octane:start

By default, the server will run on http://127.0.0.1:8000. Your application is now being served by Octane!

For local development, you can use the --watch flag to automatically restart the server whenever you change a file.

php artisan octane:start --watch

Octane-Friendly Coding: Best Practices

Because your application now lives in memory between requests, you must be mindful of a few coding patterns to avoid issues like memory leaks or stale data.

1. Managing Memory Leaks

The most common pitfall is accidentally creating memory leaks by adding data to static arrays or properties that are never cleared. Since the application instance is long-lived, these arrays will grow indefinitely with each request, eventually consuming all available memory.

The Problem:

class ReportController extends Controller
{
    public static array $log = [];

    public function generate()
    {
        // This array will grow with every request, causing a memory leak.
        self::$log[] = 'Generated report at ' . now();

        // ...
    }
}

The Solution:
Avoid storing request-specific state in static properties or long-lived objects that are registered as singletons. Reset any necessary state at the beginning of each request or refactor the code to avoid static accumulation. Octane automatically restarts workers after a certain number of requests (500 by default) to help mitigate minor leaks, but writing clean code is the best defense. You can adjust this with the --max-requests flag:

php artisan octane:start --max-requests=1000

2. Handling Dependency Injection

Be cautious when injecting the service container (Application) or the Request object into the constructor of a singleton. A singleton object is created only once, so it will hold onto the container or request instance from the very first request it handled, leading to stale data on subsequent requests.

The Problem (Singleton with Request Injection):

// In a service provider
$this->app->singleton(MyService::class, function ($app) {
    // This injects the request from the FIRST request, which will be stale later.
    return new MyService($app->make('request'));
});

The Solution:
Instead of injecting the entire request object, pass the specific data your service needs into its methods at runtime. Alternatively, if you must have access to the current request, inject a resolver closure.

// Better: Pass data at runtime
public function handle(Request $request)
{
    $myService->doSomething($request->input('name'));
}

// Or: Use the `request()` helper, which always returns the current request
class MyService
{
    public function doSomething()
    {
        $name = request()->input('name');
        // ...
    }
}

Advanced Octane Features (with Swoole)

If you chose Swoole as your server, you unlock powerful features for concurrent processing.

Concurrent Tasks

Octane's concurrently method allows you to execute multiple operations at the same time, waiting for them all to finish. This is incredibly useful for fetching data from multiple external APIs simultaneously.

use Illuminate\Support\Facades\Http;
use Laravel\Octane\Facades\Octane;

[$users, $posts] = Octane::concurrently([
    fn () => Http::get('https://api.example.com/users')->json(),
    fn () => Http::get('https://api.example.com/posts')->json(),
], timeout: 3000); // 3-second timeout

return view('dashboard', ['users' => $users, 'posts' => $posts]);

This code runs both HTTP requests in parallel, significantly reducing the total wait time compared to running them sequentially.

High-Speed Caching and Tables

Swoole provides access to in-memory tables that offer extreme read/write speeds (millions of operations per second). Octane exposes this through its octane cache driver and Octane::table() method. This data is shared across all workers but is flushed when the server restarts.

To use it, first set your cache driver in .env:
CACHE_DRIVER=octane

Then, configure your table in config/octane.php:

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

Now you can interact with this high-speed table anywhere in your application:

use Laravel\Octane\Facades\Octane;

// Set a row
Octane::table('example')->set('user_1', [
    'name' => 'Taylor Otwell',
    'votes' => 100,
]);

// Get a row
$user = Octane::table('example')->get('user_1'); // ['name' => 'Taylor Otwell', 'votes' => 100]

// Increment a column value
Octane::table('example')->increment('user_1', 'votes');

When Is Octane the Right Choice?

Octane provides the most significant benefit for applications that are CPU-bound or handle a high volume of requests.

  • High-Traffic APIs: APIs that need to serve thousands of requests per minute will see a dramatic improvement in throughput and lower server costs.
  • Real-Time Applications: Applications with real-time components, like broadcasting events, benefit from the persistent process model.
  • Microservices: Lightweight services that need to respond with very low latency are perfect candidates.

However, Octane might be overkill for smaller, low-traffic sites like a personal blog or a simple brochure website. In those cases, the simplicity of a traditional PHP-FPM setup may be preferable.

Conclusion

Laravel Octane is a powerful tool that brings the performance of asynchronous, long-running servers to the elegance and productivity of the Laravel framework. By booting your application once and keeping it warm in memory, Octane delivers a massive performance boost that is essential for building scalable, high-traffic applications.

By understanding the new request lifecycle and adopting Octane-friendly coding practices—such as managing memory and handling dependency injection carefully—you can safely unlock its full potential. Whether you're building a lightning-fast API or a complex web application, mastering Octane is a crucial step toward creating a truly high-performance system.


Related articles

Continue exploring Laravel insights and practical delivery strategies.

Laravel consulting

Need senior Laravel help for this topic?

Let's adapt these practices to your product and deliver the next milestone.