Open In App

What is Spring Modulith?

Last Updated : 17 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will embark on a journey to explore Spring Modulith and understand how it revolutionizes the way we design and develop modern software applications.

What is a modular monolith application?

A modular monolith application in Spring Boot is when a traditionally monolithic application (with all code in one project/module) is broken up into distinct modules while still maintaining a single codebase and deployment.

Monolith-APP

Spring Modulith

Benefits

  • Improved maintainability: Spring Modulith helps to improve the maintainability of Spring Boot applications by encouraging developers to structure their code in a modular way.
  • Improved observability: Spring Modulith provides support for observing the behavior of modules at runtime, which can be used to troubleshoot problems and to identify opportunities for optimization.
  • Increased testability: Spring Modulith makes it easier to test individual modules, which helps to improve the overall quality of the application.
  • Reduced coupling: Spring Modulith helps to reduce coupling between modules, which makes the application more flexible and easier to evolve.

Why is Spring Modulith important?

  • Improves code organization and maintainability of large monolithic applications. Splitting a monolith into logical modules makes the codebase easier to understand, develop and maintain.
  • Supports independent deployments. Modules can be deployed, upgraded or rolled back independently without affecting other modules.
  • Simplifies refactoring. Changes can be made to a module without impacting other code. Refactors are isolated to individual modules.
  • Utilizes the benefits of both monoliths and microservices. It addresses monolith drawbacks like lack of scalability and maintainability while avoiding the heavy overhead of full microservices.

Key Concepts of Spring Modulith

Application modules:

  • User module: Handles all user authentication, authorization, profile management, etc.
  • Product module: Manages products, inventory, catalogs, pricing, etc.
  • Content module: Creates, manages, and publishes website content like articles, blogs, pages, etc.
  • Shared module: Common utilities, models, and configurations shared across modules.

Package arrangement:

Example

Arrangement

Module encapsulation:

  • Module encapsulation in Spring Modulith is a way of restricting the visibility of types within a module. By default, only types in the public API of a module are visible to other modules. This means that types in internal packages are not visible to other modules, unless they are explicitly imported.

Module interaction:

  • Dependency injection: Modules can inject dependencies on each other’s types. This is the most common way for modules to interact with each other.
  • Event-based communication: Modules can publish and subscribe to events. This can be used to decouple modules and to send messages between modules.
  • API calls: Modules can call each other’s APIs. This is less common than dependency injection or event-based communication, but it can be used when the modules need to have a more direct interaction.

Get started with Spring Modulith, you can follow these steps:

  1. Add the Spring Modulith dependency to your project.
  2. Create a Java package arrangement that puts business modules as direct sub-packages of the application’s main package.
  3. Create an ApplicationModules model, run verifications, and create documentation snippets.
  4. Run integration tests for individual application modules.

Dependency

Maven:

<!-- https://mvnrepository.com/artifact/org.springframework.modulith/spring-modulith-bom -->
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>

Gradle:

// https://mavenlibs.com/maven/pom/org.springframework.experimental/spring-modulith-bom
implementation group: 'org.springframework.experimental', name: 'spring-modulith-bom', version: '0.6.0'

Case studies of organizations using Spring Modulith

  • Trivago – The hotel search platform broke its monolith into over 50 modules organized by domain. It improved velocity, scalability, and deployability.
  • UrbanClap – The home services marketplace in India structured its monolith into modules like user, vendor, booking, etc.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads