# Modular monolith

## What it is

A modular monolith is an application delivered as one deployable unit and organised into modules with explicit responsibilities. Each module exposes a boundary that other modules can use. For example, an order module can own order state, and a billing module can own invoices. The code that creates an invoice uses a defined billing operation. The billing module decides how its rules and records implement that operation.

The deployment shape and the code boundaries each have a role. Modules can communicate within the same process through calls or application events. They can share infrastructure and a release procedure. The design still gives each module authority over its data and rules. A single database can contain tables owned by different modules, with agreed rules about which module may change each table.

Teams enforce modularity through the language's package rules, static analysis, tests and code review. A structural test can identify a prohibited dependency or a cycle. A behavioural test can exercise a module through its public calls. The patterns are useful when a maintainer can find the owner of a decision and change its implementation without having to search through unrelated packages.

## Why it matters

A product can be difficult to change when many components know about the same internal fields. A pricing rule can appear in a route, a batch job and a report. Each reader may make different assumptions about which changes are allowed. Putting that rule behind one module boundary gives callers one way to ask for the result and one place to discuss its meaning.

A single deployable can also simplify the operation of parts that change together. Internal calls can follow ordinary application interfaces, and some behaviour can use a shared database transaction when the design intentionally needs one. Team ownership still matters. The service needs a way to identify which module caused a failure, and each module should have a clearly defined reason for relying on other parts.

The architecture involves trade-offs. Modules released together share a deployment and some runtime failure modes. A module that needs independent availability, scaling or ownership may eventually justify a different boundary. Keeping dependency direction and data ownership explicit makes that decision easier to evaluate. Measure the workload and the release constraints before changing the deployment shape.

## What it looks like in practice

Imagine a scheduling product with reservations and billing. The reservations module records an accepted booking and exposes a stable identifier. Billing can retrieve the information needed for an invoice through an expressed contract. If billing stores an invoice, its own module enforces the invoice rules. A code review can check that reservation handlers avoid writing billing records directly.

Test a malformed request, a completed booking and a case where billing work fails after the reservation is recorded. The way these related operations share a transaction or communicate through an event should follow the product's obligations. If billing can run later, record enough information to repeat the work and inspect its outcome. A structural check can make sure the modules do not consume one another's private implementation types.

Engineer's maritime platform architecture case study documents three layers with separate responsibilities and review-enforced boundaries between them. A layer is defined by a kind of work, while the illustrative modules here follow areas of product behaviour. Both design choices benefit from contracts that make responsibility clear. A billing use case can have its own presentation and persistence code while the business owner of the invoice remains easy to identify.

Keep a small inventory of the modules that have to change for a release. A change to invoice numbering can exercise billing's contract with reservations without requiring unrelated product code to understand the new rule. Look at the shape of integrations and their tests. If routine changes repeatedly need permission from several module owners, investigate whether the boundary supports the actual decisions the team makes.

## Where it goes wrong

Packages can be named as modules while callers import everything and access each other's tables directly. The directory layout then offers little protection when a rule changes. Use a small public interface and test the intended dependency direction. Review operations that touch another module's records and establish which module owns the durable decision.

A shared runtime can make signals difficult to assign. Attach the module and operation to logs or traces where error investigation depends on it. A billing problem may show up as a slow booking request if the end-to-end path waits for billing. A small, representative test can check the returned booking state and identify which module had to respond. During review, ask whether a production observation will help a maintainer find the owner of an unusually slow or unsuccessful step.

Events can hide a dependency too. A module might publish a notification and assume the work was completed by the time a caller reads the next page. State which effects are synchronous, which are deferred and how to observe a failure. Keep enough detail to test the overall use case as well as the behaviour inside an individual module.

Finally, watch for shared infrastructure that develops into a home for unrelated business rules. A common utility can make the system simpler when it contains stable reusable behaviour. A shared database procedure that owns decisions from several domains can make accountability harder. Review ownership with the people who operate the feature and keep examples of expected behaviour near the boundary. A module is useful when it helps a team explain where a rule lives and what changes when that rule evolves.

## Further reading
* Spring Modulith fundamentals. Describes application module boundaries and exposed parts.
* Spring Modulith structural verification. Shows how architecture rules can be checked.
* Spring Modulith application events. Discusses event publication and delivery within an application.

=> /portfolio/a-maritime-platform-built-in-clean-layers/ maritime platform architecture case study
=> https://docs.spring.io/spring-modulith/reference/fundamentals.html Spring Modulith fundamentals
=> https://docs.spring.io/spring-modulith/reference/verification.html Spring Modulith structural verification
=> https://docs.spring.io/spring-modulith/reference/events.html Spring Modulith application events

=> https://engineer.company/glossary/modular-monolith/ This page on the web
