Skip to content

The modular monolith: an alternative to microservices

B2Bnetwork team · · 6 min

Technology and trends

In brief

A modular monolith is an application deployed as a single unit but divided internally into modules with clear boundaries: each module has its own data model and public API and does not reach into the internals of others. It gives most of the order benefits of microservices without the costs of a distributed system: network calls between services, distributed transactions, and many pipelines. It is a good choice for one or a few teams and a good starting point if you later need to extract individual services.

How a modular monolith differs from a plain monolith and from microservices

A modular monolith differs from a plain monolith in the discipline of its boundaries, and from microservices in how it is deployed. The simplest way to show this is a table:

FeaturePlain monolithModular monolithMicroservices
Deploymentone unitone uniteach service separately
Boundaries in codeinformal, easy to crossenforced and checked by testsenforced by the network
Dataone shared database, everyone reads everythingeach module has its own tables or schemaeach service has its own database
Communicationcalling any classcalling the module's public API or an eventHTTP, gRPC, queues
Transactionslocallocal within a module, events between modulesdistributed, sagas
Operating costlowlowhigh: network, monitoring, versioning

Over time a plain monolith turns into a "big ball of mud": a change in the invoice module breaks the reports, because the reports read the invoice table directly. Microservices enforce order through the network, but you pay for it with latency, partial failures, and harder debugging. A modular monolith enforces order through rules in the code and architecture tests.

How to define modules

Modules are defined by business areas, not technical layers. A "Payments" module contains its own controller, logic, and data access. A split into "controllers," "services," and "repositories" is not a set of modules but layers that cut across every area.

The approach known from Domain-Driven Design helps: we look for bounded contexts, that is, areas in which the same word means one thing. In an insurer's system, a "policy" in the sales module is an offer with a price, and in the claims module it is a contract with a scope of coverage. If both modules have their own policy model, they can change independently.

Practical questions when drawing boundaries:

  • Does this area have a separate business owner?
  • Does it change at a different pace than the rest (e.g. tariffs every quarter, the core once a year)?
  • Can its API be described in a few operations, without exposing internal tables?
  • Can its data have its own schema in the database?

How to enforce boundaries

Module boundaries must be checked automatically, because an agreement in the documentation stops applying at the first urgent release. Ways to do it:

  1. Visibility in the language. The module's public API in a separate package, the rest internal (e.g. `internal` in C# or Kotlin, modules in Java, private packages in Go).
  2. Architecture tests. Tools such as ArchUnit for Java check in CI that the "Reports" module does not import classes from inside the "Invoices" module. Breaking the rule stops the pipeline. In the Spring ecosystem, the Spring Modulith project provides similar checks and module documentation.
  3. Separate database schemas. A module has its own schema and its own database account, which cannot see the tables of other modules. Joining the tables of two modules in a query becomes impossible, not just forbidden.
  4. Communication through events. When an order is paid, the "Payments" module publishes an event and the "Shipping" module reacts to it. In-process events are simple, and when a service is extracted later, they are swapped for a queue.

An example ArchUnit rule:

@ArchTest
static final ArchRule reports_do_not_reach_into_invoices =
    noClasses().that().resideInAPackage("..reports..")
        .should().dependOnClassesThat().resideInAPackage("..invoices.internal..");

When to choose a modular monolith and when microservices

A modular monolith is a good default choice until a concrete reason appears to deploy parts separately. Such reasons include:

  • Independent teams. Several teams block each other during a shared release because each has a different pace of change.
  • Different scaling requirements. One module, e.g. product search, needs ten instances while the rest need one.
  • Different reliability or security requirements. The payments module must run in a separate network zone with its own audit cycle.
  • Different technology. A computation module requires a different language or environment.

Conversely: if one team works on the project and the load is even, microservices add work without benefit. Every service means a separate pipeline, monitoring, API versioning, contract tests, and handling of network failures. That cost pays off only at an appropriate organizational scale.

We describe how to choose a communication style between modules and services in the article REST, GraphQL, or gRPC.

Migrating in both directions

A modular monolith makes the move easier in both directions. You move from an old monolith to a modular one gradually: define modules, move tables into schemas, replace direct references with a public API, and add architecture tests. Each step can be deployed separately, without stopping development.

When a module has clean boundaries, extracting it into a separate service comes down to swapping an in-process call for a network call and moving the schema to a separate database. The strangler fig pattern, that is, gradually shifting traffic to the new service, then works much more simply than with tangled code.

The reverse also happens: a team that split a system into too many small services merges them into a few larger ones, or into a modular monolith, to reduce maintenance costs. That is not a failure, just a correction of the decision to match the real scale.

Any such change benefits from good regression testing. B2Bnetwork engineers build test automation and develop software in projects for banks, insurers, and operators. The scope of services is described in the What we do section.

Questions and answers

Can a modular monolith be scaled?

Yes. The whole application can be run in many instances behind a load balancer, like any stateless service. The limitation is that all modules scale at once. If one module needs many times more resources than the rest, that is a signal it may be worth extracting into a separate service.

Can modules share one database?

They can use one database server, but each should have its own schema, or at least its own tables that other modules cannot access. Shared tables read by many modules are the most common cause of sliding back into a tangled monolith and the biggest obstacle to extracting a service later.

How do you test a modular monolith?

Each module is tested through its public API, with unit tests inside and integration tests at the boundary. Architecture tests in CI ensure modules do not reach into each other's internals. E2E tests cover only the most important paths across several modules, in line with the proportions of the test pyramid.

Where do you start when cleaning up an old monolith?

With a dependency map: which packages refer to which, and which tables each area reads. Then pick one area with clear boundaries, give it a public API, move its tables to a separate schema, and add an architecture test. Clean up further modules gradually, alongside planned changes.

What it looks like in a project

Considering a new technology for your system? Talk to an engineer.

Let's talk

Read more: Technology and trends