How modular can your monolith go? Part 1 - the basics
This article is the first in a series of articles about modular monoliths. The other articles are:
- Part 2 - a first look at how subdomains collaborate
- Part 3 - encapsulating a subdomain behind a facade
- Part 4 - physical design principles for faster builds
- Part 5 - decoupling domains with the Observer pattern
The first rule of migrating to microservices is don’t. Instead, you should make the most of your monolith. Even though a large monolith might not resolve the dark matter energy, especially multiple technology stacks, the monolithic architecture not an anti-pattern. A key question is, therefore, what’s the most effective way of structuring your monolith?
The traditional layered architecture
Over the years, I’ve started development of many monolithic application by doing the equivalent of:
mkdir web domain persistence
The monolith is structured as three technically oriented layers:
web- the user interface and/or API logic
domain- the business logic
persistence- the database access logic
This works well for small applications but as the number of developers grows it becomes increasingly problematic.
The trouble with technical layers
The trouble technically-oriented architecture is that it doesn’t mirror the structure of the application’s development organization. A large development organization should be structured as collection of small, cross-functional teams (ie. Team topology’s stream aligned teams), each of which is responsible for a particular business (sub)domain. For example, let’s imagine that the FTGO application consists of three teams/domains:
customers- manages customers
orders- manages orders created by customers
notifications- delivers notifications (e.g. email, SMS, etc) to customers
Each subdomain consists of
But since the monolith is structured around the technical layers, the code for each subdomain is scattered across the technical layers.
For example, the
persistence module would contain classes such as
domain module would contain classes such as
web module would contain classes such as
As a result, teams are forced to work across the technical layers, which can be a source of friction that reduces productivity.
Moreover, it’s likely that this way of structuring the monolith will result is a slow deployment pipeline, since a change to a class belonging to one subdomain, e.g
OrderRepository will require classes belonging to other subdomains to be tested as well.
The modular monolith
A better way to organize a monolith is to structure it around the business domains rather than the technical layers, the so-called modular monolith architectural style. For example, the development of the application described earlier would start by doing the equivalent of:
mkdir main customers orders notifications
mkdir web domain persistence
main module is the entry point to the application.
Improved team autonomy and faster builds
In a modular monolithic, the code based mirrors the domains. As a result, Team autonomy is improved since each team is working on a distinct part of the code base.
This architectural style can also accelerate the deployment pipeline since a change to a class belonging to one subdomain will (hopefully) only require classes belonging to the same subdomain to be tested.
But how to implement operations that span multiple domains?
One key question that I haven’t yet addressed is how to implement operations that span multiple domains.
createCustomer() needs to create a
Customer and send out an email confirmation, which requires the
notifications domains to collaborate.
createOrder() operation needs to create an
Order in the
orders, reserve credit in the
customers domain (which can fail) and send an order confirmation using the
In the next article, I’ll describe the issues that you need to consider and explore the various options.