Skip to content

Lesson 3 of 9 · 25m

Monolith First: Designing Modules Before Microservices

Start with explicit modules in one deployable, enforce ownership seams, and split only when operational evidence justifies the cost.

The worst distributed systems I have seen were built by teams that had never enforced a boundary in a monolith. They had thirty services, one shared database, and deploy order ceremonies. Distribution did not create modularity; it multiplied coupling across networks. Start monolith-first unless you already have a proven operational reason not to.

A module is an ownership boundary

A good module owns a business capability, its data, and its invariants. It exposes a small public API. Other modules do not reach into its tables or private classes.

billing/
  api.py              public application commands and queries
  domain.py           invoices, ledger entries, payment state machine
  storage.py          SQL repositories owned by billing
  events.py           integration events billing emits
identity/
  api.py              resolve user, organization membership
  domain.py
  storage.py

The crucial rule: identity may call identity.api; it may not import identity.storage or join identity_users directly from a billing query because a deadline is close.

Enforce seams mechanically

If a boundary matters, make violations visible. Dependency checks beat architecture wiki pages.

# tests/test_architecture.py
def test_billing_does_not_depend_on_identity_internals():
    violations = find_imports(
        package="billing",
        forbidden_prefixes=["identity.storage", "identity.models"],
    )
    assert violations == []

Run it in CI. Without enforcement, boundaries survive only until the first urgent ticket. You do not need a heavyweight framework for this; a few import-linter or custom static checks are enough to catch erosion early.

Share data through contracts, not tables

A modular monolith still uses one database in many cases. That is fine. The mistake is treating tables as public APIs. Prefer module-owned tables plus explicit read models or application queries.

CREATE TABLE billing_invoices (
    invoice_id text PRIMARY KEY,
    organization_id text NOT NULL,
    status text NOT NULL CHECK (status IN ('draft', 'open', 'paid', 'void')),
    amount_minor integer NOT NULL CHECK (amount_minor >= 0),
    currency char(3) NOT NULL,
    version integer NOT NULL DEFAULT 0
);

If reporting needs invoice and identity data together, build a reporting projection or consume owned exports. A foreign key is sometimes acceptable inside one ownership boundary, but cross-module foreign keys create deployment and deletion coupling you will regret.

Split for operational reasons, not fashion

Good reasons to extract a service include independent scaling, a different reliability target, team ownership at a size where coordination hurts, regulatory isolation, or an expensive resource boundary. Bad reasons include "microservices are cleaner," "the file is getting large," or "we might need it later." A split turns every ambiguity into network behavior, data duplication, retries, versioning, and distributed debugging.

Design the extraction path before you need it

Keep module APIs asynchronous-tolerant. Avoid lazy object graphs that cross boundaries. Record integration events when facts change. Use explicit transaction boundaries. These habits let you extract a module without rewriting the business.

result = billing.close_invoice(invoice_id, actor=actor)
events.emit("billing.invoice_closed", {
    "invoice_id": result.invoice_id,
    "organization_id": result.organization_id,
    "closed_at": result.closed_at.isoformat(),
})

The event is a contract. Do not publish ORM rows or internal enum representations by accident.

Field notes

  • One deployable does not mean one object graph. Modularity is independent of process count.
  • Cross-module database transactions feel convenient until extraction; document every one as debt.
  • A shared kernel should be tiny: identifiers, money, time primitives. If it contains business workflows, it is a hidden monolith inside the monolith.
  • Team boundaries and module boundaries drift apart over time. Realign ownership before splitting infrastructure.
  • Measure coupling by change sets: files that change together probably belong together, regardless of folder names.

Was this lesson helpful?

Sign in to rate