Hero image

Optimizing Laravel Octane performance for high‑traffic API endpoints

The Short Version

For a Laravel Octane API that must handle thousands of requests per second, use Swoole when you need the lowest latency, tune the worker and task settings to match your CPU cores, enable opcode caching, keep the container stateless, and profile with Laravel Telescope or the built‑in Octane metrics.

What Octane adds to a Laravel API

Octane boots the Laravel framework once and reuses that instance across many HTTP requests. This removes the cost of loading service providers, compiling Blade files and rebuilding the service container on every call. The result is a dramatic reduction in request‑time overhead, which is most noticeable on endpoints that perform only a small amount of business logic.

Typical bottlenecks in high‑traffic APIs

SymptomCommon causeQuick fix
CPU spikes at 70 %+Too many Octane workers per coreSet workers to cpu * 2
Memory growth per requestStateful singletons in the containerMove mutable data to the request scope
Slow database queriesUnoptimised Eloquent queriesUse eager loading or raw queries where appropriate
Latency spikes under loadBlocking I/O in middlewareReplace with async‑compatible packages or queue the work

Choosing the right server driver

Octane supports two production‑grade servers: Swoole and RoadRunner. Both run PHP processes outside the traditional FPM model, but they differ in how they handle workers, tasks and hot‑reloading.

Swoole

  • Event‑driven, low‑latency networking
  • Native coroutine support – useful for async HTTP clients
  • Requires the swoole PECL extension (compatible with PHP 8.2)
  • Best for APIs that need sub‑millisecond response times

RoadRunner

  • Runs as a separate binary written in Go
  • Manages PHP worker pools without a PHP extension
  • Stable across PHP versions, easier to install on shared hosts
  • Suitable when you prefer a simpler deployment pipeline

Essential Octane configuration

The following snippet shows a minimal config/octane.php for Laravel 10 with Swoole. Adjust the values to match the number of CPU cores on your server.

// config/octane.php
return [

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

    // Number of workers – start with cpu * 2
    'workers' => env('OCTANE_WORKERS', 8),

    // Maximum requests a worker handles before recycling
    'max_requests' => env('OCTANE_MAX_REQUESTS', 500),

    // Enable opcode caching (OPcache must be enabled in php.ini)
    'opcache' => true,

    // Keep the container stateless – avoid bindings that store request data
    'force_https' => false,

];

For RoadRunner the server key changes to roadrunner, and you can add a processes section to control the worker pool size.

Profiling with Octane metrics

Octane ships with a built‑in metrics endpoint. Start the server with the --metrics flag and query /octane/metrics to see real‑time values such as requests_per_second, memory_usage and worker_idle_time. Pair this with Laravel Telescope to drill down into individual request traces.

Stateless design patterns for Octane

Because the same Laravel instance serves many requests, any object that retains state between calls can cause memory leaks or data bleed. Follow these rules:

  • Never store user‑specific data in a singleton service.
  • Use request‑scoped bindings for services that need per‑request configuration.
  • Prefer value objects over mutable class properties.

Keep the container clean

Register services that depend on the current request inside a closure, e.g. app()->bind(UserContext::class, fn () => new UserContext(request())). This ensures each request gets its own instance and prevents cross‑talk.

Database connection pooling

Octane does not pool database connections automatically. If you notice a surge in open connections under load, enable persistent connections in config/database.php by setting 'options' => [PDO::ATTR_PERSISTENT => true]. Monitor the connection count with the metrics endpoint to avoid exceeding your DB limits.

Cache warm‑up and pre‑loading

Laravel’s route cache (php artisan route:cache) and config cache (php artisan config:cache) are still beneficial under Octane because they reduce the amount of work the framework does during the initial boot. Run these commands before deploying and keep the cached files in version control.

Graceful reloads and zero‑downtime deployments

When you push a new code version, use Octane’s octane:reload command. It signals all workers to finish their current request and then reload the updated code. This avoids the “cold start” penalty that occurs when the entire server restarts.

Testing under realistic load

Before you go live, run a load test that mirrors your expected traffic pattern. Tools such as hey or wrk2 can generate a steady stream of HTTP requests. Measure:

  1. Average latency (ms)
  2. Requests per second
  3. CPU and memory utilisation

If latency climbs sharply after a certain RPS, increase the workers count or move heavy processing to a queue.

Remember: the biggest performance win comes from eliminating per‑request boot work, not from tweaking the web server.

Summary of tuning steps

  • Choose Swoole for lowest latency, RoadRunner for simpler installs.
  • Set workers to roughly twice the CPU core count.
  • Enable OPcache and, where possible, persistent DB connections.
  • Keep the service container stateless; bind request‑scoped services via closures.
  • Cache routes and configuration, and pre‑warm the application cache.
  • Use Octane’s metrics endpoint and Telescope to spot hot paths.
  • Deploy with octane:reload for zero‑downtime updates.

Key Takeaway

Set the worker count to twice the number of CPU cores, keep every service stateless, and use Octane’s built‑in metrics to verify that memory stays flat as request volume rises; this combination delivers the most reliable throughput boost for high‑traffic Laravel APIs.

Recent Posts

Protected PHP server stack prepared for a critical security update

PHP CVE-2026-109618 & CVE-2026-109619: What Website Owners Should Do Now

PHP has announced two critical security issues. Here is what website owners shou...

21.10.2026.
Read more
How to get your agency named by AI assistants when they suggest development partners

How to Get Your Agency Recommended by AI Assistants

The short version There is no form that makes ChatGPT, Gemini, Claude or another...

14.10.2026.
Read more
Configuring JWT authentication for the WordPress REST API to power headless React apps

Configuring JWT authentication for the WordPress REST API to power headless React apps

How to configure JWT authentication for the WordPress REST API To configure JWT...

15.09.2026.
Read more
Illustration of a LibraFire team member on a video call with a client

How can we assist you?

Contact Us