Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Design a modular monolith in Spring Boot with enforced module boundaries for orders, inventory, billing and customers.

We give each module, orders, inventory, billing and customers, one top-level package with a small public API at its root and internal subpackages that other modules cannot use, and we verify that with Spring Modulith's ApplicationModules.verify() in a test, which also documents the structure. Modules talk only through public interfaces or application events, never through each other's repositories, each module owns its own tables with no cross-module joins or shared entities, and Maven or Gradle modules can add compile-time enforcement. Modules built this way can later be extracted into services if there is a reason to.

Why this question is asked

A modular monolith is a single deployable application whose code is divided into modules with real, enforced boundaries. Many teams choose it because it gives most of the organisational benefits of microservices, clear ownership and independent change, without the network calls, distributed transactions and operational cost. The catch is that boundaries in one codebase are easy to break by accident: one convenient import and orders is reading billing's tables. Interviewers ask this design question to see whether we know how to make the boundaries real. In this answer, we will lay out the package structure, enforce it with Spring Modulith, design the communication between modules, split the data, add build modules where needed, and see how this sets up a later extraction.

1The Package Structure

Spring Modulith, a Spring project for building modular monoliths, treats every direct subpackage of the main application package as a module. So we create one package per business capability under com.acme.shop. Types in a module's root package form its public API; types in subpackages such as internal are hidden from other modules by default.

Text
com.acme.shop
ShopApplication.java @SpringBootApplication
orders/
OrderManagement.java public API: placeOrder(), findOrder()
OrderPlaced.java public event
internal/ OrderEntity, OrderRepository, OrderController, pricing rules
inventory/
InventoryManagement.java public API: reserve(), release()
internal/
billing/
internal/ listens to OrderPlaced, creates invoices
customers/
CustomerDirectory.java public API: findCustomer()
CustomerSummary.java public record returned by the API
internal/

Notice that each module's public surface is a handful of classes. Everything else, including entities, repositories and controllers, lives under internal and is only reachable from inside the module. Inside a module we can keep a simple layering; the module boundary matters more than the internal style.

2Verifying the Boundaries With Spring Modulith

Package conventions alone are easy to break, so we turn them into a test. ApplicationModules builds a model of the modules from the package structure, and verify() fails if any module uses another module's internal types or if modules depend on each other in a cycle.

Java
class ModularityTests {
ApplicationModules modules = ApplicationModules.of(ShopApplication.class);
@Test
void verifiesModularStructure() {
modules.verify();
// fails with, for example:
// Module 'billing' depends on non-exposed type com.acme.shop.orders.internal.OrderRepository
}
@Test
void writesDocumentation() {
new Documenter(modules).writeDocumentation(); // module diagrams and a canvas per module
}
}

The second test generates diagrams of the modules and their dependencies, so the documentation is produced from the code and cannot go out of date. Spring Modulith also supports @ApplicationModuleTest, which starts only one module and its dependencies, so module tests stay fast. Exact class names and options can differ between Spring Modulith versions, so check the version that matches your Spring Boot release.

3How Modules Communicate

Modules talk in two ways. For a question that needs an answer now, a module calls another module's public API, for example orders calling InventoryManagement.reserve(). For something that has happened and that other modules may react to, a module publishes an application event, such as OrderPlaced, and other modules listen.

Events are the better default between modules because they remove the dependency in one direction. Orders does not need to know that billing exists; billing listens to OrderPlaced. Adding a loyalty module later means adding a listener, not changing orders.

Java
// orders module
@Service
public class OrderManagement {
private final ApplicationEventPublisher events;
@Transactional
public OrderId placeOrder(PlaceOrder cmd) {
Order order = orders.save(Order.place(cmd));
events.publishEvent(new OrderPlaced(order.id(), order.customerId(), order.total()));
return order.id();
}
}
// billing module
@Component
class InvoiceCreator {
@ApplicationModuleListener // async, after commit, in its own transaction
void on(OrderPlaced event) {
invoices.createFor(event.orderId(), event.customerId(), event.total());
}
}
Step throughPlacing an order across modules
OrdersInventory APIEvent registryBilling
reserve(sku, qty): synchronous call to the public API
reserved
save order, publish OrderPlaced
store event with the order transaction
after commit: deliver OrderPlaced
create invoice in its own transaction
mark event completed

7 messages in order. Play them, or step through them one at a time.

Inventory is asked a question; billing is told a fact.

