Skip to content

How PAM Works

PAM (“PHP, Always in Memory”) is a PHP application server written in Rust. It embeds the Zend Engine through PHP’s official Embed SAPI, boots your application once, and then serves every request from that warm, already-loaded process. Rust owns the network, the process lifecycle and the event loop (Tokio); PHP owns your application code, Composer packages and business logic.

Compared with PHP-FPM, the big difference is what happens between requests:

Every request starts from an empty PHP state: Composer’s autoloader is loaded again, the framework boots again, configuration is parsed again, and everything is thrown away when the response is sent.

This page explains the moving parts. Read Request Lifecycle next for what exactly survives between requests, and Memory & State for how to write code that stays correct in a long-lived process.

A production PAM server started with pam start is a small tree of processes:

pam start index.php --workers 4
│
├─ master supervises workers, owns the control plane (/live, /ready, /metrics)
│ │ never runs application PHP
│ ├─ zygote (Linux) boots the Zend engine once and forks workers
│ ├─ worker 1 Tokio + Zend engine + your booted application
│ ├─ worker 2 Tokio + Zend engine + your booted application
│ ├─ worker 3 ...
│ └─ worker 4 ...

Each worker is a separate operating-system process with:

  • one Zend Engine (non-thread-safe build, so there are no ZTS locks);
  • one Tokio runtime that drives sockets, timers and other I/O;
  • exactly one thread that is allowed to call into PHP.

Workers share the listening port with SO_REUSEPORT, so the kernel spreads new connections between them. On Linux, sibling workers forked from the same zygote also rebalance keep-alive connections toward the least-loaded worker.

The master never executes PHP. It starts workers, restarts the ones that crash, recycles them, performs zero-downtime reloads and answers the health and metrics endpoints even when a PHP engine is busy or stuck. See Workers & Supervision.

When a worker starts, PAM performs these steps in order:

  1. php_embed_init() starts the Zend Engine with the real argv.
  2. PAM looks for the nearest composer.json (walking up from the entrypoint), honors config.vendor-dir, and loads vendor/autoload.php.
  3. PAM’s bundled runtime modules are evaluated: async I/O, streams, tasks, the WebSocket transport, observability, the HTTP primitives and the optional Laravel bridge.
  4. Your entrypoint runs once. It registers a handler (for example with Pam\Http\Server or Nimbus\App) and calls listen().
  5. Tokio opens the TCP/TLS listeners and starts dispatching requests to the handler. The entrypoint is never read again.

Here is the smallest complete application. Everything above the Server::create() call runs once per worker; the closure runs once per request:

server.php
<?php
declare(strict_types=1);
use Pam\Http\Request;
use Pam\Http\Response;
use Pam\Http\Server;
$bootedAt = microtime(true); // runs once, when the worker boots
Server::create(static function (Request $request, Response $response) use ($bootedAt): Response {
return $response->json([
'path' => $request->path,
'bootedAt' => $bootedAt, // the same value for every request on this worker
'pid' => getmypid(),
]);
})->listen(3000);
Terminal window
pam server.php

Since PAM 2.2.25, plain HTTP/1 connections are served on the PHP thread itself. Tokio’s single-threaded runtime reads the request, PHP runs the handler, and the response is written back on the same thread. There is no hand-off between an I/O thread and the PHP thread, which saves wake-ups and cache traffic on every request.

You can restore the previous model, where separate I/O threads own the sockets, with the PAM_IO_THREADS environment variable:

Terminal window
PAM_IO_THREADS=1 pam start index.php --workers 8
PAM_IO_THREADS Behavior
0 (default) HTTP/1 connections are served on the PHP thread
1 to 16 That many I/O threads per worker; each request crosses to the PHP thread and back

TLS listeners always use I/O threads. HTTP/2 (h2c and TLS ALPN h2) and HTTP/3 over QUIC are served by the same worker and reach the same PHP handler.

Every request runs in its own Fiber. When your code calls a PAM async operation, such as Pam\Async\delay(), the PAM HTTP client, DNS, file or process APIs, the Fiber suspends and control returns to Tokio. While that operation is pending, the same worker can start or resume other requests. When the operation completes, Tokio resumes exactly the Fiber that was waiting.

request A ──► handler ──► $client->get(...) (Fiber A suspends)
request B ──► handler ──► response (runs while A waits)
Tokio: HTTP reply arrives
request A ◄── resumed ──► response

This is cooperative concurrency: one worker runs one piece of PHP at a time, and switches only at PAM suspension points. Code that blocks inside a C extension (for example a synchronous database driver or sleep()) holds the whole worker until it returns. See Async I/O for the full list of non-blocking operations.

Every queue between Rust and PHP is bounded, so overload is predictable instead of turning into unbounded memory growth. The defaults, which you can change in the listen() options, include:

Option Default Meaning
maxConcurrentRequests 4096 Requests in flight per worker
phpExecutorQueueCapacity 1024 Work waiting for the PHP thread
requestTimeoutMs 30000 Hard deadline for one request
maxBodyBytes 2 MiB Largest request body
maxInflightBodyBytes a quarter of memory_limit Request bodies held by PHP at once, per worker

The complete list lives in the Configuration Reference.

PAM can create worker processes in three ways. You rarely choose directly; pam start picks the right one.

Mode When What happens
Exec macOS, a single worker, or PAM_PREFORK=0 The master starts each worker as a fresh pam process. Each worker has its own OPcache.
Prefork, engine (default on Linux) Linux with two or more workers A zygote process starts the Zend Engine and OPcache/JIT shared memory once, then forks workers before any application code runs. Workers share one OPcache segment, like PHP-FPM children.
Prefork, application PAM_PREFORK_BOOT=app (the default for Laravel Octane on PAM) The zygote also boots your application, and workers are forked from that warm state. A worker is ready in milliseconds and boot memory is shared copy-on-write.

In application mode, PAM makes process-bound state safe across the fork on its own: random seeds (mt_rand, rand, shuffle, lcg_value, uniqid) are re-seeded, timers are reset, inherited file offsets are reopened and sockets left open at fork time are disconnected. Use pam_before_fork() and pam_on_fork() for anything else, such as closing a database connection opened during boot. See Fork Hooks.

The same application runs unchanged in every mode:

Command Processes Use it for
pam server.php One standalone worker Quick experiments and scripts
pam dev One worker, restarted when PHP, Composer or environment files change Local development
pam start index.php --workers 8 Master + supervised workers Production on one host
pam up index.php A detached, managed pam start Long-running services; see Process Management

PHP and Rust talk through a small, versioned C ABI (native/pam.h, ABI version 1). PAM refuses to start if the compiled C shim and the Rust binary disagree on the ABI version, and you can read it from PHP:

Terminal window
pam inspect server.php
{
"phpVersion": "8.5.13",
"sapi": "embed",
"nativeAbiVersion": 1,
"zts": false,
"opcacheLoaded": true
}

(Output trimmed.) Every suspension that crosses the boundary carries a numeric operation kind, a deadline and a payload that Rust validates before acting on it, and Rust validates the status line and headers again before it writes a response to the network.