Skip to Content
Mengty Learns
Architecture

Modular Monolith

Organize a single deployable application into isolated business modules that communicate only through public APIs—with zero shared database tables.

explanationintermediateTested with typescript 7.0.2Updated Nov 27, 2026

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

TL;DR

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.

Direct imports and cross-module SQL joins turn a monolith into a tangled ball of mud.

Solution

A Modular Monolith applies two hard rules inside a single repository and deployment:

  1. Private internals, public index.ts API: src/modules/inventory/index.ts exports InventoryApi (reserveStock()). No other module may import from src/modules/inventory/internal/*.
  2. No shared tables: orders owns orders.* tables (separate PostgreSQL schema or prefixed tables with no foreign keys across modules). If orders needs stock availability, it calls InventoryApi—never SELECT * 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)

Overview of Module A → Public API → Module B (No Shared DB). 6 steps.
Overview6 steps in this flow

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

When to use
  • 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.
When to avoid
  • 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

Pros
  • Single deployment, single CI pipeline, and sub-millisecond in-process module calls.
  • Easy to refactor across module boundaries while the domain is still evolving.
Cons
  • 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