From Monolith to Microservices: How Spring Modulith Libraries Simplify Modular Design
Spring Modulith offers a set of libraries to help applications become more organized in a modular application, with decoupled modules that interact based on events.
This architecture brings the benefits of low coupling and high cohesion, by having many independent modules. To take advantage of this architecture, it is important to have a clear separation of concerns, with well-defined domains, and use event sources to guarantee the communication between the modules.
The Modulith can also be used to apply the Aggregator pattern, this is very useful when we have several domains, but the application’s core is to aggregate different services, for example in a Backend for frontend (BFF). You can find an example of this application here: How to guarantee your Architecture integrity with Spring Modulith
Modulith Libraries
Spring Modulith provides some libraries to test the modules’ isolation and generate the documentation.
In this article, I will focus on how the modules can communicate with each other using events, and how it works.
Modulith Events
The Modulith provides many libraries to integrate with the events.
The events are stored in a database, in our case we are going to use a relational database.
org.springframework.modulith
spring-modulith-starter-jpa
This library creates a table EVENT_PUBLICATION
that is responsible for running the events in an asynchronous mode.
This library also includes a dependency for the events, that allows us to annotate a method to listen to an event.
@ApplicationModuleListener
void on(ProductCreatedEvent event) {
var productId = event.productId();
stockRepository.save(new StockEntity(productId, 1L));
}
This annotation forces the application to listen to the ProductCreatedEvent
and execute it in a new transaction in asynchronous mode.
The events can also be integrated with external applications. It can help external systems to take advantage of the event produced by an internal module. If you would like to know more about it, have a look at the documentation: Externalizing Events.
Modulith Moments
The Spring Modulith Moments is a Passage of Time Events implementation heavily inspired by Matthias Verraes blog post.
The main idea is to provide a way to configure the Schedule based on “Moments”. These are some moments available: HourHasPassed
, DayHasPassed
, WeekHasPassed
, MonthHasPassed
, QuarterHasPassed
, YearHasPassed
After these moments the event will trigger and be processed. These moments replace the cron jobs and scheduled commands with an agnostic event indicating the passage of time.
It can be added by using this library
org.springframework.modulith
spring-modulith-moments
Conclusion
Spring Modulith provides several features to create a decoupled and more domain-oriented application, where each domain has its own logic and interacts with the other modules through events.
However, life is not a bed of roses, these events require an extra effort to manage, for that, spring Modulith also provides a good integration with observability systems such as Zipkin.
What is your opinion about this new architectural pattern? Do you like it? Have you already used it?
All the code is available here: GitHub — spring-modulith-sample.