All posts
architecturebackend

Start With a Modular Monolith, Not Microservices

One deployable, hard walls inside it. How we split a reseller backend into modules that can't reach into each other, and why that beats splitting it into services on day one.

Muhamad Choirul Adam 3 min readPhoto: Unsplash

Two months into the reseller project, someone asked whether we should split it into microservices before it got too big. Orders, payments, resellers, notifications: four services, four deploys, four databases.

We didn't. We kept one app and drew hard lines inside it. Here's the shape, and why I think it's the right default for a small team.

What microservices actually buy you

Microservices solve organizational problems: many teams shipping independently, parts that need to scale very differently, a blast radius you want to contain. They charge for it in network calls that can fail, data spread across databases, distributed transactions, and a lot of infrastructure.

A small team with one product has none of those problems yet. What we did have was the problem underneath them: code where everything could call everything. That's what turns into a big ball of mud, and you can fix it without a network in between.

Modules with a front door

Each module is a folder with one public file. Everything else inside is private.

src/modules/
  orders/
    index.ts        ← the only thing other modules may import
    service.ts
    repository.ts
    routes.ts
  payments/
    index.ts
    ...
  resellers/
  notifications/
src/modules/orders/index.ts
// The public surface of the orders module. Keep it small.
export { createOrder, getOrder } from "./service";
export type { Order, OrderStatus } from "./types";

The rule is simple: payments may import from @/modules/orders, never from @/modules/orders/repository. A lint rule makes it stick, because a rule nobody enforces is a suggestion.

.eslintrc.cjs
module.exports = {
  rules: {
    "no-restricted-imports": [
      "error",
      { patterns: [{ group: ["@/modules/*/*"], message: "Import the module's index, not its internals." }] },
    ],
  },
};

Each module owns its tables

This is the part teams skip, and it's the part that matters most. Only the orders module writes to orders and order_items. If payments needs an order's total, it asks getOrder(). It doesn't join the table.

Shared tables are how a monolith quietly becomes impossible to split. If every module owns its data, extracting one later is mostly moving a folder and replacing function calls with HTTP calls.

Talking without reaching in

Sometimes a module needs to react to something another module did. When an order is paid, notifications should send a WhatsApp message. Payments shouldn't know notifications exists.

Rendering diagram…

An in-process event emitter is enough to start. The handlers run in the same process, so there's nothing new to deploy. When a handler needs retries, it writes a row to the job queue instead of doing the work inline.

When to actually split

A module earns its own service when it has a reason the others don't share:

  • It needs to scale on its own, like an image pipeline that eats CPU while the rest of the app idles.
  • Another team owns it and keeps getting blocked by your deploys.
  • It needs a different runtime or a hard security boundary.

Until one of those is true, the modular monolith gives you most of the benefits: clear ownership, code you can reason about, a path to splitting later. It costs you one deploy and zero network calls.