Modular Monolith
Organize a single deployable application into isolated business modules that communicate only through public APIs—with zero shared database tables.
TL;DR
A Modular Monolith runs as one deployable process but enforces strict module boundaries: each business module (orders, inventory, billing) owns its own internal code and database tables, and modules talk only through explicit public APIs or in-process events.
Intent
Get the clean domain boundaries of microservices without paying the operational tax of network calls, distributed tracing, and multi-repo deployments.
Problem
In an unmodularized monolith, BillingService imports orders/internal/order-model and joins directly against orders_items in SQL. Over time, every module depends on every other module's internals—turning simple schema changes into risky whole-app rewrites.
Solution
A Modular Monolith applies two hard rules inside a single repository and deployment:
- Private internals, public
index.tsAPI:src/modules/inventory/index.tsexportsInventoryApi(reserveStock()). No other module may import fromsrc/modules/inventory/internal/*. - No shared tables:
ordersownsorders.*tables (separate PostgreSQL schema or prefixed tables with no foreign keys across modules). Ifordersneeds stock availability, it callsInventoryApi—neverSELECT * FROM inventory_items.
Because everything runs in one process, InventoryApi is a fast TypeScript method call today—and if inventory ever needs to scale independently later, extracting it to a network service only requires swapping the InventoryApi adapter.
Real-world analogy
▶ Flow: watch it work
Module A → Public API → Module B (No Shared DB)
Press Start or tap any step dot to trace the path.
▸All steps (6)
Code
Enforcing module boundaries with explicit index.ts exports and DTO contracts:
// src/modules/inventory/index.ts (Public API — the ONLY file other modules may import)
export interface ReserveStockResult {
reserved: boolean;
remaining: number;
}
export interface InventoryApi {
reserveStock(sku: string, qty: number): Promise<ReserveStockResult>;
}
// src/modules/orders/place-order.ts (Consumes InventoryApi, never inventory/internal/*)
export class PlaceOrder {
constructor(private readonly inventory: InventoryApi) {}
async execute(sku: string, qty: number): Promise<void> {
const res = await this.inventory.reserveStock(sku, qty);
if (!res.reserved) throw new Error('Out of stock');
}
}When to use / when to avoid
- You are building a product with multiple business domains (Orders, Billing, Auth, Notifications) and a small-to-medium team.
- You want clean boundaries now while keeping the option to extract a microservice later.
- Different modules have radically different scaling or runtime needs (e.g., Python ML inference vs Node.js web API).
- A tiny single-purpose CRUD app with 2–3 endpoints where module folders add needless boilerplate.
How to implement
Split src/modules/<domain>/
Group code by business capability (orders/, inventory/), not technical layer (controllers/, services/).
Add lint/compiler boundary checks
Configure ESLint no-restricted-imports or eslint-plugin-boundaries so deep imports into modules/*/internal/** fail CI.
Separate database schemas
Use separate Postgres schemas (orders.*, inventory.*) or distinct DB users in CI to catch accidental cross-module SQL joins.
Pros / Cons
- Single deployment, single CI pipeline, and sub-millisecond in-process module calls.
- Easy to refactor across module boundaries while the domain is still evolving.
- Requires discipline and lint rules—nothing at the OS level stops a hurried developer from bypassing a boundary.
- All modules share the same process fate and scaling unit.
Relations
Check yourself
Check Yourself
1. In a Modular Monolith, the Billing module needs a customer's shipping country from the Orders module. What is the right way to get it?
2. How can a TypeScript team enforce module boundaries automatically in CI?
Sources
Related Notes
Vertical Slice Architecture
Organize code by feature slice (request → handler → data) instead of horizontal technical layers, minimizing cross-feature coupling.
Monolith vs Modular Monolith vs Microservices
Weigh the honest costs of distributed systems, team topology (Conway's Law), and why starting with a modular monolith is the default choice.