Sizing PHP Workers

The PHP workers settings bound how many requests run, queue and wait at once. This page picks values for them from your CPUs, memory, handler time and connection budget. In short:

  1. 1
    Count the CPUs you pay for. The defaults are sized from the CPUs the process can see. On a platform that enforces its limit out of sight, set PHP_WORKERS and TOKIO_WORKERS yourself (How the CPU count is detected).
  2. 2
    Choose a fixed or a dynamic pool. PHP_WORKERS=N or PHP_WORKERS=MIN:MAX (Static vs dynamic workers).
  3. 3
    Check memory in worker mode. Compare PHP_WORKERS × WORKER_MAX_MEMORY_MIB with the memory you have, leaving room for OPcache and the server itself.
  4. 4
    Size the waiting set from your handler time. About W × B / T requests can usefully wait (Sizing the waiting set).
  5. 5
    Stay below MAX_CONNECTIONS. Keep PHP_WORKERS + QUEUE_CAPACITY + QUEUE_MAX_WAITING under it; the server warns at startup when it is not (Keeping headroom for the accept loop).

Static vs dynamic workers

Set PHP_WORKERS to a single number for a fixed pool:

bash
PHP_WORKERS=8 # Fixed 8 workers PHP_WORKERS=0 # Auto-detect: CPU / 2 (min 1)

Set PHP_WORKERS to MIN:MAX for automatic scaling:

bash
PHP_WORKERS=2:16 # Scale between 2 and 16 workers PHP_WORKERS=4:0 # 4 minimum, auto-detect maximum (CPU × 2) PHP_WORKERS=0:16 # auto-detect minimum (CPU / 4, min 1), 16 maximum

A dynamic pool adds workers when all are busy and retires one once it has been idle longer than PHP_WORKERS_IDLE_SECONDS. Its queue defaults are sized from the minimum.

A pool of one worker has nothing to cover for it. When an OPcache restart replaces that worker, which can happen twice per restart, requests wait until the replacement has started and are refused with 529 once QUEUE_WAIT_TIMEOUT_MS has passed.

How the CPU count is detected

Every default written in terms of CPU (TOKIO_WORKERS, and PHP_WORKERS unset, 0, or with 0 on either side of MIN:MAX) counts the CPUs the process may run on. That is its affinity mask (taskset, docker run --cpuset-cpus), lowered to the container's CPU quota where one is set (docker run --cpus; cpu.max on cgroup v2, cpu.cfs_quota_us on v1). A quota is rounded down and counts as at least 1, so --cpus=1.5 counts as 1.

Two kinds of limit are not counted, and a container held back only by one of them is sized from every CPU it can see:

  • A CPU weight (docker run --cpu-shares, cpu.weight on cgroup v2, cpu.shares on v1) only decides how contended CPU time is shared with neighbours. It is not a cap, so no CPU count follows from it.
  • A platform can sell a share of a machine and enforce the limit outside the container, where the container cannot see it. On DigitalOcean App Platform, for example, an instance sold as 1 vCPU was observed in a gVisor sandbox with no quota (cpu.cfs_quota_us is -1) and 8 CPUs visible, so the defaults came to 4 PHP workers, 4 runtime threads and a queue of 512.
Plans that enforce limits out of sight

On such a plan, set PHP_WORKERS and TOKIO_WORKERS explicitly, to what the plan provides. In worker mode, also check PHP_WORKERS × WORKER_MAX_MEMORY_MIB against the plan's memory. That threshold is a recycle trigger the worker checks in its loop, not a cap on its PHP heap, so the pool can hold that much and more before a worker is recycled, with OPcache and the server on top. With the default 0 there is no threshold at all. The same sandbox can hide the memory limit too: the App Platform container above saw none.

At startup the server logs the count, what set it, and the resulting PHP_WORKERS and TOKIO_WORKERS. Here it is under docker run --cpus=1 on a 14-CPU machine:

