Search by

cairophp / laravel

samirzz

Laravel on the Cairo application server

Package info

github.com/cairophp/laravel

pkg:composer/cairophp/laravel

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-12-03 17:41 UTC

This package is auto-updated.

Last update: 2026-12-03 21:26:24 UTC


README

Laravel on the Cairo application server.

A PHP thread boots one app, and each request it runs at the same time gets a world on that app: its own values for everything the app's objects and static properties hold. A request starts with the app as boot left it and leaves nothing in it. Nothing in Laravel or in a package is replaced or patched for it, and the app needs no Laravel Octane.

Not a release yet. It needs a Cairo with Cairo\World::keep() (cairophp/cairo#138, merged, in the release after 0.11.0). To serve a Laravel app on a released Cairo today, use cairophp/laravel-octane, the Octane driver.

The idea

In Cairo's fiber mode a PHP thread runs many requests at once, each in its own fiber. A Laravel app keeps the state of the request it serves in its own objects (the container, the router, the auth manager, a package's singleton), so the Octane driver boots one app for each request a thread has in flight: 64 at once are 64 booted apps.

Here a thread boots one app. Then it takes every object the app is made of (what can be reached from the container and from static properties, through properties, arrays and closures) and hands them to Cairo (Cairo\World::keep()): from there on each world has its own values for the properties of those objects, as it already has for static properties, and starts with what they held after boot. A request runs in a world that no other request is in. The objects, and everything in them that no request changes, exist once, and Cairo swaps the values when it switches between two requests.

Measured on a Laravel 13 app with 46 packages, 256 requests in flight on four threads: every one of the 1,664 checks of the isolation suite right, the speed of a booted app for each request, and 28 to 38% less memory.

Try it

The package is not on Packagist.

composer config repositories.cairo-laravel vcs https://github.com/cairophp/laravel
composer require cairophp/laravel:dev-main
php artisan cairo:install
php artisan cairo:serve

cairo:install publishes two files into the app:

  • server.php in the app's root, the Cairo declaration. cairo:serve runs cairo serve server.php, and so can a container or a service file, with no artisan in between. It is yours: Laravel answers from the fallback route, so routes of your own on $http (a WebSocket endpoint, a health check) and other servers on $app (gRPC) go next to it.
  • config/cairo.php, what the runtime reads when a thread boots the app.
Command What it does
cairo:install Publishes server.php and config/cairo.php (--force overwrites them)
cairo:serve Runs the server in the foreground. --watch reloads the app when its files change; -v has each thread log how many objects it found, how many values a request has of its own, and what stays one for all requests
cairo:reload New threads boot the app again and take over; the requests in flight end on the old ones. For new code after a deploy. A change of the address or of the number of threads needs a restart
cairo:stop Stops the server once the requests it holds have ended
cairo:status Says whether the server is running, with exit code 0 or 1

cairo:reload, cairo:stop and cairo:status find the server that cairo:serve started through a file in storage/logs. A server that was started with cairo serve server.php directly takes the signals themselves: SIGUSR1 reloads, SIGTERM stops.

config/cairo.php cairo:serve Default Meaning
host, port --host, --port 127.0.0.1, 8000 Where the server listens
threads --threads auto PHP threads, each with one booted app (auto: one for each CPU)
in_flight --in-flight 8 Requests a thread runs at once, each in a world on the thread's app
max_requests --max-requests 500 Requests for each of those before the thread is replaced; 0 for never
timeout --timeout 30 The seconds a request may take, see Deadlines
pool --pool request Database connections and Redis clients from pools of the thread, see Connection pools: request, statement or off
pool_max --pool-max in_flight The connections a thread opens to one server
warm --warm none Paths that each thread requests once before it serves, see Warm boot
leaks --leaks off The leak watch, see What stays shared
--binary The path to the cairo binary, when it is not on PATH or in the app's root

An option of cairo:serve wins over the configuration file, also when the configuration is cached.

A new world for each request

A request runs in a new world, through Laravel's HTTP kernel (its middleware, the router, terminate()): it starts with the app as boot left it, as under PHP-FPM, and what it did to the app is gone when it ends. Nothing has to be reset, so there is no list of resets that a package can be missing from.

The other way, a world that is used again and reset between two requests by Laravel Octane's listeners, was built first (the package ran Octane's worker). Both ways were measured, on isolation and on speed, before that one was removed.

Isolation: tests/fixture/isolation-routes.php has one request set state in 28 places where code keeps it, and a later request say what it still finds. With a new world it finds 5, all of them PHP's own state of a thread, which no option gives a request for itself (a static variable of a function, a global variable, the time zone, an ini_set(), error_reporting()). With Octane's resets it finds 16: those 5, and a static property of an app class, a property of a service that boot made, an event listener, a macro, Model::unguard(), Model::preventLazyLoading(), Carbon's test time, Number's locale, data shared with the views, the query log switch and the count of open transactions.

