Modular monolith boundaries
Prerequisite: node-specialist-version-gated-apis. Objective: Design catalog and order modules. Tested locally on Node.js 22.16.0 Linux. Each example is independent. Use a fresh disposable working directory, save as main.mjs, then run node main.mjs. The harness uses a five-second timeout and synthetic environment. These are static code blocks, not ByteMind interactive execution.
How it works
Start with a catalog module owning product facts and an order module owning order validation. A dependency on the catalog's public lookup is simpler than exposing its mutable storage. Keep domain boundaries meaningful: an order may store the agreed price independently of future catalog updates. Inject the lookup function to test the order module without the full application. An in-process call is not a remote service call, and a module boundary is not a security boundary. Shared transactions may simplify consistency in one database; split deployment only when independent release, scaling or team ownership justifies the operational cost.
Compose catalog and order through a narrow contract
function catalog(){const products=new Map([['tea',{price:4}]]);return {find(id){const p=products.get(id);return p?{...p}:null;}};}function orders(find){return {quote(id,quantity){const p=find(id);if(!p||!Number.isInteger(quantity)||quantity<1)return null;return {id,quantity,total:p.price*quantity};}};}const c=catalog(),o=orders(c.find);console.log(JSON.stringify(o.quote('tea',3)));console.log(o.quote('missing',1));Expected output:
{"id":"tea","quantity":3,"total":12}nullOrders receives only the lookup contract, not the catalog storage.
Test the boundary with an injected fake
import assert from 'node:assert/strict';function total(find,id,qty){const product=find(id);if(!product||qty<1||!Number.isInteger(qty))throw new Error('invalid order');return product.price*qty;}assert.equal(total(()=>({price:6}),'toy',2),12);assert.throws(()=>total(()=>null,'missing',1),/invalid order/);console.log('contract cases passed');Expected output:
contract cases passedThe fake isolates domain logic; it does not prove a real catalog integration.
Common mistakes
Do not introduce HTTP between modules just to claim microservices, or let every module mutate shared tables without an ownership contract.
Practice
Design catalog, orders and inventory modules with their public operations, ownership rules and one transaction boundary.
Reference candidate: node-modular-monolith-boundaries-lookup (route unverified). Original application architecture reasoning and deterministic model; no external implementation claimed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modular monolith boundaries — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What defines a modular monolith here?
Why inject the catalog lookup?
When might separate services be justified?