Choosing an architecture, part 1 of 2 (series complete)
Start with a modular monolith
Microservices solve specific problems that most new projects don't have yet. A modular monolith keeps the good parts, ships sooner and leaves the door open.
Bhanu Chhabra, . 12 minute read
A new project starts. Before there’s a single working screen, the architecture already has a dozen services, a message broker, an API gateway and a pipeline per repository. It looks modern and ready for scale.
Six months later the team is still building plumbing, and the first version a customer could use keeps slipping. Here’s why that happens, and why a modular monolith is usually the better place to start, especially on a greenfield project.
The cost of microservices on day one
A small team, too many services
Every service is something someone has to own: a repository, a pipeline, configuration, dependencies to upgrade, alerts to answer, a contract to keep stable. With fifty engineers and clear team boundaries, that load spreads out. With six engineers and fifteen services, each person owns several, and the team becomes the bottleneck. Work queues behind the same few people, every change touches several services, and nobody has time to go deep on any of them.
DevOps overhead before there’s a product
Microservices assume a platform underneath them: fast, automated provisioning, a deployment pipeline per service, monitoring and tracing across service boundaries, and environments you can spin up on demand. Martin Fowler called these the prerequisites for having microservices at all [1]. If they don’t exist yet, the team builds them first, and that’s real engineering time spent before any business feature ships.
Boundaries drawn before the domain is understood
At the start of a project, you don’t really know the domain yet. You learn it by building, showing and changing. Draw service boundaries in week two and some will be wrong, and in a distributed system a wrong boundary is expensive: two services that always change together, data that has to be fetched across the network on every request, a “simple” change that needs three coordinated releases. Moving a boundary stops being a refactor and becomes a migration.
Distributed-systems problems before the first MVP
Once data lives in separate services, you have to think like a distributed-systems engineer from day one: events instead of direct calls, eventual consistency instead of transactions, retries, idempotency, sagas, ordering, duplicate messages, partial failures. These are solvable, and in the right system they’re worth solving. But solving them before you have a working MVP means the first useful release arrives much later.
Microservices on day one
10 network messages, 1 retry, inventory updated after the response
Modular monolith
2 network messages, 3 function calls, 1 transaction
Friction all the way across
The cost isn’t only technical. Product waits longer for something to show users. Testers need several services running to test one flow. On-call covers more moving parts. Developers spend their days on plumbing instead of the problem they joined to solve. Months of that cost a team its momentum, and eventually some of its people.
What a modular monolith is
A monolith is one deployable application: one codebase, one build, one process (or several identical copies), usually one database. A modular monolith keeps that single deployable, but divides the inside into modules with boundaries that are enforced. Shopify, one of the best-known examples, describes it as all the code powering a single application, with strictly enforced boundaries between domains [3].
In practice:
- Modules follow business capabilities (billing, catalogue, notifications), the same lines you’d draw for services.
- Each module owns its data. Its tables are its own, and other modules don’t read them directly.
- Modules talk through public contracts: an interface, or events published in-process. They never reach into each other’s internals.
- The build checks the boundaries, so they survive deadlines.
In-process events need one caution. If the process dies after the database commit but before the handler runs, the event is gone. For anything that matters, such as billing a completed order, write the event to an outbox table in the same transaction as the change, and dispatch it from there. The outbox comes along unchanged when a module later becomes a service, which is a good reason to use it from the start.
Tidy folders alone don’t make a monolith modular. The boundaries have to be designed and enforced the way they would be between services, only without the network in between.
How it solves the day-one problems, and keeps the good parts
| The day-one problem | With a modular monolith |
|---|---|
| Small team, too many services | One deployable to own, run and upgrade. Ownership is per module, not per pipeline |
| DevOps overhead first | One pipeline, one deployment, one place to look when something breaks. Build the platform when you need it |
| Boundaries drawn too early | Moving a boundary is a refactor: the compiler and tests tell you what broke |
| Distributed-systems problems before the MVP | Calls are function calls and transactions are transactions. Use in-process events where decoupling helps, without the network failure modes |
| Slow start, friction everywhere | Working software sooner; the whole flow runs on one laptop and tests in one suite |
You also keep what made microservices attractive in the first place: clear boundaries, ownership per capability, modules you can test on their own, and code that doesn’t turn into a ball of mud.
Why it suits a greenfield project
A greenfield project is when you know the least about the domain and most need to learn fast. That’s exactly when a modular monolith pays off most:
- Faster to a working MVP. No platform to build first; the team spends its time on the product.
- Cheap to be wrong. You will draw some boundaries in the wrong place. Inside one codebase, fixing that is an afternoon’s refactor, not a cross-team migration.
- Simple to run and debug. One process, one log stream, one stack trace through the whole request.
- Cheap to host. One application and one database cost far less than a fleet of services, each with its own runtime and storage.
- Easy to join. New engineers clone one repository and run one thing.
- Ready to split later. A module with its own data and a clean contract is a service waiting to be extracted.
Because a module can be split out later, starting this way doesn’t lock you in. Fowler’s MonolithFirst observes that almost every successful microservices system he’d come across started as a monolith that was broken up later [2].
What a modular monolith costs
It isn’t free, and it’s better to know the bill before you start:
- One process, one blast radius. A memory leak or a runaway loop in one module takes the others down with it, and rolling back a bad deploy rolls back everyone’s changes.
- Scaling is all or nothing. You scale by running more copies of everything. One hot module means paying for capacity the rest don’t need. Running the same build in two roles, web and background worker, often buys enough time before anything has to leave.
- One database, shared load. Separate schemas don’t stop one module’s heavy report from slowing another module’s checkout. Watch the expensive queries per module, and move reporting to a replica early.
- Upgrades in lockstep. Every module moves to the next .NET version, framework or major library at the same time. In a large monolith the yearly .NET upgrade becomes a planned piece of work with its own owner.
- One pipeline and one queue. Build and test time grow with the code, and past a few teams everyone is waiting on the same merge queue.
None of these is a reason not to start this way. They’re what to watch, and several of them are the signals, in the next article, that a module is ready to leave.
Microservices are good, in their own right
None of this makes microservices wrong. At the right scale they’re the right answer, and many of the largest systems in the world run on them. The mistake is choosing them before the need exists.
Think about design patterns. Nobody good starts the day with “I know five patterns, they’re proven and they’re cool, so I’ll use three of them in today’s code.” You hit a problem: object creation is getting complicated, behaviour needs to vary at runtime. Then a pattern fits, and you use it because it solves something real. The need comes first, and the pattern follows.
Architecture works the same way. Microservices are a solution to a set of problems. If you don’t have those problems yet, you’re paying for the solution without getting the benefit. Two teams that went the other way show the cost: Segment merged over a hundred per-destination services back into one when the operational overhead outweighed the benefit [4], and Prime Video’s stream-monitoring team reported cutting infrastructure cost by more than 90% after moving a distributed design into a single process [5]. In both cases, for that workload, the split cost more than it returned.
When the need is real
The needs that justify a separate service
- Independent scaling. One capability has a very different load profile from the rest (a hot path, a batch-heavy job) and scaling the whole application for it is wasteful.
- Independent delivery for many teams. Several teams need to release on their own schedules without coordinating, and the shared deployable has become the bottleneck.
- Fault isolation. A failure in one capability must not take the others down, and the blast radius matters enough to pay for separation.
- A different runtime or technology. A part of the system needs another language, runtime or hardware (a model-serving component, for example).
- Compliance or security isolation. Some data or processing has to live separately, for audit scope, data residency or access control.
If you can’t name one of these for a given piece of the system, it probably doesn’t need to be a service yet.
Get the numbers from the monolith itself. Tag logs, traces and metrics with the module that
produced them (in .NET, an ActivitySource and a Meter per module). The hot module, the slow
one and the one whose errors spread then show up on a dashboard before anyone argues about them.
The prerequisites, before you split
- Stable, well-understood boundaries, ideally proven as modules first.
- Automated delivery per service: CI/CD, infrastructure as code, environments on demand.
- Observability across services: distributed tracing, correlated logs, metrics and alerting.
- Clear ownership and on-call for every service.
- Contract discipline: versioned APIs and events, and a way to change them without breaking consumers.
- Distributed-data skills: the outbox pattern, idempotent consumers, retries, sagas, and comfort with eventual consistency.
- Platform capacity: someone whose job includes keeping all of the above working.
Choosing
| Choose a modular monolith when… | Consider microservices when… |
|---|---|
| The team is small, or one team owns most of the system | Several teams need to deploy independently |
| The domain is new and the boundaries are still moving | Boundaries are stable and proven |
| CI/CD, observability and on-call aren’t mature yet | The platform and the skills are in place |
| Load is broadly even across the system | One capability’s scale or failure profile clearly differs |
| You need a working MVP quickly | A specific need above justifies the operational cost |
You don’t have to pick one forever. The usual path is a modular monolith first, then extracting the one or two modules that earn it, and keeping the rest together. How to make that move, the strategies and patterns for turning modules into services, is what the next article covers.
Building it so it can evolve
A modular monolith only keeps its promise if the “modular” part is real. In .NET, each module is
its own project with a small public contract, and everything else stays internal:
// Billing.Contracts: the only Billing types other modules may use
public interface IBillingModule
{
Task<InvoiceSummary> GetInvoiceAsync(Guid invoiceId, CancellationToken ct);
}
public sealed record InvoiceSummary(Guid InvoiceId, decimal Total, string Currency, InvoiceStatus Status);
Each module wires itself up, so the host only calls AddBilling() and never sees the internals.
The module’s DbContext puts every Billing table in a billing schema, and the module connects
with its own database login:
// Billing: registration and data, all internal apart from the extension method
public static class BillingRegistration
{
public static IServiceCollection AddBilling(this IServiceCollection services, IConfiguration config)
{
services.AddDbContext<BillingDbContext>(db => db.UseSqlServer(
config.GetConnectionString("Billing"), // a login that can only touch the billing schema
sql => sql.MigrationsHistoryTable("__EFMigrationsHistory", "billing")));
services.AddScoped<IBillingModule, BillingModule>();
return services;
}
}
internal sealed class BillingDbContext(DbContextOptions<BillingDbContext> options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder model) =>
model.HasDefaultSchema("billing");
}
That gives three layers of ownership. The code keeps internals internal. The schema keeps
Billing’s tables together, with their own migrations. The database login means a query from
another module into billing fails in the database, not only in code review. Questions that span
modules, like a report joining orders and invoices, don’t get a cross-schema join. They get a
read model fed by events, or a reporting replica that can read everything and that nothing writes
to.
The compiler stops one project using another’s internal types. It doesn’t stop a module from
using another module’s public domain types or its DbContext project directly. An architecture
test does, for every pair of modules, so the build fails the day someone takes a shortcut. This one
uses NetArchTest; ArchUnitNET works too:
public class ModuleBoundaryTests
{
private static readonly string[] Modules = ["Billing", "Catalogue", "Orders"];
public static IEnumerable<object[]> ModulePairs =>
from source in Modules
from target in Modules
where source != target
select new object[] { source, target };
// A module may use another module's Contracts project, and nothing else of it
[Theory]
[MemberData(nameof(ModulePairs))]
public void Modules_only_use_each_others_contracts(string source, string target)
{
var result = Types.InAssembly(Assembly.Load($"Shop.{source}"))
.ShouldNot()
.HaveDependencyOnAny($"Shop.{target}.Domain", $"Shop.{target}.Data", $"Shop.{target}.Application")
.GetResult();
Assert.True(result.IsSuccessful,
$"{source} reaches into {target}: {string.Join(", ", result.FailingTypeNames ?? [])}");
}
}
Then watch for the ways it decays:
- Shared tables. Two modules writing the same tables are one module with two names. Give each module its own schema.
- Boundaries by convention only. Folders don’t stop anyone; access modifiers, separate projects and architecture tests do.
- The “Common” dumping ground. Keep shared code to generic utilities; business concepts belong to a module.
- Chatty chains of calls across modules. That’s a distributed monolith inside one process. Use in-process events for anything that doesn’t need an immediate answer.
- Transactions across modules. Convenient now, painful when you extract. Use events with a transactional outbox instead.
- No owner per module. Boundaries without owners erode.
- One slow pipeline. Invest early in incremental builds and per-module test suites.
Avoid these, and each module already looks like a service: its own data, a public contract, and events for everything else. When one of the needs above shows up, you extract that module along boundaries you’ve already built and tested.
Today: one deployable with three modules, each owning a schema on a shared database server.
IBillingModule, so its code moves out first behind the same contract, its data follows, and only then can it scale on its own. The next article covers each step in detail.Next
The next article looks at the other side: microservices when the need is real. It covers how to tell the need is real and what has to be in place before you split.
We’ll also learn the strategies and patterns for converting a modular monolith into microservices, such as the strangler fig and branch by abstraction, and how to move a module out, data included, without stopping delivery.
References
- Martin Fowler, Microservice Prerequisites (2014). Back to text
- Martin Fowler, MonolithFirst (2015). Back to text
- Kirsten Westeinde, Shopify Engineering, Deconstructing the Monolith: Designing Software that Maximizes Developer Productivity (2019). Back to text
- Alexandra Noonan, Twilio Segment, Goodbye Microservices: From 100s of problem children to 1 superstar (2018). Back to text
- Marcin Kolny, Prime Video Tech, Scaling up the Prime Video audio/video monitoring service and reducing costs by 90% (2023). Back to text