Speed (cloud VM, PHP 8.6, a Laravel 13 app, one PHP thread with 64 requests at once; microseconds of the thread for a request, median of four runs):

hello Two queries Queries and an HTTP call
Octane's worker in a world that is used again 794 2,182 3,614
A new world, nothing done before the requests 488 2,595 4,148
A new world after the warm boot (what the package does) 483 1,928 3,089
A new world after the warm boot and three warm paths 462 1,622 3,076
For the bound: a world that is used again, with no reset at all 369 1,642 3,356

The suite of an app with 46 packages gives 1,664 of 1,664 checks right with each of them.

Warm boot

A new world costs a few microseconds. What costs is what Laravel does at first use and then remembers: a model's boot, the inflector behind its table name, a service the container builds, a compiled route. A request that starts with the app as boot left it does those again, so the thread does them once, before its requests, and every world starts with them done:

  • Always: the services nearly every request asks the container for are made (auth, cache, the router, the session, views and so on), the Eloquent models of app/Models are booted, with the inflector, and the object of the default database connection is made (it connects at its first query). No request is involved, so nothing of a request is left in the app.
  • With warm in config/cairo.php (or --warm=/ --warm=/login): each thread also requests those paths once, through the HTTP kernel, before it serves. Whatever a request builds on its way is then built. Afterwards the container holds what it held before (the instances the warm requests made are forgotten, the scoped ones too, and the auth guards), as at the end of a request's lifecycle. What a warm request left elsewhere, in a static or in a service that boot made, is where every request starts: name pages that any visitor gets, run the app's tests against the server, and let --leaks tell what the requests share. A warm path that fails is logged (warm: GET /x answered 500) and the server serves without it.

Connection pools

No world and no request owns a connection. A thread has a pool for each database and each Redis server its app connects to, as a Go program has its sql.DB, and a request takes a connection from there when it runs a statement. --pool=off turns the pools off: each world then opens its own connections and keeps them. The Laravel objects stay what they are (DB::connection() is the app's own Connection, with its grammar, its events and its count of open transactions; Redis::connection() is Laravel's Predis connection), so an app changes nothing: only where the PDO and the Predis client under them come from.

--pool A request's database connection
request Taken at its first statement and kept until the request ends. Everything a session of the database keeps works: SET, temporary tables, locks
statement Taken for each statement and given back after it, as in Go. A transaction and a cursor keep it until they end. The requests of a thread then need as many connections as they have statements under way at once, not one each. What a session keeps between two statements is lost, so an app that counts on it stays on request

A Redis command takes a client and gives it back, under either setting. What goes on over several commands keeps its client: from MULTI or WATCH until EXEC, DISCARD or UNWATCH, and for the rest of the request after client().

When every connection of a pool is out, a request waits for one, and only its fiber waits. A transaction that a request left open is rolled back when its connection goes back. A request that ends between MULTI and EXEC has its client closed.

A connection in config/database.php, of a database or of Redis, takes a pool entry:

'mysql' => [
    'driver' => 'mysql',
    // ...
    'pool' => [
        'max' => 16,          // connections a thread opens (--pool-max, or the concurrency)
        'wait' => 2.0,        // seconds a request waits for a free one before it fails (none: until its deadline)
        'lifetime' => 1800,   // seconds before a connection is retired (0: never)
        'idle_time' => 300,   // seconds unused before it is retired (0: never)
        'hold' => 'request',  // or 'statement'; databases only
    ],
],
'sqlite' => ['driver' => 'sqlite', /* ... */ 'pool' => false],   // this one stays Laravel's own

How it is hooked: Connection::resolverFor(), Laravel's own way to name the class of a driver's connections, for mysql, mariadb, pgsql and sqlsrv (src/Database), and RedisManager::extend() for predis (src/Redis). The pooled classes extend Laravel's, because Laravel asks a connection for its class in the cache, the queue and the rate limiter.

What is not pooled, and stays as Laravel has it: SQLite, a connection that was given a PDO by hand, the phpredis client (not built yet), and Redis clusters. hold: statement is taken for MySQL and MariaDB (on a Laravel that reads the id of an inserted row inside the statement) and PostgreSQL; elsewhere a request keeps its connection. The tasks of Concurrency::run() each get a connection of their own.

Concurrency

Laravel's Concurrency::run() works as it is written, and without processes. On a Cairo thread the runtime adds a cairo driver and makes it the default where the configuration names process or fork (a sync or a driver of your own stays):

[$users, $orders] = Concurrency::run([
    fn () => User::count(),
    fn () => Order::where('status', 'open')->count(),
]);

