Video in progress

In a modular monolith, how do you define module boundaries and protect them from "melting away" over time?

I draw boundaries by business capability rather than technical layer (package by feature), give each module a small public API, and enforce the rules in CI with ArchUnit or Spring Modulith tests. Documentation doesn't protect anything; only a test that fails does.

📖 Answer

The first decision is packaging. In a project split into controller/service/repository, "payments" is scattered over three packages and every service sees every repository. If we split by feature:

uz.shop.catalog      (api/, internal/)
uz.shop.ordering     (api/, internal/)
uz.shop.billing      (api/, internal/)

How I find the boundary: which data changes together in one transaction, which concepts belong to one team, which words mean different things in different places. If two "modules" call each other synchronously in every use case, the boundary is wrong.

Protection:

class ModularityTest {
    @Test
    void verify() {
        ApplicationModules.of(ShopApplication.class).verify(); // Spring Modulith
    }
}

This test finds cycles and access to another module's internal package. If you don't use Spring Modulith, two or three ArchUnit rules are enough.

Extra rules, from experience:

  • modules don't JOIN each other's tables. Each module has its own schema or at least a table prefix; data from another module comes through the API or events;
  • calls between modules should be few and coarse-grained;
  • for asynchronous coupling, use internal events: if you later need to split a module into its own service, the events move to Kafka and the code barely changes.

The trade-off: strict boundaries slow you down at the start, because you can't just say "I'll inject that repository". But after 2–3 years this discipline is exactly what keeps the monolith splittable. Even if the boundaries were drawn wrong, moving them inside one monolith is much cheaper than doing it with microservices.