Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Symfony 8.2 Messenger Concurrency: What Breaks in Multi-Tenant Applications

With a database per tenant, Symfony 8.2 --concurrency lost the tenant in handlers, held tenants 21 s behind one probe and flushed a batch into the wrong DB.

Symfony 8.2 Messenger Concurrency: What Breaks in Multi-Tenant Applications

Symfony 8.2 introduced a new concurrency feature for the Messenger component, which can greatly improve performance in multi-tenant applications. However, this feature brings some challenges, especially when it comes to handling tenant context and database connections. In this article, we will explore what happens when --concurrency is enabled in a shared deployment, how tenant context is affected, and what steps must be taken to ensure proper functioning of the application.

Firstly, it is important to understand that in the default configuration, Messenger's Worker::run() method performs all actions - fetching messages, dispatching them on the bus, and acknowledging them - within a single process. When --concurrency is set to a value greater than 1, Messenger creates a parent process that handles message fetching, acknowledgments, and retries.

Each message is then dispatched to a parallel execution strategy, which in turn runs multiple child processes (equal to the concurrency value). Each child process runs a copy of the Symfony application and processes messages using the amphp/parallel library.

In this setup, the WorkerMessageReceivedEvent is dispatched in the parent process, before the ParallelExecutionStrategy receives the message. The event handler sets TenantContext, which is used by TenantConnection to determine which database to use for each query. However, with --concurrency enabled, this context is no longer present in the child processes, leading to issues when handling messages that belong to different tenants.

To address this issue, a middleware named TenantMiddleware is suggested. This middleware reads the TenantStamp from the received envelope and sets the tenant using TenantContext. It also clears the entity manager when the tenant changes, ensuring that tenant A's entities do not interfere with tenant B's queries. The TenantMiddleware is added to the Messenger bus configuration, allowing it to run in both the parent and child processes.

With this middleware in place, the demo application successfully handles messages from multiple tenants without issues. Each note is connected to the correct tenant and stored in the appropriate database connection. The log output shows that each message is handled by the correct child process, with the tenant set correctly for each process.

In conclusion, enabling --concurrency in Symfony 8.2 Messenger can significantly improve performance in multi-tenant applications. However, it requires careful consideration of tenant context and database connections. By implementing the TenantMiddleware, developers can ensure that tenant data remains isolated, even when messages are processed concurrently. With this solution in mind, developers can confidently adopt the new concurrency feature in their Symfony applications.

Rewritten by urgent.news; may contain errors โ€” check the original.

Source: hackernoon.com

Read the original at hackernoon.com โ†’

More from HackerNoon on urgent.news

Report a problem with this story

Get the day's news by email. Daily, weekly or monthly. Sign up

More in Tech

More from Tuesday, 1 December 2026 โ†’