Inter-Module Communication Patterns
What you'll learn: How modules in a monolith should talk to each other without creating tight coupling or breaking independence.
The Communication Challenge
Once you've defined clear module boundaries and enforced their independence, you need a way for modules to collaborate. The goal is communication without direct dependencies—meaning Module A shouldn't import Module B's internal classes or reach into its database tables.
Think of modules like separate companies doing business together. They don't share office space or internal documents; instead, they communicate through official contracts, scheduled meetings, or formal letters. Software modules need similar boundaries.
Three Primary Patterns
Interfaces (Contracts): Module A depends on an abstract interface, and Module B implements it. Module A never sees B's internals—only the contract. This is dependency inversion in action, letting you swap implementations or test with mocks.
Events (Publish-Subscribe): When something significant happens in Module A, it publishes a domain event. Module B subscribes to events it cares about and reacts independently. Neither module knows the other exists—they communicate through an event bus or message broker. This decouples modules in time and knowledge.
Mediators (Message Passing): A central coordinator receives requests from modules and routes them appropriately. Instead of Module A calling Module B directly, it sends a message to the mediator, which handles delivery. This adds indirection but centralizes communication logic.
Why It Matters
Direct module-to-module calls (import CustomerModule from '../customer') create hidden dependencies that make modules harder to understand, test, and evolve separately. These patterns preserve the autonomy you worked hard to establish with bounded contexts and module boundaries.
Key Takeaway: Modules should communicate through well-defined interfaces, asynchronous events, or mediators—never by directly accessing each other's internal implementation details or data stores.