Each task is a fiber of the thread that runs the request, so their waits overlap, and each has a world of its own on the thread's app:

  • A task starts with the app as boot left it, as a task of Laravel's process driver starts with a new app, and what it does to the app is gone with it. It has the request's Context (Laravel's, as with the process driver) and nothing else of the request: not its user, not its route.
  • Its database statements run on a connection of its own from the thread's pool, beside the request's, so two queries of one request really run at once.
  • Nothing is serialized: a task returns its result as it is, also an object or a closure.
  • A task that throws gives the caller its own exception, once the other tasks have ended. With a timeout (Laravel 13) the tasks are given up and Cairo\Context\DeadlineExceeded is thrown.

Laravel Octane's own API (Octane::concurrently(), Octane::tick(), its tables and cache) is not part of this package: an app that uses it runs on cairophp/laravel-octane, the Octane driver.

Deadlines

A request may take timeout seconds (30, as Laravel Octane's max_execution_time), counted from when the server has read its head: reading a slow upload and waiting for a free place on a thread are part of them. Past them:

  • The client gets a 504, whatever the request's code does then.
  • What the request waits for (a query, an HTTP call, a sleep()) throws Cairo\Context\DeadlineExceeded, which Laravel reports as any exception. Code that never waits gets the same exception at its next loop or call.
  • Code that catches it and goes on is unwound 50 ms later the way exit() does it, as PHP ends a script at max_execution_time: no catch and no finally runs, destructors do.
  • Only that request is stopped. The requests beside it on the thread go on, and the thread is not replaced: the request's world is dropped with it, so the next request finds nothing of what it did to the app. Its database transaction is rolled back and its place in the connection pool is free.

A client that leaves does not stop its request, as under PHP-FPM and Octane: the request runs to its end, or to its timeout, so a request that writes in several steps is not cut between two of them. (Cairo's own rule, which a handler of a Cairo app gets, ends every wait of a request once its client has left. The runtime runs Laravel under a context of withoutCancel() for the time the request has.)

What stays shared

Each request has its own values for declared properties and static properties. What it does not have for itself:

  • The contents of PHP's own collections that boot made (an ArrayObject, a WeakMap, an SplObjectStorage), and properties added to an object that its class does not declare. -v lists the ones the app has.
  • Static variables of functions and methods, and global variables.
  • What PHP has once for a thread: the time zone of date_default_timezone_set(), a setting of ini_set(), error_reporting(). A request that changes one changes it for the requests beside it and after it.
  • An object that boot made and that nothing reachable from the container or a static property holds.
  • What a library keeps in C (a connection of an extension that boot opened). The database connections and Redis connections of Laravel are closed after boot.

The leak watch (leaks in config/cairo.php, or cairo:serve --leaks) looks after every request and logs each find once: a property that was added to an object of the app or the contents of one of PHP's collections that a request changed, and a property of the booted app that changed outside every world. It reads every object of the app each time, so it is for a test run.

What was tried and dropped

Worlds that are used again, with Laravel Octane's worker and its resets between two requests: slower than a new world after the warm boot, and a later request found 16 of 28 kinds of state where a new world leaves 5 (the numbers are under A new world for each request).

Sharing one app with no worlds at all, on a copy of the app for each request (commit 5a7d3fa): as fast, but PHP alone leaves two things shared that worlds keep apart, what a closure captured with use ($app) and the statics of classes that load after the server started, and 1,654 to 1,658 of the 1,664 checks were right.

Tests

tests/ServerTest.php serves a real Laravel app and checks that requests running at once in one app each keep their own user, route, request, URL, configuration, locale, view data, container instance, database transaction and what a closure captured, and that the leak watch names what they do share. tests/IsolationTest.php checks that a request finds nothing in the app of what an earlier one set in 28 places, that the models are booted before the first request, and that every request starts with what a warm request left. tests/DeadlineTest.php checks that a request past its timeout is answered 504 and stopped, waiting or not, that the request beside it and the one after it are untouched, that its transaction is rolled back, and that a request whose client left runs to its end. tests/CommandsTest.php installs, serves, asks for the status, reloads (a new thread boots the app again) and stops. tests/ConcurrencyTest.php checks that the tasks of Concurrency::run() overlap and each have their own state.

tests/fixture/setup.sh /tmp/app
CAIRO_LARAVEL_TEST_APP=/tmp/app CAIRO_BINARY=/path/to/cairo vendor/bin/phpunit

Without the two variables the tests skip.

tests/PoolTest.php and tests/RedisPoolTest.php check the connection pools against a real MySQL, MariaDB or PostgreSQL database and a real Redis server (the test app then needs predis/predis): requests at once each have their own connection, more requests than connections take turns, a transaction keeps its connection and its rows, and one that a request left open ends with it. They skip without CAIRO_LARAVEL_TEST_DB_DATABASE (with _DRIVER, _HOST, _PORT, _USERNAME, _PASSWORD beside it) and CAIRO_LARAVEL_TEST_REDIS_HOST (with _PORT and _DB; that database is flushed).

License

Apache-2.0