json
{"timestamp":"2026-10-05T18:55:39.801254Z","level":"INFO","fields":{"message":"CPU count for default sizing","cpus":1,"cpu_source":"cgroup quota","host_cpus":14,"php_workers":"1 (auto)","tokio_workers":"1 (auto)"}}
  • cpu_source is host when neither a mask nor a quota lowered the count, affinity or cgroup quota when one of them did, and unknown when the mask or the online count could not be read. If the server could not count CPUs at all, cpus is 4.
  • host_cpus is how many CPUs the system reports online (/sys/devices/system/cpu/online on Linux), which a mask or quota does not lower. It is omitted when it cannot be read. A runtime that rewrites that file to list only the container's CPUs makes a narrowed mask read as host.
  • (auto) marks a value left entirely to the default: the variable is unset or empty, or, for TOKIO_WORKERS, not a number.

A cpu_source of host on a plan that sold you fewer CPUs than cpus is the case above.

Sizing the async runtime

TOKIO_WORKERS defaults to half the available cores, and the other half is not idle: it belongs to the PHP worker pool. The two sets of threads share the same cores, so widening the runtime takes cycles from the application.

Raising it to the full core count loses wherever the pool is already sized near that count. Measured on a 4-core machine with a 4-worker pool serving compressed responses, going from 2 runtime threads to 4 cost 13% of the rate on a 1.6 KB JSON body and 26% on a 5 KB one, and burned more CPU, because the threads were oversubscribed.

Raise it where the balance differs: a pool small relative to the machine, or work that is I/O-bound rather than PHP-bound (proxied upstreams, static files, streaming responses), where the runtime does the work and the pool mostly waits. After any change, watch the pool as well as the rate: oxphp_busy_workers against oxphp_workers_current shows which side the cores went to. A rate that rises while the pool gets busier is a real gain. A rate that rises while the pool empties usually means the work moved, not that more of it got done.

Tip

TOKIO_WORKERS and PHP_WORKERS both default to half the available cores, so their sum is the core count. Change one and the sum moves, which is why either one is worth measuring rather than guessing.

Sizing the waiting set

How many requests can usefully wait depends on how fast the pool finishes them. With W workers and a handler that takes T milliseconds, the pool takes on W / T requests per millisecond, so a budget of B milliseconds can admit about W × B / T waiters. The rest wait the whole budget and are refused anyway, holding a connection and a buffered request body all the while.

The default cannot compute this, because the service rate is unknown at startup, so it is generous on purpose. That suits fast handlers, where the pool drains a deep backlog well inside the budget, and is far too large for slow ones. With 8 workers, a 200 ms handler and the default 1 s ceiling (the budget in force whenever the pool is not shedding), only about 40 requests can be admitted in time, while the default parks up to 1024:

bash
QUEUE_MAX_WAITING=40

Set near W × B / T, the surplus gets an immediate 529 instead of one a second later. Shortening QUEUE_WAIT_TIMEOUT_MS does the same through the other term, and on a slow handler it is usually the better lever, because it also bounds how long a client waits for the refusal. B is the budget in force, which under overload is the one the server has shortened to, not the configured ceiling. Read oxphp_admission_wait_budget_us before sizing from a number in a config file (The wait budget).

The waiting set is bounded in bytes too. Bodies are buffered in full before a request reaches the queue, and every waiter holds its body in memory, so with only a count to stop them the parked requests can hold QUEUE_MAX_WAITING × 10 MiB, the server's body limit per request. The same 1024 waiters cost nothing on bodyless GETs and gigabytes on uploads. QUEUE_MAX_WAITING_BYTES bounds that sum: past it, a request carrying a body is refused at once, while bodyless ones keep waiting. Raise it for an upload-heavy application that should absorb bursts rather than shed them, and lower it on a container with a hard memory limit.

oxphp_admission_refused_total counts the two refusals apart, waiting_full for the count and waiting_bytes for the bytes, so the label names the setting to change.

Keeping headroom for the accept loop

A request holds its connection, and with it one of the MAX_CONNECTIONS permits, from arrival until it is answered. The PHP path holds requests in three places, each with its own bound (a queue slot is released the moment a worker picks the request up, before the script runs):

  • running in a worker: at least PHP_WORKERS, and more in worker mode, where one thread multiplexes fibers;
  • queued, up to QUEUE_CAPACITY;
  • parked in admission, up to QUEUE_MAX_WAITING.

