Modular Monolith
One deployable with strongly enforced module boundaries, private data per module and in-process events, giving most of the benefits of microservices without the distributed-systems cost.
Intent
Keep a single deployment unit but structure it as independent modules aligned to bounded contexts, each with a public API, private internals and (ideally) its own schema. Modules communicate through explicit interfaces or in-process events.
flowchart TB
subgraph App[Single deployable]
O[Orders module<br/>public API + private impl] -- events --> I[Inventory module]
O -- API call --> P[Payments module]
C[Catalog module]
end
O --> SO[(orders schema)]
I --> SI[(inventory schema)]
P --> SP[(payments schema)]
C --> SC[(catalog schema)]Rules that make it work
- Modules expose a small API package; everything else is internal (enforced by build tools or tests).
- No module reads another module's tables.
- Cross-module workflows use events or explicit API calls, never shared mutable entities.
- Architecture tests fail the build when boundaries are violated.
Java example with Spring Modulith
// Module: com.shop.orders (top-level package = module)
@Service
@RequiredArgsConstructor
public class OrderService {
private final OrderRepository orders;
private final ApplicationEventPublisher events;
@Transactional
public Order place(PlaceOrder cmd) {
Order order = orders.save(Order.from(cmd));
events.publishEvent(new OrderPlaced(order.id(), order.lines())); // persisted via event publication registry
return order;
}
}
// Module: com.shop.inventory
@Component
class InventoryListener {
@ApplicationModuleListener // async, transactional, retried
void on(OrderPlaced event) { /* reserve stock */ }
}
// Verification test
class ModularityTests {
@Test void verifiesModularStructure() {
ApplicationModules.of(ShopApplication.class).verify();
}
}[!VERSION] Spring Modulith 1.x targets Spring Boot 3; check compatibility notes when moving to Spring Boot 4.
Real-world use
Shopify's core platform, many internal enterprise systems, and teams preparing for future extraction ("monolith first", as Martin Fowler recommends) use this pattern.
Pros and cons
| Pros | Cons |
|---|---|
| Fast in-process calls, local transactions | Single deployable: one release train |
| Clear boundaries make later extraction cheap | Scaling still all-or-nothing |
| Simple operations | Needs discipline and tooling to keep boundaries |
| Refactoring boundaries is cheap compared to services | One technology stack |
When not to use
When independent deployability per team is essential today, or modules require different runtime characteristics (GPU inference, extreme scale).
Interview questions
How would you enforce module boundaries in Java?
Package conventions plus ArchUnit or Spring Modulith verification tests in CI; Java Platform Module System (JPMS) or Gradle/Maven multi-module builds with restricted dependencies; separate database schemas with separate credentials per module.
Why start with a modular monolith instead of microservices?
Boundaries are often wrong at the beginning. Moving a boundary inside one codebase is a refactor; moving it between services is a migration. You defer distributed-systems complexity until the business and team structure justify it.