Monolith first: architecture notes for a team of five
I have worked on two systems that were, on paper, the same product: an order-management backend for a mid-size retailer. One had nine services and three developers. The other was a single deployable with eight developers and four years of history. The second one shipped faster, had fewer production incidents, and onboarding a new hire took days instead of weeks. This post is my attempt to write down why.
What microservices actually solve
Microservices solve an organisational problem: many teams need to own, deploy and operate their part of the system without coordinating with everyone else. If you have three developers, you do not have that problem. What you get instead is the cost: network calls where a method call used to be, eventual consistency where a transaction used to be, a deployment pipeline per service, distributed tracing, and a set of failure modes that only exist because the process boundary was introduced.
A useful question before splitting anything: which boundary hurts today? If the answer is “the deploys collide because two teams touch the same repository”, you have a case. If the answer is “it would be cleaner”, you do not.
The modular monolith
A modular monolith is one deployable whose internals are organised into modules with boundaries as strict as service boundaries. The point is not to avoid the split forever; it is to make the split possible later without an archaeology dig. If Billing already talks to Orders through one explicit contract, moving Orders into its own service is a weekend of infrastructure work, not a six-month rewrite.
In .NET the cheapest honest enforcement is the assembly boundary and the internal keyword. The compiler does the review for you.
src/
App/ composition root, Program.cs only
Modules/
Orders/ Orders.Module.csproj
Billing/ Billing.Module.csproj
Shipping/ Shipping.Module.csproj
SharedKernel/ Money, Address, CustomerId: things every module uses
Each module has exactly one public type, its registration entry point. Everything else is internal:
// Orders/OrdersModule.cs: the only public type in the assembly
public static class OrdersModule
{
public static IServiceCollection AddOrders(this IServiceCollection services) =>
services
.AddScoped<IPlaceOrderHandler, PlaceOrderHandler>()
.AddScoped<OrdersDbContext>();
}
public interface IPlaceOrderHandler
{
Task<OrderId> HandleAsync(PlaceOrder command, CancellationToken ct);
}
The composition root knows nothing about the modules except their Add methods:
// App/Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddOrders()
.AddBilling()
.AddShipping();
var app = builder.Build();
app.MapOrdersEndpoints(); // a small public extension per module
app.Run();
Because PlaceOrderHandler and OrdersDbContext are internal, Billing physically cannot call them. The only way for Billing to ask Orders anything is through the contract that Orders chose to expose, and that contract is the thing you review.
Rules that keep it from decaying
- One public type per module. If you need a second, it is probably part of the same contract; put it next to the first.
- No shared tables. Give each module its own schema prefix (
orders.,billing.) from day one, even inside one database. A later split is then aCREATE SCHEMAand a move. - Cross-module calls go through a contract, never through a query against another module’s tables. Read models that join across modules are the first crack in the wall.
- Test the boundary, not the internals. Module tests live in the module assembly via
InternalsVisibleTo; integration tests drive the public contract only.
When to split
Three signals, in my experience: a module needs a different scaling profile than the rest (it is the only part that is CPU-bound); a module needs a different release cadence (regulatory changes every month while the core changes quarterly); or a different team now owns it. Absent all three, the monolith is not a compromise. It is the architecture that matches the size of the team you actually have.