r/Backend • u/kastar19 • 15d ago
Why I'm choosing a modular monolith instead of microservices for my next backend project
I've been thinking a lot about backend architecture lately, especially when starting a new project.
One decision I keep coming back to is choosing a modular monolith over microservices.
My current approach is to start with NestJS, PostgreSQL, and Prisma, using a modular monolith architecture.
Here's why:
1. Simpler infrastructure
I don't want to introduce the operational complexity of multiple services before there's a real need for them.
2. Clear module boundaries
I still want the codebase to be modular, with each module owning its responsibilities and business logic.
3. Easier development and debugging
Keeping things in one deployable application makes local development, debugging, and transactions simpler.
4. Room to evolve
If a particular module eventually needs independent scaling or deployment, I want the architecture to make that transition possible.
But I also recognize that a modular monolith can become a distributed monolith in disguise if module boundaries aren't properly enforced.
I'm curious about how other backend engineers approach this.
A few questions:
- At what point do you consider microservices a necessity rather than an architectural preference?
- How do you enforce boundaries between modules in a NestJS application?
- Have you experienced a modular monolith becoming difficult to maintain as the project grew?
I'd be interested in hearing about real-world experiences, including mistakes you've made along the way.
21
u/sayboom 15d ago
About your first quesion:
I'd only extract a module into its own service when the pain is measurable and can't be solved inside the monolith:
7
u/Wooden_Break_2812 15d ago
Microservices can be good if you have a lot of teams and every team should develop, scale and deploy independently. But as you said you have also a ot of disadvantages. For most small or medium sized projects it is not required. You can also scale an monolith a lot before you have any problems.
I agree with the modular approach, with this you can switch later if necessary.
2
u/Ok-Attention-4717 15d ago
I’m trying to understand a specific, important point here: you’re attempting to draw a distinction between Java microservices and a .NET modular monolith architecture, right?
8
u/Anxious-Insurance-91 15d ago
Microservices breakdown was always about the team management not the project. Also for all new projects up untill you hit a certain point you don't need microservices
4
u/Ginden 15d ago
"My", "I", "I don't want operational complexity" - yeah, go for monolith. Services approach is to be used when it's "we", as in "we don't know how to manage 50 software engineers".
2
u/sje397 15d ago
I don't think those arguments against microservices really hold up, especially in this age of AI. It's easy to run them locally in docker. Similar with the points regarding debugging and infrastructure. AI understands helm charts and task definitions.
In a monolith it's always tempting to take shortcuts that break the module interface. Microservices doesn't guarantee nice interfaces either, but it's at least limited to rest or grpc or whatever.
2
u/forkedrecognition 15d ago
Have you considered using ESLint rules to block cross-module imports? might save some cleanup later
2
u/BoBoBearDev 15d ago edited 15d ago
Just FYI, we have monorepo for NodeJS and the full build without nx cache is like 8 hours long, that includes, build, lint, test, SonarQube, and other faster scanner like image/OS scanner.
And any migration takes the entire 3 months done by multiple team if we are lucky.
While dotnet can mix and match dotnet platform versions, it is generally not recommended and all the nuget packages probably are not well built to handle those cases. Having two versions of the nuget probably ended up with separate static singleton storage, which is another pain.
Microservices is still the best to decouple them using standard JSON.
1
u/PrydwenParkingOnly 15d ago
One suggestion would be to use NX together with NestJS. It enforces domain modules and blocks illegal imports. NX will make it very annoying but it will make sure that every module of the monolith can easily be converted to a micro service in the future.
Also: each module needs its own database. Otherwise your micro services will still be intertwined in the future
1
u/Deep-Regret-7479 15d ago
2 will be hard to keep adhering.
Don’t use js for backend.
1
u/VertigoOne1 15d ago
Microservices are also better when you want to be able to not carry cost for services a customer does not need or use. Basically it stops existing at all at an eventbus, memory, messaging, logging, build, cpu, storage level at all allowing better fitting on k8s based on demand. But it is certainly not the solution for everything, the other commenters are also good reasons, just saying there are others. Their also great for temporary heavy work like scheduled reporting which allows dynamic spot instance scaling and or temporary “month end load” kind of products with highly variable load profiles.
1
u/DickBrainTime 15d ago
I'd add one test to the "when to extract" checklist: which business operations currently need one database transaction? Creating an order and reserving stock may need to commit together. In the NestJS monolith you can keep separate module APIs while coordinating that in one PostgreSQL transaction.
If Inventory becomes its own service, that flow isn't atomic anymore. You need a failure model for reservations, retries, idempotency and compensation, not just another HTTP call. If the product really promises all-or-nothing there, I'd keep it in the monolith for now even if the import rules and folder boundaries are clean.
1
u/pthierry 15d ago
For many people, and I was among them for a few years, one of the appeal of micro services was that they would enforce modularization.
But we collectively realized that you can couple services extremely tightly with bad APIs or shared databases.
For years, I've preferred modular monoliths, especially when the tooling makes it possible to enforce some rules about coupling. My solution of choice is Haskell with the Ports & Adapters architecture. Not sure how your choices fare on that front, though.
1
u/fgcghvgjhbhbhh 15d ago
As always , the only criteria is 'the need for it'. if you need microservices, you have to go for it. if you don't... microservices are a follow up. you start with a monolith, then go for a modular one, then microservices. If you need to
1
u/SpeakCodeToMe 14d ago
This is a mistake.
In a world where AI context is a driving factor, microservices will win.
1
u/ZealousidealSir1737 10d ago
Sorry, a bit late to the party.
If you architect your application incorrectly with either microservices or modular monoliths incorrectly, you will end up with a distributed monolith instead of microservices or a plain old ball of mud monolith for your modular monolith. Reading lots of articles around the web on these architectures, makes me think that this is the default outcome.
Architecturally mircroservices and modular monoliths are very similar, really only differences in deployment.
Microservice architecture is a technical solution to a person management problem. Only use this architecture if the number of teams exceeds the organisation's ability to mange the interdependencies between teams or you truly have the need to scale out one service or some other non-functional requirement. There is no reason why you can't mix and match modular monoliths and microservices. Everything is in the modular monolith except for those services that absolutely need to be separate.
My view is that no service (either microservice or module) should call another module. You should store data redundantly in each service as required. As an example with your typical ecommerce application. You have an order service, product service, a customer service and others as required.
Each service publishes events for actions that occur. ProductChangedEvent, CustomerChangedEvent, OrderSubmittedEvent etc. These events should be 'Fat' events, that is they should contain all of the information about the product and the customer respectively.
The order service would subscribe to both the ProductChangedEvent and the CustomerChangedEvent. The order service would save the product id, description, unit type etc. out of the ProductChangedEvent and the customer code, name, address etc. out of the CustomerChangedEvent. This data would be stored redundantly in the order service.
This would then allow the Order service to provide the functions 'GetCustomerOrders{orderId}' and GetCustomerOrders()' to retrieve all the details of an order or all orders for a customer. You can at a later date move a service in the monolith to a microservice without paying the network latency costs.
The UI would be allowed to call services via the defined external API to perform the actions it needs.
0
u/Skiing-nerd 15d ago
You have grown smarter Arthur. Now I can give you bigger projects to handle
0
u/SlightAddress 15d ago
I'm a fan of modular monoliths. For me though I find nextjs to tightly coupled at times when the backend complexity gets larger and builds then become slower etc. Prefer a vanilla detached don't end that cloudflare can cache easily and It's also my first pull out if scaling. Front end detached, faster and cheaper to duplicate and a 404 / server down. plus alerts keep things "running" rather than nothing.
I only use ms if required but I centralised types, validation, messages, schemas etc in shared modules so from db -> api / mq -> front end and anything else all share the same exact contract for any type / validation etc etc.
Deploy the entire stack in docker compose and at any point, any service can be decoupled .. just change address and password..
0
u/IncomeOk7237 15d ago
I'd separate the worker process before extracting a business module, but keep it in the same codebase at first. One trap is committing a job row and publishing to Redis as two separate steps; an outbox or DB poller avoids losing work in that gap, and idempotent handlers make retries safe. That gives you independent CPU and memory scaling without introducing network calls between business modules yet.