We are designing a modernization plan for a modular monolith. The goal is to introduce clear package boundaries, define a shared kernel with rules, and set up dependency linting to enforce the architecture.
Steps:
Define the Current State: Understand the existing codebase structure, dependencies, and identify modules (or potential modules).
Establish Module Boundaries:
Group related functionality into modules (packages). Each module should have a clear responsibility.
Define public APIs (interfaces, classes) for each module that other modules can use.
Hide internal implementation details (make internal classes package-private or use language-specific visibility).
Shared Kernel:
Identify common code that is used by multiple modules (e.g., DTOs, utility classes, domain exceptions).
Create a shared kernel (a separate module or package) that contains these shared elements.
Rules for the shared kernel:
a. It should be stable and only change for very good reasons.
b. It should have minimal dependencies (ideally none on the application modules).
c. It should be small and focused on truly shared concepts.
Dependency Rules:
Define the allowed dependencies between modules. Typically, we want a layered or hexagonal architecture where:
Outer layers (e.g., UI, infrastructure) can depend on inner layers (e.g., domain, application) but not vice versa.
Modules in the same layer can depend on each other only if they are in the same bounded context or through well-defined interfaces.
Use dependency inversion: high-level modules should not depend on low-level modules; both should depend on abstractions.
Dependency Linting:
Choose a tool for dependency analysis (e.g., for Java: ArchUnit, JDepend; for C#: NDepend; for JavaScript/TypeScript: dependency-cruiser, madge).
Define rules that enforce:
a. No cyclic dependencies.
b. Dependencies only in the allowed direction (e.g., from outer to inner layers).
c. Modules cannot depend on the internal packages of other modules (only on their public APIs).
d. The shared kernel should be allowed to be used by any module, but no module should depend on the shared kernel's internal details (if any).
Gradual Enforcement:
Start by setting up the linting tool and running it on the current codebase to see existing violations.
Prioritize fixing critical violations (like cycles) and then move to the dependency direction rules.
Refactor code to move classes to appropriate packages/modules, create interfaces, and invert dependencies as needed.
Continuous Enforcement:
Integrate the linting into the build process (e.g., as a Maven/Gradle plugin, or in a CI pipeline) so that violations break the build.
Educate the team on the architecture rules and the importance of maintaining them.
Documentation and Communication:
Document the module boundaries, the shared kernel, and the dependency rules.
Create diagrams (like module dependency graphs) to visualize the architecture.
Iterate:
As the application evolves, continuously enforce the rules and adjust the module boundaries if necessary (but with caution and proper refactoring).
Example Plan for a Java Application:
Let's assume we have a monolithic application with packages:
com.example.app
- controller
- service
- repository
- model
- util
Step 1: Identify modules.
We might have:
- User Management (controllers, services, repositories, models for user)
- Order Management (controllers, services, repositories, models for order)
- Shared Kernel (common DTOs, exceptions, utilities)
Step 2: Restructure packages:
com.example.app
- user
- api (public interfaces for user module)
- internal (implementation, not visible to other modules)
- order
- api
- internal
- sharedkernel
- dto
- exception
- util
But note: In Java, we can use the module system (Java 9+) or just package visibility and dependency rules.
Step 3: Define rules for ArchUnit (if using Java):
We can write ArchUnit tests:
Example rule: No classes in the internal package of a module should be accessed by classes in other modules.
We can define:
code
@Test
public void no_internal_package_access() {
classes().that().resideInAnyPackage("..user.internal..", "..order.internal..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("..user..", "..order..");
}
But we want to restrict even more: only the same module's internal classes can access each other.
Alternatively, we can define that classes in the internal package of a module should only be accessed by classes in the same module's api package? Not exactly.
Actually, we want:
code
- The internal package of a module is only accessible by the same module (i.e., the module's own classes, including api and internal).
- Other modules can only access the api package.
But note: the sharedkernel is a module itself, so we might not want to restrict the sharedkernel's own internal? We can structure the sharedkernel as:
code
sharedkernel
- api (if needed, but usually sharedkernel is just a library of shared code, so all public)
- internal (if we have something we don't want others to use, but then we shouldn't put it in sharedkernel)
Typically, sharedkernel is meant to be used by others, so everything in sharedkernel is public and intended for use.
And also, we want to enforce the dependency direction between modules:
code
User module might depend on sharedkernel, but not vice versa.
Also, we might have:
code
Order module depends on sharedkernel and maybe on user module? Only if there's a dependency (e.g., order needs user data). But then we have to decide:
- If order needs user data, should it call the user module's api? Then we have a dependency from order to user.
- Alternatively, we could have the user module publish domain events that the order module listens to? Then we break the direct dependency.
But for simplicity, let's assume we allow order to depend on user's api.
So the allowed dependencies:
code
- Any module can depend on sharedkernel.
- Inner modules (like user, order) can depend on each other only if they are in the same layer? We might not have layers.
Alternatively, we can define that:
code
user and order are both in the same layer (domain) and can depend on each other? But that leads to cycles.
We want to avoid cycles. So we need to decide on a direction:
code
user -> order? or order -> user? or no direct dependency?
A common pattern is to have a dependency from the module that is more stable (less likely to change) to the one that is less stable.
But in our case, we might have:
code
user module (core domain) and order module (another core domain). They might be independent.
So we might not allow direct dependencies between user and order. Instead, they communicate via events or through a sharedkernel (but sharedkernel should not have domain logic).
So:
code
Rule: No module (user, order) should depend on another module (user, order). They can only depend on sharedkernel.
Then how do they interact? Via events or through an application service that orchestrates? That's a design decision.
Alternatively, we can have an application layer that uses both user and order modules? Then:
code
application
- user (uses user module)
- order (uses order module)
- both? Then application might have a service that uses both.
Then we have:
application -> user
application -> order
And user and order do not depend on each other.
So we might restructure:
code
com.example.app
- sharedkernel
- user (domain module)
- order (domain module)
- application (orchestration, use cases)
Then the dependency:
code
application -> user
application -> order
user -> sharedkernel
order -> sharedkernel
And no other dependencies.
Now, we can define rules:
code
Rule 1: No cycles.
Rule 2: Only the application module can depend on user and order.
Rule 3: user and order can only depend on sharedkernel (and standard library).
Rule 4: sharedkernel depends on nothing (or only standard library).
But note: the user module might have an api that the application uses. So the application depends on the user.api.
We can structure:
code
user
- api (public interfaces and DTOs that the application uses)
- internal (domain logic, repositories, etc.)
Then the application depends on user.api, not on user.internal.
So:
code
classes().that().resideInAnyPackage("..user.internal..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("..user..")
classes().that().resideInAnyPackage("..user.api..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("..application..", "..user..")
Similarly for order.
And for sharedkernel, all public classes are intended for everyone, so we don't restrict who accesses them? But we do want to restrict that sharedkernel doesn't depend on user, order, or application.
And for the api packages of user and order, we might allow them to be accessed by each other? Probably not, because then we have a dependency between user and order. But if we allow the application to use both, then we don't need user and order to depend on each other.
So we might also want:
code
classes().that().resideInAnyPackage("..user.api..")
.should().notDependOnClassesThat().resideInAnyPackage("..order..")
and vice versa.
But wait, what if the user.api has a DTO that is used by order.api? Then order.api depends on user.api? Then we have:
code
order.api -> user.api
Then the application that uses order.api will also get user.api transitively? That's acceptable.
But then we have a dependency from order to user (via api). We have to decide if that's allowed.
If we want to avoid any dependency between user and order, then we must not have user.api types in order.api.
So we have to design the sharedkernel to hold common DTOs? But then if a user DTO is used by order, it becomes shared? That might not be appropriate because it's a user DTO.
Alternatively, we can design the application to have its own DTOs that are composed from user and order DTOs? Then the application layer has its own DTOs and the user and order modules don't depend on each other.
So:
code
application
- dto (application-specific DTOs)
- service (application services that use user.api and order.api)
Then:
user.api -> (only user internal and sharedkernel)
order.api -> (only order internal and sharedkernel)
application -> user.api, order.api
And the application DTOs are built from the user and order DTOs? Or the application service calls user.api and order.api and then maps to application DTOs.
This is getting complex.
Let's simplify:
code
We have three modules: user, order, sharedkernel.
We allow:
user -> sharedkernel
order -> sharedkernel
And we do not allow:
user -> order
order -> user
And we do not allow:
user -> application
order -> application
But we allow:
application -> user.api
application -> order.api
And we do not allow application to depend on user.internal or order.internal.
And we do not allow user.api to depend on order (either api or internal) and vice versa.
So the rules:
code
1. Classes in user.internal and order.internal are only accessed by classes in the same module (user or order respectively).
2. Classes in user.api can be accessed by:
- classes in user (including internal)
- classes in application
But not by classes in order (neither api nor internal).
3. Similarly for order.api.
4. Classes in sharedkernel can be accessed by anyone (user, order, application) but sharedkernel does not depend on user, order, or application.
5. No cycles.
Rule 2: Only application can access the api packages (and the same module's internal can access its own api? Actually, the internal might use the api? Typically, the internal implements the api, so internal should be allowed to access the api of the same module? But the api is for external use, so internal might not need to access its own api. Actually, the internal package contains the implementation of the api interfaces, so it will implement the interfaces, but not necessarily access other classes in the api package? It might if there are helper classes in the api package? We should avoid that.
Alternatively, we can say:
code
The api package should only contain interfaces and DTOs that are used by external modules.
The internal package implements the interfaces and may have its own classes.
So the internal package should not depend on the api package of the same module? Actually, it must depend on the interfaces in the api package. So we need to allow the internal package to access the api package of the same module.
So we adjust:
code
Rule 1: internal packages can only be accessed by:
- same module's internal (obviously)
- same module's api? Actually, the api is accessed by the internal? That's the implementation. So we allow:
classes().that().resideInAnyPackage("com.example.app.user.internal..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("com.example.app.user..")
This allows internal to be accessed by user.api and user.internal? But we don't want user.api to access user.internal. We want the dependency to be from internal to api? Actually, the internal package implements the api, so it depends on the api (the interfaces). So we have:
user.internal -> user.api
But we don't want:
user.api -> user.internal
So we need two rules:
a. No class in user.api should access user.internal.
classes().that().resideInAnyPackage("com.example.app.user.api..")
.should().notDependOnClassesThat().resideInAnyPackage("com.example.app.user.internal..")
b. The only classes that can access user.internal are in user.internal (and maybe the same module's api? Actually, the api should not access internal, but the internal must access the api? That's a dependency from internal to api, which is allowed because they are in the same module.
But note: the rule above (Rule 1) says that internal can be accessed by any class in the user module (including api). That's too permissive because we don't want api to access internal.
So we change Rule 1 to:
classes().that().resideInAnyPackage("com.example.app.user.internal..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("com.example.app.user.internal..")
But then the internal package cannot access its own api? That's not what we want.
Actually, we want:
user.internal -> user.api (allowed)
user.api -> user.internal (forbidden)
And we also want:
user.api -> sharedkernel (allowed)
user.internal -> sharedkernel (allowed)
And:
user.api -> order.api (forbidden)
user.internal -> order.api (forbidden)
So we need a rule that says:
user.api can only depend on:
- sharedkernel
- user.api (itself)
- maybe standard library
user.internal can only depend on:
- user.api
- sharedkernel
- user.internal (itself)
And similarly for order.
And application can only depend on:
- user.api
- order.api
- sharedkernel
- application's own packages
And sharedkernel can only depend on standard library (and maybe other sharedkernel packages).
This is getting very specific and might be too restrictive. We might need to adjust.
Alternatively, we can define the allowed dependencies by layer (if we have layers) or by module.
Let's define the modules and their allowed dependencies:
code
Module: sharedkernel
Dependencies: none (except standard library)
Module: user
Dependencies: sharedkernel
Exported packages: user.api (meaning other modules can depend on user.api)
Internal packages: user.internal (only used within user)
Module: order
Dependencies: sharedkernel
Exported packages: order.api
Internal packages: order.internal
Module: application
Dependencies: user.api, order.api, sharedkernel
Then the rules:
code
And we also need to rule out cycles. ArchUnit has noCycles().
So the plan:
code
1. Set up the package structure as above.
2. Write ArchUnit tests for the above rules.
3. Run the tests and fix violations by moving code to appropriate packages, creating interfaces, and inverting dependencies.
4. Integrate ArchUnit into the build (e.g., as a Maven plugin or in a test phase).
However, note that the above rules are very strict and might require significant refactoring. We might need to adjust as we go.
Alternative: Start with simpler rules and then tighten.
Step-by-step:
code
Step 1: Identify existing packages that are potential modules (user, order, etc.) and the shared kernel.
Step 2: Create the new package structure (without moving code yet) and set up the linting rules for the new structure.
Step 3: Run the linting on the existing code (which is in the old packages). This will show many violations.
Step 4: Gradually move code to the new packages, starting with the shared kernel (extract common code to sharedkernel) and then the modules.
Step 5: As we move code, we might break existing dependencies. We fix by:
a. If a class in module A uses a class in module B's internal package, we should move that class to module B's api if it's needed by others, or refactor to use an interface (dependency inversion).
b. If there's a cycle, we break it by introducing an interface in a third module (like sharedkernel) or by moving code.
Step 6: Continue until the linting passes.
This is a long-term plan and requires team buy-in.
Documentation:
code
- Create an architecture decision record (ADR) for each module, describing its responsibility and allowed dependencies.
Tools:
code
- For Java: ArchUnit (with JUnit tests) and possibly a build plugin.
- For other languages, choose equivalent tools.
Example ArchUnit test for no cycles:
code
@Test
public void noCycles() {
classes().that(). resideInAnyPackage("com.example.app..")
.should().beFreeOfCycles();
}
But note: we might allow cycles within the same module? The rule above allows cycles within the same package? Actually, beFreeOfCycles() doesn't allow any cycle, even within the same module. But we might want to allow cycles within the same module? Probably not, but sometimes it's acceptable. However, we want to avoid cycles between modules. We can use:
code
ArchRule rule = classes()
.that().resideInAnyPackage("com.example.app..")
.should().beFreeOfCycles()
.ignoreDependency(AnyClassInPackageThat.is("com.example.app.user.internal.."), AnyClassInPackageThat.is("com.example.app.user.api.."))
... // but this is getting complex.
Alternatively, we can check for cycles only between different modules. But that's more complex.
Maybe we start by disallowing any cycle and then if we find a cycle within a module that we cannot break, we might allow it? But that's a smell.
Given the complexity, we might start with:
code
- No cycles at all (across the entire codebase).
Then if we have a cycle that is within a module and we cannot break it, we might reconsider the module boundaries? Or allow that cycle? But then we are not strictly modular.
It's a trade-off.
Let's keep it simple: no cycles anywhere.
Summary:
code
We'll have:
- Shared kernel: a package with common code, no dependencies on application modules.
- Modules: each module has an api and internal package.
- Application module (or multiple application modules) that orchestrate the domain modules.
Rules:
1. No cycles.
2. Internal packages can only be accessed by the same module's internal package (and maybe the same module's api? We decided no, because api should not depend on internal).
3. Api packages can be accessed by:
- the same module's internal (because internal implements the api)
- the application module (if the application uses the api)
- but not by other modules' api or internal.
4. Application module can only depend on the api packages of the domain modules and the shared kernel.
5. Domain modules (user, order) can only depend on the shared kernel and their own api/internal.
This is a strict layered architecture with a shared kernel.
Implementation:
code
We'll create a test class for each rule.
However, note that the above rules are very strict and might require a lot of refactoring. We might need to adjust based on the existing code.
Let's adjust:
code
Instead of having a separate application module, we might have multiple applications (e.g., a web application and a batch application). Then we might have:
com.example.app
- sharedkernel
- user
- api
- internal
- order
- api
- internal
- web
- user (uses user.api)
- order (uses order.api)
- batch
- user (uses user.api)
- order (uses order.api)
Then the web and batch are both applications that use the domain modules.
Then the rules for web and batch are the same as application above.
Alternatively, we can have a single application module that contains all the entry points (like Spring Boot main class, controllers, etc.) and that uses the domain modules.
It depends on the project.
Given the complexity, we might start with a simpler plan:
code
Step 1: Extract shared kernel.
Step 2: Define module boundaries (api and internal) for each module.
Step 3: Move code to the appropriate packages.
Step 4: Enforce that internal packages are only used within the module.
Step 5: Enforce that api packages are only used by the application and the same module's internal.
Step 6: Enforce that the application only uses the api packages.
This is a plan. It will take time and effort.
Let me know if you have any questions.
I'll architect a comprehensive modular monolith modernization plan that introduces clear architectural boundaries while keeping the deployment as a single unit. This approach gives you the benefits of modularity without the complexity of distributed systems.
@AnalyzeClasses(packages = "com.example")
public class ArchitectureTest {
@ArchTest
static final ArchRule MODULE_BOUNDARIES =
classes()
.that().resideInAPackage("..modules.user..")
.should().onlyDependOnClassesThat(
resideInAnyPackage(
"..sharedkernel..",
"..modules.user..",
"java..",
"javax.."
)
);
@ArchTest
static final ArchRule NO_INTERNAL_LEAKAGE =
classes()
.that().resideInAPackage("..modules..")
.should().notDependOnClassesThat(
resideInAPackage("..modules..internal..")
);
}
3.2 Gradual Refactoring Strategy
Sprint 1: Shared Kernel Extraction
Identify 10-15 most-used classes across modules
Move to sharedkernel/ with deprecation cycle
Update all imports (automated with IDE/refactoring tool)
Lint rule: NoModuleDependOnSharedKernelInternal
Sprint 2-N: Module Isolation
For each module:
Create module folder structure
Move code respecting:
Domain logic → domain/
Infrastructure → infrastructure/
Interface adapters → presentation/ or application/
Define module's public API (only application/ package exported)
Add module descriptor module.yaml:
yaml
name: "user-management"
version: "1.0.0"
exports: ["application.*"] # Only these packages are public
dependencies:
- "sharedkernel"
- "order-processing" # Only if explicitly needed
Sprint Final: Composition Root
java
// bootstrap/Application.java
public class Application {
public static void main(String[] args) {
ModuleRegistry registry = new ModuleRegistry();
// Wire modules through shared kernel contracts
registry.register(new UserManagementModule(
repositoryProvider -> new UserRepository(repositoryProvider)
));
registry.register(new OrderProcessingModule(
eventPublisher -> new DomainEventPublisher(eventPublisher)
));
ApplicationRunner.run(registry);
}
}
✅ Business-aligned: Module boundaries match business capabilities
The key is starting small with 2-3 modules, enforcing rules strictly, and using automation to reduce friction. Within 3-4 months, you'll have a clean, maintainable monolith that can be easily split into services when needed.
1. No class in a module's internal package (user.internal, order.internal) should be accessed by any class outside that module.
2. The only classes that can access the api packages (user.api, order.api) are:
- classes in the same module (user or order) [because the internal might use the api? Actually, the internal uses the api? No, the internal implements the api, so it depends on the api. So the internal package can access the api package of the same module? Yes, but the api package should not access the internal. So we have a one-way dependency: internal -> api, but not api -> internal.
But wait: if the api has an interface, and the internal has an implementation, then the internal package has a class that implements the interface. That's a dependency from internal to api. So we must allow internal to access api.
However, we don't want any other module (like application or order) to access user.internal. So:
Rule for user.api:
can be accessed by:
- user.internal (because internal uses the interfaces)
- application (because application uses the user.api)
- but not by order (neither api nor internal)
Rule for user.internal:
can be accessed by:
- only user.internal (because we don't want anyone else to use internal classes)
But note: the application must not access user.internal. So we require:
classes().that().resideInAnyPackage("com.example.app.user.internal..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("com.example.app.user.internal..");
This means that even user.api cannot access user.internal? That's what we want.
But then how does user.internal use user.api? It does by having a class that implements an interface from user.api. That's a dependency from user.internal to user.api. But the rule above says that user.internal can only be accessed by user.internal. That doesn't restrict the dependencies of user.internal, it restricts who can access user.internal.
We have two different things:
- Who can access user.internal? (only user.internal)
- What can user.internal access? (user.api, sharedkernel, etc.)
So we need two sets of rules:
A. Access to internal packages: only same package/subpackage? Actually, we want to restrict by module.
B. Dependencies of each package: what packages can a given package depend on?
We can write:
Rule A:
classes().that().resideInAnyPackage("com.example.app.user.internal..")
.should().onlyBeAccessed().byClassesThat().resideInAnyPackage("com.example.app.user.internal..");
Rule B for user.api:
classes().that().resideInAnyPackage("com.example.app.user.api..")
.should().onlyDependOnClassesThat().resideInAnyPackage(
"com.example.app.user.api..",
"com.example.app.sharedkernel..",
"java..", "javax..") // standard library
Rule B for user.internal:
classes().that().resideInAnyPackage("com.example.app.user.internal..")
.should().onlyDependOnClassesThat().resideInAnyPackage(
"com.example.app.user.api..",
"com.example.app.user.internal..",
"com.example.app.sharedkernel..",
"java..", "javax..")
Similarly for order.
For application:
classes().that().resideInAnyPackage("com.example.app.application..")
.should().onlyDependOnClassesThat().resideInAnyPackage(
"com.example.app.user.api..",
"com.example.app.order.api..",
"com.example.app.sharedkernel..",
"com.example.app.application..",
"java..", "javax..")
For sharedkernel:
classes().that().resideInAnyPackage("com.example.app.sharedkernel..")
.should().onlyDependOnClassesThat().resideInAnyPackage(
"com.example.app.sharedkernel..",
"java..", "javax..")