How modular can your monolith go? Part 2 - a first look at how subdomains collaborate

architecting   modular monolith  

This article is the second in a series of articles about modular monoliths. The other articles are:

In part 1 I described how a modular monolith is structured around the business domains rather than the technical concerns. Some system operations (e.g. user requests) will be local to a single domain. For example, createCustomer() However, many system operations, especially the most complex ones and possibly the most important, will span multiple domains. For example, as I described the end of the first article, createCustomer() needs to create a Customer and send out an email confirmation, which requires the customers and notifications domains to collaborate. Similarly, the createOrder() operation needs to create an Order, reserve credit in the Customer Service (which can fail) and send an order confirmation.

Since domains need to collaborate, we must consider domain API design and inter-domain collaboration mechanisms. What’s more, we need to think about the scope of (database) transactions. In this article, I focus on the first two issues, and assume that a system operation consists of a single transaction that spans subdomains. In a later post, I’ll discuss transaction management in more detail.

The monolithic customers and orders example

To provide more context, let’s start by looking at the monolithic customers and orders application in more detail. This application implements the following system operations:

  • createCustomer(name, creditLimit) - creates a customer with a credit limit and sends out a welcome email
  • createOrder(customerID, orderTotal) - creates an Order for the specified customer. It reserves credit for the order and sends out an order confirmation email.
  • cancelOrder(orderID) - cancels the specified Order, which releases the reserved credit.

The following diagram shows the application’s package structure:

The Customer and Order entities are persisted using JPA. Their repositories are implemented using Spring Data JPA.

There are two top-level packages: web and domain. Each one is a separate Gradle sub-project.

Modularizing the monolith

Reorganizing the customers and orders application into a modular monolith results in a package structure that looks like this:

The top-level packages are customers, orders and notifications. Each one contains the classes - controllers, services, and entities - for the corresponding domain. Each top-level package is a separate Gradle sub-project.

About the inter-domain dependencies?

A notable feature of this design is that there are numerous inter-domain dependencies. The CustomerService in the customers domain and the Order Service in the orders domain both invoke the NotificationService in the notifications domain. The OrderService also invokes the CustomerRepository to retrieve the Customer entity.

Furthermore, the Order entity has an @ManyToOne relationship with the Customer entity. This reference associates the Order with the Customer. It’s also used by the Order entity to release credit when it’s cancelled:

class Order ...
  public void cancel() {
    this.state = OrderState.CANCELLED;

Behind the scenes, the ORM framework lazily loads the Customer, and when the transaction commits it executes SQL to update the underlying database tables. This is an instance of a very common pattern in a rich domain model. Not withstanding the endless and tedious debates about whether or not to use an ORM, this approach works well in many situations.

The dependency of the customers and orders domain on the NotificationService seems reasonable. But the dependency of the orders domain on the CustomerRepository and Customer entity are highly suspicious. Do these kinds of inter-domain reference make sense in modular monolith? And more generally, how should these three subdomains collaborate? As always, it depends. In the remainder of this article, I’ll discuss one option - fine-grained subdomain APIs - and in a later articles, I’ll discuss other options.

Design option: subdomains with fine-grained APIs

One option is to allow fine-grained inter-domain references including inter-entity references that span subdomains.

Subdomain API = entities, repositories, etc.

In this example, the customers domain’s API consists of the Customer entity and the CustomerRepository. In other words, it has a fine-grained API.

Let’s look at the benefits and drawbacks of this approach.

The benefits of a fine-grained API

This approach has some benefits:

  • It’s a familiar programming model that supports a rich domain model that has OO-style relationships between entities.
  • There’s no need for DTOs and the associated mapping code that other solutions typically require.

The drawbacks of a fine-grained API

Sadly, however, there are some significant drawbacks.

Lack of encapsulation

One key drawback is the lack of encapsulation and so there’s a risk of tight design-time coupling between the domains. For example, the Customer entity is part of the customers domain’s API. If the customers team wanted to replace the Customer entity with a Transaction Script, or, better yet, a quantum computing powered generative AI-based credit risk assessment algorithm, they’d have to coordinate with the orders team. Such design-time coupling is a problem because it reduces team autonomy and slows down development.

Lack of testability

Another drawback of a fine-grained API is that it makes it difficult to test the domains in isolation. Ideally, each domain should be testable in isolation using mocks for dependencies. It simplifies the tests since there’s no need to initialize the dependencies. Also, mocking often reduces the test execution time since the tests are simpler. However, in this example, the customers domain’s fine-grained API is rather difficult to mock, which prevents its clients from being tested in isolation. We could not, for example, test the orders domain without the customers domain, which complicates and slows down testing.

What about circular references?

Another issue is the existence of circular references between entities due to bidirectional associations. For example, the Customer entity could have a one-to-many relation with the Order entity, in addition to the @ManyToOne Order-Customer relationship. The problem with circular entity references in a modular monolith is that it creates a dependency cycle between the domains. Such cycles are an obstacle to modularity since the domains need to be packaged in the same Maven module or Gradle sub-project.


While allowing subdomains to expose their entities and repositories is a simple approach, it has some significant drawbacks. In the next article, I’ll discuss an alternative approach that addresses these drawbacks by applying the Facade pattern.

Need help with your architecture?

I’m available. I provide consulting and workshops.

architecting   modular monolith  

Copyright © 2024 Chris Richardson • All rights reserved • Supported by Kong.

About is brought to you by Chris Richardson. Experienced software architect, author of POJOs in Action, the creator of the original, and the author of Microservices patterns.

New workshop: Architecting for fast, sustainable flow

Enabling DevOps and Team Topologies thru architecture

DevOps and Team topologies are vital for delivering the fast flow of changes that modern businesses need.

But they are insufficient. You also need an application architecture that supports fast, sustainable flow.

Learn more and register for my June 2024 online workshops....


I help organizations improve agility and competitiveness through better software architecture.

Learn more about my consulting engagements, and training workshops.

LEARN about microservices

Chris offers numerous other resources for learning the microservice architecture.

Get the book: Microservices Patterns

Read Chris Richardson's book:

Example microservices applications

Want to see an example? Check out Chris Richardson's example applications. See code

Virtual bootcamp: Distributed data patterns in a microservice architecture

My virtual bootcamp, distributed data patterns in a microservice architecture, is now open for enrollment!

It covers the key distributed data management patterns including Saga, API Composition, and CQRS.

It consists of video lectures, code labs, and a weekly ask-me-anything video conference repeated in multiple timezones.

The regular price is $395/person but use coupon ILFJODYS to sign up for $95 (valid until April 12, 2024). There are deeper discounts for buying multiple seats.

Learn more

Learn how to create a service template and microservice chassis

Take a look at my Manning LiveProject that teaches you how to develop a service template and microservice chassis.

Signup for the newsletter

BUILD microservices

Ready to start using the microservice architecture?

Consulting services

Engage Chris to create a microservices adoption roadmap and help you define your microservice architecture,

The Eventuate platform

Use the platform to tackle distributed data management challenges in your microservices architecture.

Eventuate is Chris's latest startup. It makes it easy to use the Saga pattern to manage transactions and the CQRS pattern to implement queries.

Join the microservices google group