Only the third has a ceiling derived from MAX_CONNECTIONS, so keep PHP_WORKERS + QUEUE_CAPACITY + QUEUE_MAX_WAITING below MAX_CONNECTIONS yourself. Past it, the failure changes for the worse. The accept loop takes a permit before it serves a connection, so once the PHP path holds them all it parks, and an arriving client gets no response at all instead of a 529. A load balancer cannot tell that silence from a dead instance, and the health probe on INTERNAL_ADDR stays green because it goes through neither limit.

The sum counts requests, not connections, so treat it as a guide. A request answered on its queue deadline (529, or 499 once its client has gone) gives its connection back at once but keeps its queue slot until a worker reaches it. QUEUE_CAPACITY can therefore overstate the connections the queue holds, as PHP_WORKERS understates the ones running in worker mode.

A stall is logged and counted. An accept that has to wait for a permit logs a WARN naming the budget, and an INFO with how long the loop was parked once the permit arrives. So that a server flapping around the ceiling cannot flood its own log, a stall that starts within seconds of a reported one is held back, and reported when that window closes if it is still going; one that ends inside the window leaves no line. The metrics see every stall: oxphp_accept_stalled (a gauge, readable during the stall because the internal listener does not go through MAX_CONNECTIONS) and oxphp_accept_stalls_total (a counter); see Connection metrics.

The server checks the sum at startup and warns, naming every value, when it reaches MAX_CONNECTIONS. oxphp config --check prints the same advisory after its verdict, without changing the verdict or the exit code:

oxphp config --check
config: OK ! PHP_WORKERS (7) + QUEUE_CAPACITY (896) + QUEUE_MAX_WAITING (500) = 1403 >= MAX_CONNECTIONS (1000): …

Clearing the warning is necessary but not sufficient: idle keep-alive connections, static-file requests and handshakes in progress hold permits too, and none of them can be counted at startup.

Lowering MAX_CONNECTIONS is the usual way into the warning, because the QUEUE_CAPACITY default follows the worker count and not MAX_CONNECTIONS. That is the case above: 7 workers and MAX_CONNECTIONS=1000. Raising MAX_CONNECTIONS alone does not clear it while QUEUE_MAX_WAITING keeps its default, which is half of MAX_CONNECTIONS and rises with it: at 7 workers the sum settles at 1799, and the warning clears only from MAX_CONNECTIONS=1800 up. Lower QUEUE_CAPACITY, or set QUEUE_MAX_WAITING explicitly and then raise MAX_CONNECTIONS.

For a dynamic pool (MIN:MAX) the running term is the minimum, the count the queue defaults are sized from, so a pool grown to its maximum holds more than the sum says.

An HTTP/2-heavy deployment can sit above the line legitimately. One connection carries up to H2_MAX_CONCURRENT_STREAMS requests, so the same backlog holds a fraction of the connections, and the warning is expected there. A large pool on the stock budget reaches it too (an auto-sized pool of 39 workers gives 39 + 4992 + 4992 against 10 000), and that one is worth fixing.

Prompts for your agent

These prompts assume an agent with the OxPHP documentation MCP server connected. The agent reads the pages it needs itself.

prompt
Size the OxPHP workers for my deployment. Read the `operations/sizing-workers` and `operations/configuration` pages through the OxPHP documentation MCP server. Find where this repository deploys OxPHP (Dockerfile, compose file, Kubernetes manifests, platform spec) and how much CPU and memory it gets there, and ask me for the typical handler time if the repository does not show it. Then propose `PHP_WORKERS`, `TOKIO_WORKERS`, `WORKER_MAX_MEMORY_MIB`, `QUEUE_MAX_WAITING` and `MAX_CONNECTIONS` with the arithmetic behind each value, and check that `PHP_WORKERS` + `QUEUE_CAPACITY` + `QUEUE_MAX_WAITING` stays below `MAX_CONNECTIONS`.
prompt
Clear the OxPHP connection-budget warning. Read the `operations/sizing-workers` page through the OxPHP documentation MCP server. Work out the `PHP_WORKERS`, `QUEUE_CAPACITY`, `QUEUE_MAX_WAITING` and `MAX_CONNECTIONS` values this repository's environment resolves to, defaults included, and change the fewest settings that bring the sum below `MAX_CONNECTIONS`. Tell me if this deployment is one where the warning is expected and can be left alone.

See also

Found a mistake? Report it