Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Describe a modular monolith with Spring Boot: how you would enforce module boundaries and when you would split a module into its own service.

A modular monolith is one deployable Spring Boot application divided into modules by business capability, each with a small public API and internal packages that other modules cannot use. We enforce the boundaries with Spring Modulith's verification or ArchUnit tests, modules talk through their APIs or in-process events and each owns its own tables. We split a module into its own service only when it needs independent scaling, its own release cadence or technology, or a separate owning team, and the clean boundaries make that extraction a much smaller project.

Why this question is asked

The modular monolith has become the default recommendation for many new systems, but the term is often used loosely: a monolith with some packages is not modular unless something stops modules from reaching into each other. Interviewers ask this design question to see whether we can describe the structure concretely, enforce it with tools rather than good intentions, and state clear criteria for when a module should leave. In this answer, we will lay out the structure, see how modules communicate and own data, enforce the boundaries with Spring Modulith and ArchUnit, and then decide when and how to split a module out.

1The Structure: Modules by Business Capability

In a modular monolith, the top-level packages under the application's main package are business capabilities, not technical layers. Instead of controller, service and repository packages that every feature spreads across, we have catalog, orders, payments and shipping, and each of them contains its own controllers, services and repositories.

Each module exposes a small public API, the types other modules may use, and hides everything else in internal sub-packages. In Spring Modulith's default convention, types in a module's base package are its API, and sub-packages such as orders.internal are private to it. A reader can open the orders package and see at a glance what the rest of the application is allowed to use.

Text
com.shop
ShopApplication.java
catalog/ // module
CatalogApi.java // public API
internal/ProductRepository.java
orders/ // module
OrderService.java // public API
OrderPlaced.java // public event
internal/OrderEntity.java // hidden from other modules
payments/ // module
PaymentApi.java
internal/...
Step throughOne deployable, modules inside
  1. Spring Boot applicationone build, one deployment, one process
  2. Modules: catalog, orders, payments, shippingeach a business capability with a public API
  3. Internal packages per moduleentities, repositories, helpers, hidden from others
  4. Tables per moduleorders_* tables only touched by orders
  5. One database serverseparate schemas possible, one transaction available

5 layers, from the top down. Step through them one at a time.

2How Modules Communicate and Own Data

Modules call each other only through public APIs, or better, communicate through in-process events. When an order is placed, orders publishes an OrderPlaced event; shipping and payments listen and react. Orders does not need to know who listens, which keeps the dependency pointing one way and mirrors how separate services would talk.

Each module also owns its tables. The orders module's repositories may touch orders tables; no other module may query them directly, even though they sit in the same database. If shipping needs the delivery address, it receives it in the event or calls the orders API. Spring Modulith can store published events in an event publication registry, so that an event whose listener fails is not lost and can be resubmitted.

Java
// orders module
@Transactional
public void place(Order order) {
repository.save(order);
events.publishEvent(new OrderPlaced(order.id(), order.address()));
}
// shipping module, runs after the orders transaction commits
@ApplicationModuleListener
void on(OrderPlaced e) {
shipments.prepare(e.orderId(), e.address()); // its own tables only
}

@ApplicationModuleListener combines an asynchronous, transactional event listener that runs after the publishing transaction commits, in its own transaction. That gives us roughly the behaviour we would have with a message broker between services, but without the broker.

3Enforcing Boundaries With Tests

Boundaries that are not checked erode within months, usually under deadline pressure: someone imports orders.internal.OrderEntity into shipping because it is quicker. So we make the build fail when that happens. Spring Modulith's verify() checks that no module uses another's internal types and that there are no cycles between modules.

Java
class ModularityTests {
@Test
void verifiesModuleStructure() {
ApplicationModules.of(ShopApplication.class).verify();
// fails: Module 'shipping' depends on non-exposed type orders.internal.OrderEntity
}
}
// ArchUnit alternative, for projects without Modulith
@ArchTest
static final ArchRule noInternalAccess = noClasses()
.that().resideOutsideOfPackage("..orders..")
.should().dependOnClassesThat().resideInAPackage("..orders.internal..");

