Symfony 8.2 Messenger Adds Batch Dispatch, Safer Routing and Better Worker Controls
- Javier Eguiluz
- 707 reads
Symfony 8.2 improves Messenger throughput, handler routing and queue operations, with new tools for cooldowns, message inspection and non-blocking SQL.

Symfony 8.2 gives Messenger teams more control over the path from dispatch to recovery. The release adds batch sending, clearer rules for which buses and handlers accept messages, and worker controls that help limit repeated failures and target restarts. It also improves queue inspection and adds an SQL transport built for event-loop applications.

These changes matter most in systems that rely on Messenger for imports, notifications, background processing or multiple message buses. Some features reduce configuration and operational effort; others affect delivery behavior, so teams should check how they fit existing workers and deployment order. The release also includes an Messenger component, and its wider set of updates includes outbox and claim check options, an inbox, flows, cross-application messaging, signed transports, faster workers and a MongoDB transport.

Batch dispatch cuts transport calls
Sending messages in a loop can mean one transport request for every message. That adds overhead during bulk work such as importing a file or notifying many users. Symfony 8.2 introduces BatchDispatcher to collect messages and dispatch them as a batch. It can wrap a list of messages directly or run code that expects a bus, such as an importer.

Each message still passes through the bus middleware. The dispatcher holds messages until they have all passed through that stage; if one fails before the batch is sent, none are sent. This changes the timing of transport delivery, so it’s worth checking whether application code depends on messages being sent immediately during dispatch.

Amazon SQS, AMQP, Doctrine, Redis, MongoDB and AMPHP SQL transports, along with the transactional outbox, can send batches using fewer requests. Other transports send messages individually. In local benchmarks reported for this feature, sending 2,500 messages to Redis took 67 ms, compared with 1,038 ms without batching. That result is specific to the stated local benchmark; teams should measure their own transport and workload.

Bus rules and middleware get clearer
The new #[AsMessageMiddleware] attribute lets middleware declare which bus it belongs to and where it sits in the middleware sequence. The bus name is required, with '*' available for all buses. Placement can be described with before and after values that refer to configured middleware names, service IDs or class names. Bundles can use this to register middleware without requiring each application to add it manually to bus configuration.

Symfony 8.2 also adds two bus options for applications that separate commands, queries and events across multiple buses. The messages setting limits what an application can dispatch on a bus to listed classes or interfaces. Dispatching another type raises an exception, which catches mistakes such as sending a query through a command bus. The unwrap_exceptions option makes a failing handler’s original exception bubble up instead of its surrounding HandlerFailedException.

Both settings apply to messages dispatched by the application, not messages that workers receive. Symfony can dispatch its own messages on the default bus, so a restricted configuration needs to account for that when choosing which bus is the default.

Handler routing and message renames
When several handlers process the same message on different transports, the previous setup required matching transport routing with each handler’s fromTransport setting. A mismatch could leave a handler idle without an error. The new transport argument on #[AsMessageHandler] puts that choice on the handler itself. The message can be routed to both transports, with each handler processing the copy from its assigned transport. An unknown transport name now causes a container build error.

Message class names can also change while queued messages are still waiting to be decoded. The serializedTypeNameAliases option on #[AsMessage] lets a message accept older serialized names as aliases. New messages continue to use the primary serialized name; aliases only affect decoding. For a rolling deployment, update consumers before producers so consumers can read messages using either name before producers begin sending under the new one.

Handler events and transport cooldowns
Existing worker events report once per message and cover messages consumed by workers. Symfony 8.2 adds HandlerStartingEvent, HandlerSuccessEvent and HandlerFailureEvent, emitted around each handler call, including synchronous handling. The events provide the message envelope and handler descriptor. That gives monitoring and logging code a way to identify which handler ran or failed, regardless of whether a worker fetched the message.
Repeated handler failures can also trigger a transport cooldown. When a dependency such as a database or rate-limited API is unavailable, a worker may otherwise keep fetching messages that are likely to fail. A transport’s cooldown_strategy can pause consumption after a configured run of failures while the worker continues using other transports.
For example, a threshold of five failures starts a one-second pause; subsequent failures double the delay, up to a configured maximum of 60 seconds. A successfully handled message resets the count. Fetch errors, including problems while a broker restarts, count as failures too. Each worker tracks its own count, so cooldown behavior is per worker rather than shared across all processes.
More control over consuming and restarting workers
When messenger:consume --all reads from every transport, Symfony 8.2 lets teams set transport priorities to control the order: higher values are consumed first, and the default is zero. This gives urgent queues an explicit place in the worker’s consumption order.
The new --stop-when-empty option tells a worker to process messages already available and then exit, instead of waiting for more. Delayed messages remain in their transports. This suits finite runs such as deployment tasks, scheduled jobs and CI work.
Worker restarts can now target specific transports. The messenger:stop-workers command accepts transport names, allowing a process manager to restart only workers that consume those queues. The WorkerRestarter service provides the same control from application code. For example, after a scheduler’s database-backed configuration changes, code can stop the relevant workers so replacements load the updated schedule.
Queue visibility and recovery tools
The new messenger:show command lists pending messages by ID and class, displays a selected message’s details, or reports counts by message class. More detailed contents are available with -vv. It works with transports that support listing messages, including Doctrine, so visibility depends on transport capabilities.
Failed-message commands gain date filters, letting operators retry or remove messages within a time window rather than supplying IDs one by one. The retry command also gains a class filter. These options make it easier to isolate a group of failures after a temporary dependency problem, while limiting the retry to the relevant message type and period.
SQS fairness and non-blocking SQL
For multi-tenant workloads on standard Amazon SQS queues, AmazonSqsFairQueueStamp sets a group ID on a message. SQS can then balance delivery across groups, reducing the chance that one tenant’s high volume delays everyone else’s messages. This works without converting the queue to FIFO.
The new AMPHP SQL transport addresses a different concern: Doctrine transport queries block, which can stall an event loop. The AMPHP transport stores messages in SQLite, MySQL or PostgreSQL using non-blocking drivers. It requires the Symfony transport package plus the database driver, and uses its own table schema, so it cannot share a table with the Doctrine transport.
Symfony 8.2’s Messenger additions span throughput, correctness and operations. The practical next step is to identify which features match current bottlenecks, then verify transport support and deployment behavior—especially for batch timing, message-name aliases and worker cooldowns.