Communication Between Modules: Public APIs, Events and Contracts
How modules in a modular monolith should talk: synchronous public interfaces, in-process events, DTOs, avoiding cycles and cross-module transactions.
Published November 27, 2026
In a modular monolith, modules run in the same process, so technically any code can call any other code. The discipline that makes the architecture work is choosing how modules communicate: through a narrow public API for queries and commands, and through domain events for reactions. Get this right and modules stay independent; get it wrong and you rebuild the tangled monolith with extra folders.
The concept
A module's public API is a small set of functions or a facade class that other modules may call. It accepts and returns plain data transfer objects (DTOs), never internal entities or ORM models, so callers cannot accidentally depend on internal structure or lazy-load another module's data. Use synchronous calls when the caller needs an answer now, for example checkout asking pricing for the current price.
Domain events are for reactions. When something meaningful happens (OrderPlaced), the owning module publishes an event, and any interested module subscribes. The publisher does not know or care who listens. This keeps dependencies pointing one way and makes it easy to add behaviour, such as sending an email or updating analytics, without changing the publisher. Events can be dispatched in-process, synchronously or via a queue for async handling.
Two communication styles
Choosing the style
| Use a public API call when… | Use a domain event when… |
|---|---|
| The caller needs data or a decision now | Other modules need to react to something that happened |
| The operation must succeed for the caller to continue | Reactions can fail and retry independently |
| There is exactly one obvious provider | There may be zero, one or many interested modules |
| Example: checkout asks pricing for a quote | Example: OrderPlaced triggers invoice, email, analytics |
Avoid dependency cycles
If catalogue calls checkout and checkout calls catalogue, the two modules can no longer evolve or be extracted independently. Keep the module dependency graph acyclic. When a cycle appears, one direction usually belongs as an event: instead of pricing calling catalogue when a price changes, pricing publishes PriceChanged and catalogue updates its display copy.
Transactions across modules
Because modules share a database server, it is tempting to wrap work in several modules in one database transaction. That is acceptable sparingly but couples modules and blocks future extraction. Prefer each module committing its own changes, with events published via a transactional outbox so reactions happen reliably after commit. Where strict atomicity across modules is truly required, that is often a sign the two belong in the same module.
// inside checkout's own transaction
await db.transaction(async (tx) => {
await tx.insert(orders).values(order)
await tx.insert(outbox).values({ type: 'checkout.OrderPlaced', payload: JSON.stringify(evt) })
})
// an outbox dispatcher delivers events to subscribers after commitPractical examples
Public API with DTOs (PHP/Laravel)
The facade is the only class other modules may use.
namespace Modules\Pricing;
final class PricingApi
{
public function quote(CartDto $cart): PriceQuoteDto
{
// internal services, models and rules stay hidden in Modules\Pricing\Internal
return $this->quoter->quote($cart);
}
}
final class PriceQuoteDto
{
public function __construct(public readonly int $subtotalPaise, public readonly int $taxPaise) {}
}Versioned event contract
Treat events as published contracts between modules.
export type OrderPlacedV1 = {
type: 'checkout.OrderPlaced'
version: 1
orderId: string
customerId: string
totalPaise: number
occurredAt: string
}Check for cycles in CI
dependency-cruiser can fail the build on circular module dependencies.
{ name: 'no-module-cycles', severity: 'error',
from: { path: '^src/modules/([^/]+)/' },
to: { circular: true } }Common mistakes
- Returning ORM entities from public APIs, leaking internal structure and lazy loading.
- Modules importing each other's internal classes "just this once".
- Using events for queries, then waiting for "reply" events.
- Large multi-module database transactions that couple modules tightly.
- Circular dependencies between modules.
Key takeaways
- Use narrow public APIs with DTOs for queries and commands.
- Use domain events for reactions; publishers do not know subscribers.
- Keep the module dependency graph acyclic.
- Prefer per-module transactions with an outbox over cross-module transactions.
FAQ
Should modules communicate over HTTP inside the monolith?
No. In-process calls are faster and simpler. Keep the interface narrow and DTO-based so that switching to HTTP later is straightforward if a module is extracted.
Synchronous or asynchronous event handlers?
Start synchronous in-process for simplicity, but design handlers to be idempotent so they can move to a queue later. Use async handlers for slow work such as emails.
Can modules share DTO classes?
Each module should own the DTOs in its public API. A tiny shared kernel for truly universal types (Money, IDs) is fine.