ArchUnit is a general library for testing architecture rules in Java, useful when we want custom rules or are not using Spring Modulith. Either way, the rule lives in the code and runs on every build, so it does not depend on reviewers noticing. Table ownership can be enforced similarly, with a schema per module and database users or a test that scans repository queries.

4When to Split a Module Into a Service

Most modules should stay in the monolith, because a method call is cheaper and more reliable than a network call. We split one out when it has a need the shared deployable cannot meet. Independent scaling: a search or image module needs far more CPU or memory than the rest, and scaling the whole application for it is wasteful. Release cadence: a module must be deployed many times a day while the rest is released weekly, or the reverse because of regulation. Technology: a module needs a different runtime or database. Team ownership: a separate team now owns the module and coordinating releases with others slows them down.

Size alone is not a reason, and neither is a wish to use microservices. If none of those needs applies, the module stays.

5How Clean Boundaries Make Extraction Smaller

Because the module already has a public API, owns its tables and communicates through events, extraction is mostly mechanical. We create a new application from the module's code, move its tables or schema to its own database, replace in-process event publishing with a broker such as Kafka (Spring Modulith can externalize selected events to a broker for exactly this), and replace direct API calls with HTTP clients behind the same interface.

Compare that with a tangled monolith, where extraction starts with months of discovering hidden dependencies and shared tables. The modular structure pays for itself the first time we extract something, and in the meantime it makes the monolith easier to understand and test, since each module can be tested on its own with @ApplicationModuleTest.

Step throughExtracting a module that has a real reason to leave
  1. Confirm the reasonscaling, cadence, technology or team
  2. Externalize its eventspublish to Kafka alongside in-process
  3. Move its schema to its own databasereplicate, verify, switch
  4. Run the module as its own appsame API over HTTP, flag to route calls
  5. Remove the module from the monolithafter a stable period

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Can modules in a modular monolith share a transaction?

Technically yes, because they share one database, but it is better to avoid transactions that span modules, since they would become impossible after an extraction. Keeping each transaction inside one module and using events after commit keeps the design honest, while still allowing the occasional pragmatic exception that is documented.

How is this different from a layered monolith?

A layered monolith splits code by technical role, so every feature touches every layer and every package depends on many others. A modular monolith splits by business capability, so most changes stay inside one module and dependencies between modules are few and checked.

How do you test one module in isolation?

Spring Modulith's @ApplicationModuleTest starts only the module under test and, depending on the mode, its dependencies, so we can test orders without bootstrapping payments or shipping. Its Scenario API also helps assert that a module publishes the expected events.

7Conclusion

In this answer, we described a modular monolith as one Spring Boot deployable divided into business-capability modules, each with a public API, internal packages and its own tables. Modules communicate through APIs or in-process events, the boundaries are enforced by Spring Modulith verification or ArchUnit tests, and a module is split into a service only when it needs independent scaling, release cadence, technology or team ownership, which the clean boundaries make a much smaller project.

What to say in the interviewAbout 80 seconds aloud

Open with

A modular monolith for me is one Spring Boot application, one deployment, but organised into modules by business capability, like catalog, orders and payments, rather than by technical layers.

Then, if they want more

Each module has a small public API in its base package, and everything else sits in internal packages that other modules may not use. Modules talk through those APIs or, preferably, through in-process application events, so orders publishes OrderPlaced and shipping reacts in its own transaction after commit, which mirrors how services would communicate.

Each module also owns its tables, and no other module queries them. I would enforce all this in the build: Spring Modulith's ApplicationModules verify test fails if a module uses another module's internal types or if there are cycles, and ArchUnit rules can do the same in projects without Modulith. Without automated checks, boundaries erode under deadline pressure.

I would split a module into a service only for a concrete reason: it needs independent scaling, a different release cadence, a different technology, or a separate team owns it and coordination slows them.

Because the module already has an API, its own data and event-based communication, extraction becomes mostly mechanical: externalize the events to a broker, move the schema, and expose the same API over HTTP.

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 documentation below. 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 "Describe a modular monolith with Spring Boot: how you would enforce module boundaries...": https://scrivio-interview-library.netlify.app/spring-boot/microservice-design/adc7805c67/