Skip to content

Autoscaling

A Laravel application on PAM has two independent things to scale:

What Scaled with Unit
HTTP concurrency pam start pam.php --workers N, replicas, or servers Workers (one Laravel request each)
Background work pam.processes.json, pam:process scale, pam:autoscale Process instances (queue workers, Horizon, custom daemons)

Do not add HTTP workers to drain a queue, and never process a queue inside a request.

On one server, change --workers and reload:

Terminal window
pam start pam.php --workers 12 --admin-address 127.0.0.1:3010

Size it with Performance Tuning. Across servers, put several PAM instances behind a load balancer that checks each instance’s /ready, and add instances when CPU or p95 latency stays high. On PAM Cloud:

Terminal window
pam artisan pam:remote scale production --process=http --instances=8
Terminal window
pam artisan pam:process scale queue 4
pam artisan pam:process status

The override is saved in storage/pam/processes/scale.json and survives restarts of the manifest. Instances are limited to 1..128.

pam:autoscale compares CPU and p95 latency with your targets and scales a manifest process:

Terminal window
pam artisan pam:autoscale queue --cpu=80 --p95=400
queue desired instances: 2

The rules:

  • Scale up by max(1, ceil(current × 25%)) when CPU is above PAM_AUTOSCALE_TARGET_CPU or p95 is above PAM_AUTOSCALE_TARGET_P95_MS.
  • Scale down by one instance when CPU and p95 are both below half of their targets.
  • Stay within PAM_AUTOSCALE_MIN..PAM_AUTOSCALE_MAX (and never above 128).
  • Wait PAM_AUTOSCALE_COOLDOWN seconds between changes to the same process; the last change is recorded in storage/pam/processes/autoscale.json.
PAM_AUTOSCALE_MIN=1
PAM_AUTOSCALE_MAX=16
PAM_AUTOSCALE_TARGET_CPU=65
PAM_AUTOSCALE_TARGET_P95_MS=250
PAM_AUTOSCALE_COOLDOWN=60

Scaling restarts the process with the new instance count, so queue workers must handle SIGTERM gracefully (Laravel’s workers finish the current job).

Point the command at an endpoint that returns the current numbers:

PAM_AUTOSCALE_METRICS_URL=https://metrics.example.com/pam/queue-capacity
PAM_AUTOSCALE_METRICS_TOKEN=your-token
Expected response
{ "cpuPercent": 72.5, "p95Milliseconds": 310 }
Terminal window
pam artisan pam:autoscale queue --watch --interval=30
  • The URL must be HTTPS, or HTTP on 127.0.0.1, localhost, or ::1.
  • The token is sent as Authorization: Bearer ….
  • --interval is clamped to 5..300 seconds (default 15).
  • --watch without a metrics URL is rejected; any fetch error stops the command with exit code 1, so run it under a supervisor that restarts it.
  • Cap database and Redis connections before you raise instance counts.
  • Alert on queue age, not only depth.
  • Make jobs idempotent and bound their retries.
  • Respect third-party API rate limits; more workers can mean more 429s.
  • Keep the scheduler at exactly one instance.