The Problem
When each module owns its data, a single business operation can need changes in more than one module’s data store. Separate data stores can’t take part in the same ACID transaction, so you can’t commit the changes in both modules together or roll them back together.
Take a customer who updates their default shipping address. The Customers module owns the customer’s profile and saves the new address. The Orders module keeps its own copy of customer addresses, a read model (sometimes called a materialized view) it uses when placing orders. Once Customers saves its change, the address in Orders is stale until something updates it.
This isn’t unique to modular monoliths. A big ball of mud has the same problem as soon as its data is split across separate stores. The opposite is true too: if two modules share a database and a connection, they can share a transaction, whether the code is modular or not. Encapsulated modules simply make the separation, and so the need for a consistency strategy, explicit.
The Solution
Accept eventual consistency between modules, and use integration events to get there:
- The module that owns the change saves it to its own data store.
- It publishes an integration event, defined in its contracts project, describing what happened.
- Other modules handle the event and update their own data.
In the shipping address example, Customers saves the address and publishes a CustomerAddressUpdatedEvent. Orders handles it and adds or updates the address in its own read model.
// in the Customers module
await _customerRepository.UpdateAsync(customer);
var addressUpdated = new Customers.Contracts.CustomerAddressUpdatedEvent(
customer.Id, customer.DefaultShippingAddress.ToDto());
await _mediator.Publish(addressUpdated);
// in the Orders module
internal class CustomerAddressUpdatedHandler(IOrderAddressRepository addresses)
: INotificationHandler<CustomerAddressUpdatedEvent>
{
public async Task Handle(CustomerAddressUpdatedEvent evt, CancellationToken ct)
{
await addresses.UpsertAsync(evt.CustomerId, evt.Address, ct);
}
}
Neither module references the other’s implementation. Customers doesn’t know Orders exists; it only publishes an event. Orders only depends on the Customers contracts.
Two Ways to Dispatch Events
How the event gets from publisher to handler is the main design decision. There are two common options.
In-Process, In-Memory Dispatch (the default)
The publisher dispatches the event in memory, in the same process, and waits while each handler runs. With a mediator library this is a call like _mediator.Publish(evt).
This is the default for Encapsulated Module Architecture. It favors simplicity and responsiveness:
- No extra infrastructure - no broker, queue, or background worker to run and monitor
- Fast - handlers run right away, so other modules are usually consistent by the time the request completes
- Failures are visible - if a handler throws, the exception bubbles up to the publishing module, which can retry, compensate (undo its own change), or log the problem for follow-up
The cost is weaker guarantees. Events live only in memory, so if the process crashes after the publisher commits but before the handlers finish, the event is lost and the modules stay inconsistent. A failing handler can also fail an operation in a different module that already succeeded.
Durable, Queued Dispatch
The publisher writes the event to a durable store, such as a message queue or an outbox table, and handlers process it later, often in a background worker.
- Stronger guarantees - events survive crashes and restarts, and failed handlers can be retried until they succeed
- Isolation - a slow or failing handler doesn’t slow down or fail the publishing module
The cost is more infrastructure to run, more latency before other modules catch up, and more complexity. Handlers must be idempotent, because a durable queue will typically deliver a message more than once. To avoid committing data without its event (or the reverse), the publisher should save the event in the same local transaction as its data, which is what the transactional outbox pattern does.
Comparing the Options
| In-Process, In-Memory | Durable, Queued | |
|---|---|---|
| Infrastructure | None beyond a mediator | Queue or outbox, plus a worker |
| Latency | Immediate | Seconds or more |
| Lost events on crash | Possible | No |
| Handler failures | Bubble up to the publisher | Retried independently |
| Handlers must be idempotent | Recommended | Required |
| Communication | Synchronous | Asynchronous |
You don’t have to pick one for the whole system. Start with in-process dispatch, and move specific events to a durable queue when losing them would be costly.
Commit Before or After Publishing?
With in-process dispatch, the publisher has to decide when to commit its own changes.
- Commit, then publish. Handlers only ever see changes that were saved. If a handler fails, the publisher’s change is already committed, so the publisher must retry the event, compensate, or log it for repair. This is the usual choice when modules have separate data stores.
- Publish, then commit. If a handler fails, the publisher can skip its commit. But if the commit fails after handlers ran, other modules have acted on a change that never happened.
When two modules share a database and a connection, both can enlist in one transaction, and a handler failure can roll back everything. This is convenient, but it couples the modules to the same data store, which works against modules owning their data.
Where This Fits Among Saga Patterns
Software Architecture: The Hard Parts describes eight saga patterns for coordinating work that spans services, based on three choices: communication (synchronous or asynchronous), consistency (atomic or eventual), and coordination (orchestrated or choreographed).
Event-based consistency between modules is:
- Choreographed - there’s no central orchestrator; each module reacts to events on its own
- Eventually consistent - modules catch up after the original change commits
- Synchronous by default, with in-process dispatch, which the book calls the Time Travel Saga
- Asynchronous when you use durable queues, which the book calls the Anthology Saga
If a workflow needs a central place to track progress, handle failures, and run compensating steps, consider an orchestrated saga instead.
Benefits
- Modules keep their own data - no shared transactions or shared tables between modules
- Loose coupling - publishers don’t know who handles their events
- Simple to start - in-process dispatch needs nothing beyond a mediator
- Room to grow - individual events can move to durable queues, which also eases a later move to microservices
