Data Ownership in a Modular Monolith: Schema per Module

Keep modules independent at the data layer: schema per module, no cross-module joins or foreign keys, read models and reporting.

Published November 27, 2026

Code boundaries are only half of modularity. If every module can read and write every table, the database becomes a hidden integration layer and the boundaries in code are an illusion. A true modular monolith gives each module ownership of its data: its own schema or table prefix, its own migrations, and no other module touching those tables directly.

The concept

The rule is simple to state: only the owning module reads or writes its tables. Other modules get data through the owner's public API or by keeping their own copy updated from events. This allows a module to change its schema freely, as long as its public API and events stay compatible, and it is what makes extracting a module into a service later realistic.

In practice that means no foreign keys across module boundaries, no joins across module tables, and no shared "core" tables written by everyone. It feels restrictive at first, especially for reporting, but there are good patterns for each need: local read models, API composition, and a separate reporting database or warehouse fed by events or change data capture.

Levels of data isolation

ApproachIsolationEffortNotes
Table prefix per module (catalog_*, billing_*)ConventionLowWorks everywhere; enforce with tests
Schema per module (PostgreSQL schemas)StrongLow-mediumGrant each module role access only to its schema
Database per module (same server)StrongMediumSeparate connections; no cross-db joins
Separate database serversStrongestHighUsually only when extracting a service

Getting data you do not own

queryorCheckout needs product namesOption A: call catalog APIlive dataOption B: local read modelupdated by ProductRenamed events
Two ways to use another module's data without touching its tables.

PostgreSQL schemas with role-based access

Schemas give each module a namespace and, combined with database roles, technical enforcement of ownership. In a single application this usually means one role per module in tests and CI (to catch violations) even if production uses one connection role.

CREATE SCHEMA catalog;  CREATE SCHEMA checkout;  CREATE SCHEMA billing;
CREATE ROLE checkout_module NOLOGIN;
GRANT USAGE ON SCHEMA checkout TO checkout_module;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA checkout TO checkout_module;
-- no grants on catalog or billing: cross-module queries fail in tests

Migrating toward ownership

  1. Inventory every table and assign exactly one owning module
  2. Find cross-module queries and joins (grep, query logs)
  3. Replace each with an API call, event-fed read model, or move the logic to the owner
  4. Drop cross-module foreign keys; keep IDs as plain columns
  5. Enforce with architecture tests or schema permissions
Untangling data in an existing monolith.

Practical examples

Local read model fed by events

Checkout keeps the product fields it needs for order lines.

CREATE TABLE checkout.product_snapshot (
  product_id   text PRIMARY KEY,
  title        text NOT NULL,
  updated_at   timestamptz NOT NULL
);
-- handler for catalog.ProductRenamed:
INSERT INTO checkout.product_snapshot (product_id, title, updated_at)
VALUES ($1, $2, now())
ON CONFLICT (product_id) DO UPDATE SET title = EXCLUDED.title, updated_at = now();

Migrations per module

Each module owns its migration folder; the runner applies all.

src/modules/catalog/migrations/2026_11_01_000001_create_catalog_products.php
src/modules/checkout/migrations/2026_11_03_000001_create_checkout_orders.php
src/modules/billing/migrations/2026_11_05_000001_create_billing_invoices.php

Test that fails on cross-module table access

A simple guard: scan SQL in a module for other modules' prefixes.

for module in catalog checkout billing; do
  others=$(echo catalog checkout billing | tr ' ' '\n' | grep -v "^$module$" | paste -sd'|' -)
  if grep -rEn "\b($others)_[a-z_]+\b" "src/modules/$module" --include=*.sql --include=*.php; then
    echo "module $module touches another module's tables"; exit 1
  fi
done

Common mistakes

  • Keeping shared tables that several modules write, with no clear owner.
  • Cross-module foreign keys that lock schemas together.
  • Reporting queries joining every module's tables in the production database.
  • Copying data between modules without a clear source of truth.
  • Assuming separate schemas alone enforce ownership without permissions or tests.

Key takeaways

  • Each table has exactly one owning module; only it reads and writes it.
  • Use schemas or prefixes, ideally enforced by permissions or tests.
  • Get others' data via public APIs or event-fed read models.
  • Do reporting in a separate store fed by events or CDC.

FAQ

Are foreign keys forbidden?

Within a module, use them freely. Across modules, store IDs as plain columns and validate through the owning module's API.

How do I build reports that span modules?

Feed a reporting database or data warehouse from events or change data capture, or compose data via module APIs for small reports.

Isn't duplicating data in read models risky?

It is a deliberate, bounded copy with a clear source of truth and update mechanism. That is far safer than implicit coupling through shared tables.

More Architecture articles