Video in progress

Staff 🌱 Spring

How do you split a large Spring Boot monolith into modules without breaking it into microservices?

I build a modular monolith: the code is split into modules by business area, each module has a public API and a private internals part, and modules talk to each other through an explicit interface or events. I enforce the boundaries with Spring Modulith or ArchUnit tests.

📖 Answer

The structure looks roughly like this:

uz.shop
 ├── catalog      (public: CatalogApi, ProductDto)
 │    └── internal (private: repository, entity, services)
 ├── ordering
 ├── payment
 └── shared       (a minimal set of shared things)

The rules are simple: you can't touch another module's internal package, modules don't query each other's tables directly, and side effects go through events: ordering publishes OrderPlaced, and payment and notification listen.

Spring Modulith verifies this:

@Test
void modulesAreRespected() {
    ApplicationModules.of(ShopApplication.class).verify();
}

On top of that it gives you per-module integration tests (@ApplicationModuleTest brings up just one module), an event publication registry (events are written to the database and re-sent if a listener fails, a kind of built-in outbox) and automatic documentation.

Why this is often the right step before microservices: one deploy, one transaction, no network failures. If a boundary was drawn wrong, fixing it is cheap, because it's a refactoring, not a migration. Later, if you need to pull a module out into a service, it's already separated with a clear API and events.

The main difficulty isn't technical, it's discipline. Breaking a boundary is easy, and the temptation of "let's do it quick this time" is always there. That's why the check should be mandatory in CI.