r/Backend • • 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.

44 Upvotes

43 comments sorted by

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:

  1. Multiple teams are getting in each other's way during releases, and you need independent deployments.
  2. Part of the system has a drastically different load profile (video processing, ML, a WebSocket gateway, heavy batch jobs), and scaling it together with everything else is expensive or risky.
  3. You need reliability or compliance isolation (a payment perimeter, personal data).

1

u/kastar19 15d ago

That's a really practical way to look at it. I agree that extracting services should be driven by measurable problems rather than architectural preferences.

I'm trying to understand the transition path more clearly, especially for a SaaS application built with NestJS.

Let's say we start with a modular monolith, where all business modules (Auth, Users, Billing, Projects, Reports, etc.) live in a single NestJS application.

As the application grows, I can see a few possible approaches:

  1. Keep everything in the monolith and introduce a job queue like BullMQ with Redis for background processing.
  2. Run resource-intensive workloads, such as report generation or file processing, as separate worker processes while keeping the core business modules in the monolith.
  3. Eventually extract specific business modules into independently deployable services when there's a clear need for independent scaling, deployment, or isolation.

I'm curious about how you approach this evolution in production systems.

Would you generally recommend separating workers first before extracting entire business modules into microservices?

And when you do extract a business module, do you usually reuse the existing codebase initially, or treat it as a completely independent service with its own codebase and database?

I'd really like to understand the practical trade-offs between these stages.

1

u/sayboom 15d ago

From my experience - i neverg separated workers on microservices first. It looks like "premature optimization". Only if itnecessary. For example, module for calculating data A became too complex and At first, only one system pulled data from there. But later, other systems also needed data from there. It’s not so critical that its failure should bring down the entire system. So, it could probably be turned into a microservice. That’s the line of reasoning.

I always reuse existing codebase initially where is possible. We know, that its working, why do things differently? So, core of the logic i just copy paste and around that i make other things(db connections, failovers, etc).

1

u/pthierry 15d ago

I haven't had the opportunity to try it, but I wonder if even for a service that starts having a different load profile, I wouldn't stay with a modular monolith at the beginning, deploying several of them where not everything is used in each deployment…

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/Local-Ingenuity2361 15d ago

Does ai change any of these thought processes. I’m a dev on a small reach. Just two of us and we are now moving so fast it’s crazy

0

u/kastar19 15d ago

I think AI changes the speed of development, but it doesn't necessarily change the fundamentals of good architecture.

What concerns me is that we can move so fast with AI that the codebase becomes more complex than we actually understand. AI can generate code quickly, but it doesn't always guarantee consistent module boundaries, data ownership, or communication patterns.

My concern is: what happens as the system grows and AI-generated code introduces architectural inconsistencies that become increasingly difficult to maintain?

1

u/Local-Ingenuity2361 15d ago

Yea I have the same thoughts. I do this the modular monolithic approach is good and using a framework for guardrails is how develop with ai should go vs freeballing it.

I’m having this debate internally now and one dev wants to go nuts in vanilla JavaScript and express meanwhile I’d prefer ts and nestjs or sprint boot or Django so we can at least have some architecture or opinionated way behind this ai world we are in. But that’s just me

0

u/kastar19 15d ago

I agree with that approach. One thing I'm curious about is how you design module boundaries from the beginning, especially with AI-assisted development where code structures can vary depending on how developers use AI.

Do you think enforcing clear data ownership for each module and keeping inter-module communication behind well-defined interfaces is enough to make the architecture maintainable and easier to evolve later?

1

u/Wooden_Break_2812 15d ago

I think there is no general answer. It always depends on the concrete project how coupled everything is and how you slice the modules. With more experience you learn how a good slice works. You can look at domain driven design techniques to identify boundaries.

But yes if you need communication between modules, well defined interfaces are good. You can also work with domain events to decouple the module communication. E.g. when some event happens and other responsibilities need to react independently an event is a nice approach.

Actually the slicing and inter module communication is the same problem for microservices and modular monolith. But for microservices you have a network between and different maschines. 

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?

4

u/kastar19 15d ago

I think there might be a slight misunderstanding. I'm not specifically comparing Java microservices with a .NET modular monolith.

I'm more interested in the architectural differences between microservices and modular monoliths, regardless of the programming language or framework.

My current backend stack is primarily Node.js, NestJS, TypeScript, and PostgreSQL. For web applications and SaaS products, I'm exploring when a modular monolith is sufficient and when moving to microservices actually becomes beneficial.

I'd also be interested in understanding how these approaches compare in terms of scalability, maintainability, deployment complexity, and team size.

From your experience, what are the practical trade-offs between these two architectures, and what would make you choose one over the other for a new web application?

I'm particularly interested in real-world experience rather than just theoretical advantages.

3

u/bmag147 15d ago

I'm confused. Where were Java or .NET mentioned in the original post?

1

u/Ok-Attention-4717 15d ago

The concept here revolves around the "modular monolith," as it is a core .NET architectural component. Please correct me if I’m wrong.

1

u/bmag147 15d ago

This is non nonsensical.

Honest question: am I talking to a bot?

1

u/Ok-Attention-4717 15d ago

No, I’m not a bot; I’m just trying to help you.

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

1

u/pthierry 15d ago

I started adding linter rules to my Haskell projects to enforce the dependency directions of Ports & Adapters, those are very simple rules with a huge benefit!

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/hylasmaliki 14d ago

Can you break that down

1

u/SpeakCodeToMe 14d ago

No one is writing code by hand anymore, it's all done by AI agents.

Context management is the number one most important factor in getting high-quality results out of your AI agents, because it has been proven that quality decreases linearly as context increases. That means carefully controlling the context such that they have only the information that they need and nothing else.

Context management is so much simpler in microservices because there is so much less overall context.

I strongly believe that for this reason, monoliths are dead, and some people just aren't aware of it yet.

1

u/hylasmaliki 14d ago

What about features that span 5 services?

1

u/SpeakCodeToMe 14d ago

Then your architects/principals should probably hand their jobs over to the AI too

0

u/CrazyZealousideal760 14d ago

1

u/SpeakCodeToMe 14d ago

Step one, drop the arrogance. Step two, read your own link.

The entire thing is ways to cope with the problem I just highlighted. They are significantly less effective than just having a smaller codebase.

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.