numprocs=4 or --concurrency=4: What a Symfony 8.2 Messenger Worker Costs in Throughput, Memory and Restarts
Prerequisites
PHP 8.4.22 with ext-redis, ext-pcntl and ext-posix, symfony/messenger 8.2.0, symfony/redis-messenger 8.2.0, symfony/framework-bundle 8.2.0, amphp/parallel 2.4.0, which --concurrency requires, Redis 7.4.9 as the transport, streams with a consumer group, Supervisor 4.3.0.
The benchmark app: three message classes, a recorder that writes every boot and every handled message to a Redis list, and a script that writes a Supervisor config per topology, runs one scenario and stores the metrics. The repository is linked at the end.
The numbers come from a 16-core Apple M3 Max, so they describe the shape of each trade-off, not your server. RSS is what ps reports per process on macOS. Every number is the median of three runs.
Throughput was first measured on 2000 messages, which was too short against the machine's background load (one topology ranged from 149 to 475 messages per second over eight runs), so the table below comes from 8000 messages.
1. The topologies and what each one is made of
A topology is P Supervisor processes with --concurrency=C, written PxC. Five of them cover the question:
- 4x1 - 4 PHP processes, 4 handler slots - the 8.1 way: four workers, no pool
- 1x4 - 1 parent + 4 children, 4 handler slots - the 8.2 pitch: one worker, four children
- 4x4 - 4 parents + 16 children, 16 handler slots - both
- 16x1 - 16 PHP processes, 16 handler slots - sixteen workers, no pool
- 1x16 - 1 parent + 16 children, 16 handler slots - one worker, sixteen children
4x1 and 1x4 have the same four slots and compare directly. 4x4 has sixteen, so 16x1 and 1x16 sit next to it as the controls. Without them, a 4x4 result only says that sixteen slots beat four.
The Supervisor program is the one from the Messenger docs, with the concurrency option added where the topology has one:
[program:worker]
command = php / app / bin / console messenger : consume async -- time - limit = 3600 -- concurrency = 4
numprocs = 1
process_name =% ( program_name ) s_ % ( process_num ) 02d
autostart = true
autorestart = true
startsecs = 0
startretries = 10
stopwaitsecs = 30
environment = APP_ENV = "prod" , MESSENGER_CONSUMER_NAME = "%(program_name)s_%(process_num)02d"
MESSENGER_CONSUMER_NAME feeds the consumer option of the Redis transport, so every Supervisor process reads the stream under its own name. The children never touch the transport, which is the point of the feature and what sections 2 and 7 come back to.
Three message classes make the workloads. FastMessage holds a slot for 20 or 50 milliseconds, SlowMessage for 10 to 20 seconds, and Ping does nothing but record how long it waited in the queue, which is what a user of a latency-sensitive message feels.
The handlers hold their slot with a loop of short sleeps until a deadline rather than one usleep(), because a single usleep() returns early when SIGTERM arrives and makes a stopped handler look like a finished one. That cost me a wrong first result on the stop scenario.
2. Throughput: the parent costs 15% at sixteen slots
Eight thousand FastMessage of 20 ms, workers warm, every class already probed. The ideal for four slots is 200 messages per second, for sixteen it is 800.
| topology | msg/s | min-max | PHP processes | RSS MB | transport connections | busiest parent CPU |
|---|---|---|---|---|---|---|
| 4x1 | 150 | 143-151 | 4 | 136 | 4 | 2% |
| 1x4 | 146 | 143-150 | 5 | 191 | 1 | 12% |
| 4x4 | 601 | 593-618 | 20 | 768 | 4 | 11% |
| 16x1 | 622 | 617-660 | 16 | 545 | 16 | 2% |
| 1x16 | 526 | 435-560 | 17 | 595 | 1 | 23% |
At four slots the shape does not matter: 150 against 146. At sixteen it starts to. One parent with sixteen children does 85% of what sixteen processes do, and four parents with four children each close the gap.
The parent does the fetching, the unserializing, the acknowledging and the channel round trip for every message, and the PR text says where that leads: "the fetch+unserialize steps are not parallelized when --concurrency is used. This is unfavorable for very short tasks".
The parent of 1x16 sits at a quarter of a core, so the limit is not CPU but the serial loop: sixteen children hand back results faster than one loop can fetch, dispatch and acknowledge in turn. Shorter handlers should widen the gap, as the PR text says; I did not measure below 20 ms. The PR's own table, 20 ms handlers on 24 cores, shows 7.5x for eight children.
Memory: a child is a booted kernel
The PR pitch says nothing about memory, but the assumption that one process with children must be lighter than N processes is common. It is wrong. Each amphp child requires bin/console, boots the kernel and builds the container, so it weighs what a worker weighs:
- Supervisor worker,
--concurrencynot set - 34 MB - amphp child - 34 MB
- parent of 4 children - 55 MB
- parent of 16 children - 51 MB
1x4 is five kernels against four, 191 MB against 136. 1x16 is seventeen against sixteen, 595 MB against 545. 4x4 is twenty, 768 MB. On this app a kernel is small; multiply by your own worker's RSS, the ratio stays.
Connections: the one thing the parent saves
16x1 holds sixteen connections to Redis, 1x16 holds one. For Redis that number does not matter. For a managed broker with a connection cap, or a Doctrine transport sharing a pool with the web tier, it is the argument for the parent, and it is the only one in this table.
3. Latency: same slots, same waits
Two SlowMessage of 15 s and two hundred FastMessage of 50 ms are queued, then a Ping every half second for 20 seconds. The pings arrive behind the backlog, so the first ones wait for it to drain, and the median tells how a ping fares once the slow handlers hold their slots.
| topology | ping wait p50 s | p95 | max | fast wait p50 s |
|---|---|---|---|---|
| 4x1 | 0.26 | 4.64 | 5.65 | 3.21 |
| 1x4 | 0.64 | 4.82 | 5.83 | 3.31 |
| 4x4 | 0.26 | 0.62 | 1.04 | 1.18 |
| 16x1 | 0.33 | 0.70 | 0.71 | 0.66 |
| 1x16 | 0.36 | 0.86 | 0.98 | 0.87 |
With every class probed and nothing to spawn, the topology does not matter: four slots give the same waits whether they live in four processes or one, and so do sixteen. The slow handlers occupy two slots either way. What moves the numbers is the slot count.
This is the steady state, and it is what the PR's benchmark table measures, on warm workers. The next three scenarios are the ones it skips.
4. A burst meets the probe, and the fetch size hands it to one parent
Article one of this series measured the probe: the first message of each class on a parent runs alone, and the messages of that class fetched behind it are held until the probe answers. Here the slow class is left unprobed during warm-up, as it is after every restart, and four SlowMessage of 10 s arrive at once, with pings behind them.
| topology | slow wait p50 s | ping wait max s | drain s |
|---|---|---|---|
| 4x1 | 0.58 | 10.09 | 16.0 |
| 1x4 | 10.33 | 9.83 | 21.0 |
| 4x4 | 10.35 | 0.90 | 20.9 |
| 16x1 | 0.24 | 0.89 | 15.8 |
| 1x16 | 10.30 | 0.94 | 20.9 |
Every topology with a parent takes five seconds longer, because three of the four slow messages wait 10 seconds for the probe of the first. Four processes take one each and finish together.
4x4 is the surprise. Four parents, four slow messages, and still three of them held. The cause is --fetch-size: it defaults to the value of --concurrency, so a parent with four children asks Redis for four messages per call, and the first parent to ask gets the whole burst. The other three parents sit idle with sixteen free slots between them while one parent holds three messages behind its probe. 16x1 fetches one message per process and spreads the burst by construction.
The pings tell the other half. On 4x1 and 1x4 they wait up to 10 seconds, because all four slots are taken, whether by four running handlers or by one probe plus three held messages, which count against the limit. On the sixteen-slot topologies the pings pass, because the burst does not fill the pool.
Size the burst to the slot count and it does:
| topology | slow wait p50 s | ping wait max s | drain s |
|---|---|---|---|
| 4x1 | 0.13 | 9.78 | 15.9 |
| 1x4 | 10.16 | 9.72 | 20.7 |
| 4x4 | 10.31 | 9.84 | 20.9 |
| 16x1 | 0.62 | 10.03 | 15.9 |
| 1x16 | 10.56 | 10.04 | 20.9 |
One slow message per slot pauses every topology for 10 seconds, pings included. The parent ones pay the probe on top and need 21 seconds to clear what the process ones clear in 16.
--fetch-size=1 on a topology with several parents softens this. On 4x4 the same burst then spread over three parents: three slow messages started at once and one was held, the drain stayed at 21 seconds because of that one, but the median slow wait fell from 10.3 seconds to 0.2. A parent with free children fetches again as soon as it has dispatched, so it can still take two messages before a sibling polls. The option costs a round trip per message, which section 2 showed the parent can afford less than the processes can.
5. Cold start: children boot one at a time
Four hundred FastMessage and twenty pings are queued before Supervisor starts. The table shows when the Nth message started handling, counted from the moment supervisord was launched.
| topology | boots | 1st message s | 10th | 50th | 100th | drain s |
|---|---|---|---|---|---|---|
| 4x1 | 4 | 2.02 | 2.09 | 2.41 | 2.76 | 4.83 |
| 1x4 | 1 + 4 | 2.37 | 3.71 | 4.03 | 4.36 | 7.76 |
| 4x4 | 4 + 16 | 3.13 | 4.20 | 4.54 | 4.62 | 5.37 |
| 16x1 | 16 | 1.93 | 2.02 | 2.08 | 2.15 | 2.66 |
| 1x16 | 1 + 16 | 2.68 | 7.13 | 10.54 | 10.78 | 11.87 |
Sixteen processes boot in parallel and are all handling within 0.3 seconds of each other. One parent with sixteen children handles its tenth message at 7 seconds and its fiftieth at 10.5. The recorder shows why: the children boot 0.45 to 0.5 seconds apart, in sequence.
- 0.00
messenger:consume async --concurrency=16(parent) - 0.73 child 1
- 1.39 child 2
- 1.91 child 3
- ... 7.71 child 15
- 8.30 child 16
The kernel boot itself takes 20 to 40 milliseconds. The half second is the context start: ParallelExecutionStrategy::getChannel() creates a child when a message arrives and no channel is free, and createChannel() starts the amphp context and submits the task before the parent's loop continues. Children are created on demand, one per message that needs a slot, and the parent fetches nothing while one starts.
So a pool of sixteen reaches full width eight seconds after every restart, and with --time-limit=3600 that is every hour, on top of one probe per class per parent. 4x4 looks fine at the 50th message because four parents spawn four chains in parallel, but its first message comes last of all, at 3.1 seconds: four parents fetching four messages each and all spawning at once.
6. Stop: the parent waits for its children
Every slot is inside a 20-second handler, with about 15 seconds left, when supervisorctl stop all runs. Supervisor sends SIGTERM and waits up to stopwaitsecs=30.
| topology | stop took s | handlers finished | left pending | processes left |
|---|---|---|---|---|
| 4x1 | 14.8 | 4 of 4 | 0 | 0 |
| 1x4 | 15.3 | 4 of 4 | 0 | 0 |
| 4x4 | 16.2 | 16 of 16 | 0 | 0 |
| 16x1 | 16.1 | 16 of 16 | 0 | 0 |
| 1x16 | 15.3 | 16 of 16 | 0 | 0 |
No difference. The parent receives the signal, stops fetching, waits for every child to return its result, acknowledges, shuts the pool down and exits. No child was orphaned, no message was left pending.
stopasgroup=true and killasgroup=true, which send the signal to the children as well, changed nothing: amphp's process-runner.php registers a no-op handler for SIGTERM, SIGINT, SIGHUP and SIGUSR1 in every child, so a child finishes its handler and waits for its parent whatever Supervisor sends it.
What matters is stopwaitsecs: it must cover your longest handler, on both topologies, or Supervisor escalates to SIGKILL and section 7 applies.
7. Kill: who dies decides whether the message runs twice
Same load, and three seconds in, one process gets SIGKILL. Three cases, because a topology with a parent has three kinds of process to lose.
A Supervisor worker without a pool, 4x1 and 16x1. Supervisor restarts it in about two seconds. The restarted worker has the same consumer name, and the Redis transport reads a consumer's own pending entries before new ones, so the message that died with the process is handled by its replacement 5.6 seconds after dispatch instead of 0.9. Every message handled once, nothing pending at the end.
A child, 1x4, 4x4 and 1x16. The parent logs A parallel worker failed: The context stopped responding during receive, the envelope fails with The parallel worker stopped before returning a result, WorkerMessageFailedEvent fires with willRetry() true, the retry strategy re-dispatches it with the default one-second delay, and the parent creates a new child. The message is handled once, 5.2 seconds after dispatch. The parent's other children keep running.
A parent, 1x4, 4x4 and 1x16. This is the case the pool adds.
| topology | dispatched | handled | boots after the kill | last slow wait s |
|---|---|---|---|---|
| 1x4 | 24 | 28 | 1 + 4 | 26.1 |
| 4x4 | 36 | 40 | 1 + 4 | 26.0 |
| 1x16 | 36 | 52 | 1 + 16 | 29.9 |
The children do not die with the parent. They keep their 20-second handlers running to the end, write their results, fail to send them to a parent that is gone, and exit. Supervisor restarts the parent, which reads its own pending entries, which are exactly the messages the orphans are finishing, and runs every one of them again on fresh children. One parent with sixteen children handled 52 messages for 36 dispatched: every in-flight message twice, 25 seconds apart.
Comments
No comments yet. Start the discussion.