Workers & Supervision
A long-lived process needs a supervisor. When you run pam start, PAM starts a master process that keeps a fixed number of workers alive: it waits for them to boot, restarts them when they crash, replaces them when they reach their request limit, reloads them without dropping traffic, and drains them on shutdown. The master never runs your PHP code, so it stays responsive even when a worker is stuck.
This page explains what the master does and how your application can cooperate with it. For the operational flags in day-to-day use, see Clustering; for running clusters in the background, see Process Management.
Starting a Cluster
Section titled “Starting a Cluster”pam start index.php --workers 4 --admin-address 127.0.0.1:3010Pam control plane listening on http://127.0.0.1:3010● READY Pam listening on http://127.0.0.1:3000 worker=1● READY Pam listening on http://127.0.0.1:3000 worker=2● READY Pam listening on http://127.0.0.1:3000 worker=3● READY Pam listening on http://127.0.0.1:3000 worker=4Pam master 1019501 started 4 worker(s); send SIGHUP for zero-downtime reloadWhen you omit the script, pam start uses index.php. Arguments after -- are passed to your script.
Supervision Options
Section titled “Supervision Options”| Option | Default | Meaning |
|---|---|---|
--workers N |
Number of CPUs | HTTP worker processes (at most 256) |
--max-requests N |
10000000 |
Recycle a worker after N PHP requests (staggered, see below) |
--task-workers N|auto |
0 |
Extra workers for background tasks and ticks (at most 64; auto = number of CPUs) |
--graceful-timeout MS |
15000 |
How long a draining worker may finish in-flight requests |
--startup-timeout MS |
30000 |
How long a new worker may take to become ready |
--restart-backoff MS |
100 |
First delay before restarting a crashed worker |
--watchdog-grace MS |
250 |
Extra time before the master kills a worker stuck past its request deadline |
--admin-address IP:PORT |
off | Bind the control plane (/live, /startup, /ready, /metrics, /diagnostics) |
--php-extension NAME |
all bundled | Load only the listed extensions (repeatable); see Composer Integration |
Every worker receives its identity in the environment: PAM_WORKER_ID (1 to N), PAM_WORKER_GENERATION and, for task workers, PAM_TASK_WORKER=1.
Worker Readiness
Section titled “Worker Readiness”A new worker counts as ready once it has booted your entrypoint and is listening. The master waits for every worker of a generation to be ready before it reports the cluster as ready, and fails the start if a worker does not become ready within --startup-timeout.
The control plane exposes this state for load balancers and orchestrators:
| Path | Meaning |
|---|---|
/live |
The master is running |
/startup |
The master is running and the first generation finished starting |
/ready |
The desired number of workers are ready or busy, and none is past its deadline |
/metrics |
Prometheus metrics aggregated across workers, plus PID, generation and lifecycle |
/diagnostics |
Per-worker lifecycle and event-loop lag as JSON |
curl -s http://127.0.0.1:3010/diagnostics{"schemaVersion":1,"surfaceCode":1,"resultCode":1,"generation":1,"desiredWorkers":2,"readyWorkers":2,"workers":[{"workerId":2,"generation":1,"pid":1026730,"pool":"default","lifecycleCode":2,"resultCode":1,"currentLagMicros":0,"maxLagMicros":1083,"averageLagMicros":541,"lagSampleCount":2}]}Crash Restarts
Section titled “Crash Restarts”When a worker exits unexpectedly, the master logs it and starts a replacement in the same slot:
pam: worker 2 generation 1 exited with signal: 9 (SIGKILL); restartingRestarts back off exponentially: the first restart waits --restart-backoff, and each consecutive failure doubles the delay, up to 30 seconds. A worker that ran for at least a minute before crashing starts the count again, and a worker that exits cleanly (because it was recycled on purpose) is replaced immediately. While a slot waits for its restart, its listener is closed so the kernel sends new connections to the healthy siblings.
Recycling
Section titled “Recycling”A worker asks to be recycled when it reaches --max-requests, when a PHP fatal error or exit() leaves its engine unusable, or when a blocking request wedged its PHP thread. Recycling is make-before-break:
- the master starts a replacement worker in the same slot;
- the old worker keeps serving until the replacement is ready;
- the old worker receives
SIGTERM, drains, and exits.
If the replacement fails to start, the old worker keeps serving and the master tries again later.
The request limit is staggered so workers do not recycle together: with --workers 4 --max-requests 1000, the limits are spread between 1,000 and 750. Requests answered entirely by Rust, such as edge-cache hits and /metrics, never count toward the limit.
pam start index.php --workers 8 --max-requests 500000Request Deadline Watchdog
Section titled “Request Deadline Watchdog”Each worker enforces its own request deadline (requestTimeoutMs, see Request Lifecycle): it answers 504 and asks to be recycled when PHP blocks past the deadline. The master is the second line of defense. If a worker stays busy past its deadline plus a one-second allowance and --watchdog-grace, the master kills it with SIGKILL and starts a replacement:
pam: worker 3 generation 1 exceeded the hard request deadline (3-48211-17); killing itZero-Downtime Reloads
Section titled “Zero-Downtime Reloads”Send SIGHUP to the master to deploy new code without dropping connections:
kill -HUP <master-pid>The master:
- starts a complete new generation of workers, which boot the current code;
- waits until every new worker is ready;
- activates the new generation and drains the old one.
Pam master activated worker generation 2If any new worker fails to boot (a syntax error, a missing environment variable), the new generation is discarded and the old one keeps serving:
pam: generation 2 failed readiness; keeping generation 1: ...Graceful Shutdown
Section titled “Graceful Shutdown”SIGTERM or Ctrl+C stops the cluster gracefully. Each worker stops accepting connections, finishes its in-flight requests within --graceful-timeout, and exits; workers still running after that deadline are terminated. During the drain, pam_draining() returns true inside the worker, so readiness endpoints can answer 503:
if (pam_draining()) { return $response->json(['ready' => false], 503);}A request that is already running when SIGTERM arrives completes normally:
$ curl -s http://127.0.0.1:3000/work # sent just before SIGTERM{"finished":true,"drainingAtEnd":true}Prefork and the Zygote
Section titled “Prefork and the Zygote”On Linux, with two or more workers, the master does not start each worker from scratch. A zygote process initializes the Zend Engine and its OPcache/JIT shared memory once, then forks workers on request. All workers of a generation share one compiled-code segment, which saves memory and keeps the CPU cache warm. Set PAM_PREFORK=0 to disable it.
By default the fork happens before your application boots, so every worker still runs your entrypoint itself. With PAM_PREFORK_BOOT=app (the default for Laravel Octane on PAM), the zygote boots your application first and workers are forked from that warm state:
PAM_PREFORK_BOOT=app pam start index.php --workers 8pam: post-boot zygote ready in 5 ms; forking workers from the booted applicationFork Hooks
Section titled “Fork Hooks”When workers are forked from a booted application, anything that the boot phase opened is inherited by every worker. PAM already re-seeds random number generators, resets timers, reopens files with independent offsets and disconnects sockets left open at fork time. For everything else, register fork hooks:
<?php
declare(strict_types=1);
pam_before_fork(static function (): void { // Runs once in the zygote, before the first fork. // Close connections so each worker opens its own. Database::disconnect();});
pam_on_fork(static function (): void { // Runs in every worker right after its fork. // PAM_WORKER_ID is already set here. Metrics::setWorker((int) getenv('PAM_WORKER_ID'));});pam_before_fork() and pam_on_fork() are shortcuts for Pam\Fork::beforeFork() and Pam\Fork::afterFork(). Pam\Fork::isForkedWorker() tells you whether the current worker was forked from a booted application. The hooks are harmless when the application is not forked; they simply never run.
Task Workers
Section titled “Task Workers”--task-workers N starts extra workers that do not serve public traffic. They listen on a private loopback address protected by a random per-cluster token, and they run two kinds of work:
- Tasks sent from HTTP workers with
Pam\Shared\Tasks; - Ticks: one per second from the master, skipped while the previous tick is still running.
pam start index.php --workers 8 --task-workers 2use Pam\Shared\Tasks;
if (Tasks::available()) { // Run two payloads on task workers concurrently; wait up to 3 seconds. // A null result means that task was still running when the wait ended. [$first, $second] = Tasks::resolve([$payloadA, $payloadB], 3000);
// Fire and forget. Tasks::dispatch([$payloadC]);}A task worker receives each task as an HTTP request to your application with an x-pam-task header (run, dispatch or tick) and an x-pam-task-token header that must match PAM_TASK_TOKEN. Nimbus answers ticks for you and uses them to run its scheduler and queue workers without a system cron. Laravel Octane’s concurrently() and task dispatch use task workers too. See Background Tasks for the full API.
Shared Memory Across Workers
Section titled “Shared Memory Across Workers”Workers are separate processes, so ordinary PHP variables are never shared between them. When workers must see the same data, such as counters, rate-limit buckets or a small cache, use Pam\Shared\Table. The master owns the memory, so rows survive worker crashes, recycling and reloads. See Shared Memory & Caching.
Scaling Beyond One Host
Section titled “Scaling Beyond One Host”The master supervises the workers of one host. PAM does not ship distributed locks, cross-host queues or cluster-wide coordination. Run one PAM cluster per host behind a load balancer, and use infrastructure you already trust (Redis, a database, a message broker) for state that crosses hosts. WebSocket broadcasts across hosts use the Redis Streams or NATS adapters described in WebSockets.
Next Steps
Section titled “Next Steps”- Clustering: sizing and operating
pam start - Request Lifecycle: draining and deadlines from the worker’s point of view
- Observability & Diagnostics:
pam topand the metrics endpoint