Topic 053 / Core
Modular monolith architecture
Chapter 4.4 · Dependencies and Application Structure
What and why
With internal direction settled by layering, clean, or hexagonal architecture (→ 50–52), a separate decision remains: how many deployables should the application be? Splitting into services (→ 143) buys independent deployment and scaling at the cost of network calls, distributed data, and operational overhead — a cost many applications should not pay yet. A modular monolith is one deployable unit containing multiple modules with explicit, enforced boundaries — each module owns its own tables and exposes only a defined interface to the others, with no module reaching into another's data directly — giving most of the organizational benefit of service boundaries without the distribution cost. Builds on: → 49 (the same dependency discipline, applied at the module/deployment-unit level instead of the class level).
Diagram
┌──────────────────────── one deployable process ────────────────────────┐
│ TaskModule CommentModule NotificationModule │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ own API │◄───────►│ own API │ │ own API │ │
│ │ own │ │ own │ │ own │ │
│ │ tables │ │ tables │ │ tables │ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ Modules call each other only through their public interface — │
│ never by querying another module's tables directly. │
└──────────────────────────────────────────────────────────────────────┘
How it works
- Draw module boundaries along the lines a future service split would use — bounded contexts (→ 56) usually drive this.
- Give each module its own tables (or schema) and forbid any other module from querying them directly — only through the owning module's public interface, as an in-process call, never a network call.
- Enforce the boundary mechanically where the language allows it (package-private visibility, separate schemas, a lint rule) rather than relying on convention alone.
- Deploy the whole thing as one process: one transaction manager, one deployment pipeline, one set of operational concerns — no distributed tracing or network retries needed yet.
- When a module's growth or scaling needs genuinely diverge from the rest, its already-explicit interface is exactly what a future extraction into a separate service (→ 143–145) would use — the boundary work is not wasted either way.
Practical example
(illustrative). In the task manager — Project 1's deliberate
shape — TaskModule, ProjectModule, and NotificationModule each own their own tables;
completing a task calls NotificationModule through its public notify(event)
interface, never by inserting a row into a table NotificationModule owns.
When to use it
- Use when: enforced boundaries and future flexibility are wanted without paying for a distributed system today — the default shape for Projects 1, 2, 3, 5, and 6.
- Avoid when: the application genuinely needs independent deployment, independent per-module scaling, or independent team ownership right now — then the boundary needs to be a network boundary, not an in-process one (→ 143).
- Failure scenarios: "modules" that still share one database schema with foreign keys crossing module boundaries — the module names exist on paper, but a change to one module's table can silently break another, meaning there is no real boundary at all.
- Simpler alternative: no modules at all — a single undifferentiated codebase — for a small application with one team and no near-term growth; introduce module boundaries only once a second cohesive concern, and ideally a second contributor, appears.
Practice
(20–30 min; no lab services — any language). Take two "modules" sharing
one table; give each its own table, define one public interface method for the other to
call through, and enforce no cross-module table access (even a grep for cross-module
SQL/ORM calls counts). Done when the only way TaskModule's data changes from
CommentModule's code is through TaskModule's public interface.
Key takeaway
A modular monolith is one deployable with service-grade internal boundaries, deferring the network cost until it is actually earned. Done when you can: name every module's public interface and show no other module bypasses it. Used next in → 54 (domain-driven design usually supplies the lines these boundaries follow), → 143 (the honest, explicit decision of whether to ever cross into microservices), → 145 (these same boundaries become service boundaries if that day comes).
References
None — this topic makes no external claims.
Source directory