Skip to content

Worker Mode (FrankenPHP) ​

The biggest performance win in HexaGen is FrankenPHP worker mode: PHP boots once and serves thousands of requests per process — eliminating cold-start overhead on every request.

How it works ​

php
// public/index.php
require_once __DIR__ . '/../vendor/autoload.php';

$kernel = new HexaGen\Core\Kernel();
$kernel->boot(); // runs ONCE

if (function_exists('frankenphp_handle_request')) {
    for ($i = 0; frankenphp_handle_request() && $i < 1000; ++$i) {
        $request  = Request::createFromGlobals();
        $response = $kernel->handle($request);
        $response->send();
        $kernel->terminate($request, $response);
        gc_collect_cycles();
    }
} else {
    // Traditional server fallback (Apache, Nginx, php -S)
    $request  = Request::createFromGlobals();
    $response = $kernel->handle($request);
    $response->send();
    $kernel->terminate($request, $response);
}

What gets cached between requests ​

ComponentCachedSafe because
UrlMatcherYesRoute definitions don't change at runtime
ReflectionMethod / ReflectionClassYesClass structure is immutable
Middleware instancesYesMiddlewares are stateless
DI ContainerYesBindings are immutable after boot

What gets reset on every request ​

Kernel::terminate() runs after every response and clears all per-request state:

php
public function terminate(Request $request, Response $response): void
{
    AuthManager::reset();    // clears cached authenticated user
    CurrentRequest::reset(); // clears request singleton
}

This prevents identity leaks between requests — the most critical bug in worker mode apps.

Starting the server ​

bash
php hexaphp server:start

If FrankenPHP is not installed, the command offers to download it automatically.

Performance comparison ​

ModeCold startMemoryRequests/sec
Traditional PHP-FPMEvery requestLow (per process)~500
FrankenPHP workerOnceHigher (shared)~5,000+

The framework is fully state-safe for worker mode — no static state leaks between requests except the intentional caches listed above.

Released under the MIT License.