@ApplicationModuleListener runs the listener after the publishing transaction commits, asynchronously and in a new transaction, so a failure in billing does not roll back the order. With Spring Modulith's event publication registry enabled, published events are stored in a table and retried if a listener fails, which gives at-least-once delivery inside the monolith.

4Each Module Owns Its Tables

Code boundaries are only half of a module. If billing can join orders' tables in SQL, the modules are still coupled through the database: a column rename in orders breaks billing's query, and neither team sees it coming. So each module owns its tables, by naming convention such as orders_order and billing_invoice or, better, by a separate database schema per module, and no module reads another's tables or shares an entity class.

When billing needs order data, it gets it from the event payload or through the orders API, and if it needs to query it often, it keeps its own copy of the few fields it needs. That may feel like duplication, but it is what lets each module change its schema independently.

Who owns what in the shop
ModuleOwns tablesOthers get data through
ordersorders.order_header, orders.order_lineOrderManagement API, OrderPlaced event
inventoryinventory.stock, inventory.reservationInventoryManagement API
billingbilling.invoiceInvoiceIssued event
customerscustomers.customerCustomerDirectory API returning CustomerSummary
No foreign keys or joins across schemas; references are plain ids.

5Stronger Isolation With Build Modules

Spring Modulith's verification runs as a test, so a violation fails the build, but code still compiles until the test runs, and someone could disable the test. When we want stronger isolation, for example with several teams in one repository, we can make each module its own Maven or Gradle module, with a separate API artifact. Then billing depends only on orders-api, and importing an orders internal class simply does not compile.

This costs more build configuration, so many teams start with packages plus Spring Modulith and move a module to its own build module when its team or its rate of change justifies it.

6Preparing for Extraction Without Planning on It

A module that communicates through a public API and events and owns its tables is already shaped like a service. If billing later needs to scale separately or be deployed on its own schedule, we can extract it: the event listeners become consumers of a message broker topic, the API calls become HTTP or gRPC calls, and its schema moves to its own database. Because no other module reached into its internals, the extraction changes the transport, not the design.

The important point is that extraction is an option, not a goal. Most modules can stay in the monolith for years, and the clean boundaries already deliver the ownership and change isolation that teams wanted.

7Follow-up questions

How is this different from just using packages carefully?

The difference is enforcement and data. Packages with conventions erode as soon as one deadline makes an import convenient. Spring Modulith or build modules turn the rules into failing tests or compile errors, and separate table ownership removes the hidden coupling through SQL that package rules cannot see.

What about transactions that span modules?

Within the monolith we could technically use one transaction, but we avoid it across module boundaries, because that design will not survive extraction and it couples the modules' failures. Instead we use events after commit with retries, and design each module's actions so they can be compensated or repeated safely.

What if two modules need the same entity, such as Customer?

Then each module models what it needs. Customers owns the full customer; orders keeps a customer id and perhaps a name snapshot; billing keeps a billing address. Sharing one Customer entity across modules would couple all of them to every change in it.

8Conclusion

In this answer, we designed a modular monolith for orders, inventory, billing and customers. Each module is a top-level package with a small public API and hidden internal subpackages, verified by Spring Modulith's ApplicationModules.verify() and documented from the same model. Modules communicate through public interfaces for questions and application events for facts, each module owns its tables with no cross-module joins or shared entities, build modules add compile-time enforcement when needed, and the result can be extracted into services later if there is a reason.

What to say in the interviewAbout 90 seconds aloud

Open with

I would make each business capability a top-level package under the application package: orders, inventory, billing and customers. In each one, the root package holds the public API, for example an OrderManagement service, a few result records and the events the module publishes, and everything else, entities, repositories, controllers, sits in an internal subpackage.

Then, if they want more

Spring Modulith treats those packages as modules, and I would add a test that calls ApplicationModules.of(ShopApplication.class).verify(), which fails if one module uses another module's internal types or if there is a cycle, and a Documenter test that generates the module diagrams.

For communication, modules call each other's public APIs when they need an answer, like orders reserving stock, and publish application events for things that happened, like OrderPlaced, which billing handles with @ApplicationModuleListener after the order commits, with the event publication registry for retries.

Data is split too: each module owns its tables, ideally in its own schema, with no cross-module joins or shared entity classes; others get data through the API or events. If we need stronger isolation, I would move a module into its own Maven or Gradle module with a separate API artifact so illegal imports do not compile.

And because modules already talk through APIs and events and own their data, extracting one into a service later is mostly a transport change.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design a modular monolith in Spring Boot with enforced module boundaries for orders...": https://scrivio-interview-library.netlify.app/java/application-architecture/50a41f337e/