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:
- the number of workers;
- Laravel’s own caches and OPcache;
- worker recycling;
- fork-after-boot (the post-boot zygote); and
- the edge response cache for public pages.
Measure before and after every change (see Measuring).
Choosing the Worker Count
Section titled “Choosing the Worker Count”Each worker serves one Laravel request at a time, so workers are your concurrency. Start with one per CPU core:
pam start pam.php --workers "$(nproc)" --admin-address 127.0.0.1:3010Then 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:
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.
Laravel Caches and OPcache
Section titled “Laravel Caches and OPcache”Build every framework cache in production:
pam artisan optimizepam 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:
pam composer install --no-dev --classmap-authoritativeWorker Recycling
Section titled “Worker Recycling”--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:
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).
Fork After Boot
Section titled “Fork After Boot”On Linux, set PAM_PREFORK_BOOT=app to boot Laravel once in a zygote process
and fork every worker from the booted application:
PAM_PREFORK_BOOT=app pam start pam.php --workers 8 --admin-address 127.0.0.1:3010pam: post-boot zygote ready in 161 ms; forking workers from the booted applicationWorkers 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.
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.
Edge Response Cache
Section titled “Edge Response Cache”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:
$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:
Route::get('/catalog', CatalogController::class) ->middleware('cache.headers:public;s_maxage=60');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
GETresponses with status200, withoutSet-Cookie,private, orno-store, are stored. - Requests that carry
CookieorAuthorizationbypass the cache. - The query string is part of the key;
responseCacheVaryHeadersadds headers. - Concurrent misses for the same key collapse into one Laravel execution.
- The cache is per worker, bounded by
responseCacheMaxEntries(1024) andresponseCacheMaxBytes(64 MiB).
Tag-based purging, PurgesPageCache models, and the pam.cache middleware
come with the Octane bridge; see Laravel Octane on PAM.
Blocking I/O
Section titled “Blocking I/O”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.
Measuring
Section titled “Measuring”Use the admin listener and a load generator against the same route you care about:
pam benchmark http://127.0.0.1:3000/api/ping --requests 10000 --concurrency 64curl -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.