How modular can your monolith go? Part 1 - the basics

architecting   modular monolith  

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

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 application
cd application
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 web, domain and persistence logic. 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 CustomerRepository, and OrderRepository. Similarly, the domain module would contain classes such as Customer, and Order. Finally, the web module would contain classes such as CustomerController, and OrderController. 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 application
cd application
mkdir main customers orders notifications
cd customers
mkdir web domain persistence
...

The 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. For example, 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 in the orders, reserve credit in the customers domain (which can fail) and send an order confirmation using the notifications domain.

In the next article, I’ll describe the issues that you need to consider and explore the various options.

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 www.prc.education

www.prc.education is brought to you by Chris Richardson. Experienced software architect, author of POJOs in Action, the creator of the original CloudFoundry.com, and the author of Microservices patterns.

ASK CHRIS

?

Got a question about microservices?

Fill in this form. If I can, I'll write a blog post that answers your question.

NEED HELP?

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 NPXJKULI to sign up for $95 (valid until December 25th, 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 Eventuate.io 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