Skip to content

Performance Tuning

Most of PAM’s speed comes for free: Laravel boots once per worker instead of once per request. This page covers the knobs that matter after that, in the order you should reach for them:

  1. the number of workers;
  2. Laravel’s own caches and OPcache;
  3. worker recycling;
  4. fork-after-boot (the post-boot zygote); and
  5. the edge response cache for public pages.

Measure before and after every change (see Measuring).

Each worker serves one Laravel request at a time, so workers are your concurrency. Start with one per CPU core:

Terminal window
pam start pam.php --workers "$(nproc)" --admin-address 127.0.0.1:3010

Then adjust:

  • More workers when requests spend time waiting on the database, Redis, or HTTP APIs (CPU is idle while p95 latency climbs).
  • Fewer workers when CPU is saturated, or when you hit the database’s connection limit. Every worker can hold its own database and Redis connections.
  • Memory caps the maximum. Estimate from a measured per-worker RSS:
Terminal window
pam artisan pam:capacity --memory-mb=2048 --worker-mb=110
+----------+---------+------------+---------------------+
| Memory | Reserve | Per worker | Recommended workers |
+----------+---------+------------+---------------------+
| 2048 MiB | 20% | 110 MiB | 14 |
+----------+---------+------------+---------------------+

The formula is floor(memory × (100 − reserve) / 100 / per-worker), with a default reserve of 20% (--reserve-percent). Read the real per-worker RSS from pam_process_resident_memory_bytes on the admin /metrics endpoint after warm-up.

Build every framework cache in production:

Terminal window
pam artisan optimize

pam start enables OPcache (with JIT) for the workers. With more than one worker on Linux, the workers fork from a shared zygote, so they share one OPcache segment, like PHP-FPM children. A SIGHUP reload starts a new zygote, so new code is picked up even with opcache.validate_timestamps=0.

Install production dependencies with an authoritative class map:

Terminal window
pam composer install --no-dev --classmap-authoritative

--max-requests N replaces a worker after it has served about N requests. It is a safety net against slow memory growth in third-party code:

Terminal window
pam start pam.php --workers 8 --max-requests 100000
  • The default is 10,000,000.
  • Each worker gets a limit between 75% and 100% of N, so workers never recycle at the same moment.
  • Recycling is make-before-break: the replacement boots first, the old worker then drains. No request is dropped.

Choose N from a soak test: if RSS is flat after a million requests, keep the default; if it grows, lower N and investigate (see Testing).

On Linux, set PAM_PREFORK_BOOT=app to boot Laravel once in a zygote process and fork every worker from the booted application:

Terminal window
PAM_PREFORK_BOOT=app pam start pam.php --workers 8 --admin-address 127.0.0.1:3010
pam: post-boot zygote ready in 161 ms; forking workers from the booted application

Workers are then ready within milliseconds and share the booted memory copy-on-write. PAM handles what cannot cross a fork: open sockets (database, Redis) are disconnected in the zygote so each worker reconnects, file offsets and random seeds become per-worker, and PAM_WORKER_ID is set before your afterFork callbacks run.

app/Providers/AppServiceProvider.php
public function boot(): void
{
if (class_exists(\Pam\Fork::class)) {
\Pam\Fork::afterFork(fn () => logger()->info('Worker forked', ['worker' => getenv('PAM_WORKER_ID')]));
}
}

PAM_PREFORK_BOOT=engine (the default for pam.php) forks before PHP code runs: workers share OPcache but each boots Laravel itself. PAM_PREFORK=0 disables forking entirely. PAM falls back to engine forking automatically if the boot fails or the process is multi-threaded after boot.

For public, anonymous pages (a product catalog, a pricing page, a public API), PAM can serve the response from Rust without entering PHP at all. It is off until you list the paths:

pam.php
$app->listen(
port: (int) (getenv('PAM_PORT') ?: 3000),
host: (string) (getenv('PAM_HOST') ?: '127.0.0.1'),
options: [
'maxConcurrentRequests' => 1,
'responseCachePaths' => ['/api/catalog'],
'responseCacheTtlMs' => 30_000,
'responseCacheMaxBytes' => 64 * 1024 * 1024,
'responseStreamQueueCapacity' => 16,
'maxResponseBytes' => 256 * 1024 * 1024,
'maxResponseChunkBytes' => 1024 * 1024,
],
);

Laravel marks responses Cache-Control: no-cache, private by default, which PAM never stores. Mark the route public:

routes/api.php
Route::get('/catalog', CatalogController::class)
->middleware('cache.headers:public;s_maxage=60');
Terminal window
curl -sD - -o /dev/null http://127.0.0.1:3000/api/catalog | grep -i x-pam-cache
# x-pam-cache: MISS
# x-pam-cache: HIT (second request)

Rules worth knowing:

  • Only GET responses with status 200, without Set-Cookie, private, or no-store, are stored.
  • Requests that carry Cookie or Authorization bypass the cache.
  • The query string is part of the key; responseCacheVaryHeaders adds headers.
  • Concurrent misses for the same key collapse into one Laravel execution.
  • The cache is per worker, bounded by responseCacheMaxEntries (1024) and responseCacheMaxBytes (64 MiB).

Tag-based purging, PurgesPageCache models, and the pam.cache middleware come with the Octane bridge; see Laravel Octane on PAM.

Laravel’s database, Redis, and HTTP clients block the worker while they wait. That is fine: it is how PHP-FPM works too, and the answer is the same: enough workers to cover the wait. Watch the admin metrics for event-loop lag and queued requests, and move slow work (emails, exports, webhooks) to the queue.

Use the admin listener and a load generator against the same route you care about:

Terminal window
pam benchmark http://127.0.0.1:3000/api/ping --requests 10000 --concurrency 64
curl -s http://127.0.0.1:3010/metrics | grep -E 'pam_http_request_duration|resident_memory'

Compare p95/p99 latency and errors, not just requests per second. See Benchmarks for measured numbers and a reproducible protocol.