What it is
A modular monolith is a single deployable application whose code is divided into modules along business capabilities (Ordering, Catalog, Payments), each with:
- its own data, usually a separate schema in a shared database, touched by no other module;
- a narrow public contract (interfaces, commands, DTOs, integration events) through which other modules interact with it;
- internal everything else: entities, handlers, EF Core configuration.
It runs as one process and ships as one artifact, like any monolith. The difference from a "big ball of mud" is that the boundaries are designed and enforced.
Why it exists
Microservices give teams independent deployment and scaling, but they bring network calls, partial failures, distributed data and a lot of operational machinery. Many systems do not need those properties on day one, and many teams split too early along the wrong lines. A modular monolith delivers most of the design benefit, clear boundaries, private data, explicit contracts, while keeping in-process calls, one deployment and simple local development. If a module later needs independence, the boundary already exists.
How it works
Project layout. Each module is a few projects, for example Ordering.Core, Ordering.Infrastructure and Ordering.Contracts; only *.Contracts is referenced by other modules. The host (Shop.Api) references all modules and wires them up.
// Catalog.Contracts
public interface ICatalogApi { Task<Price?> GetPrice(ProductId id, CancellationToken ct); }
// Catalog.Core
internal sealed class CatalogApi(CatalogDbContext db) : ICatalogApi
{
public async Task<Price?> GetPrice(ProductId id, CancellationToken ct) =>
await db.Products.Where(p => p.Id == id).Select(p => p.Price).SingleOrDefaultAsync(ct);
}
Data per module. Each module has its own DbContext mapped to its own schema (HasDefaultSchema("catalog")) and its own migrations. No joins across schemas; a module that needs another's data asks through the contract or keeps a local copy fed by events.
Synchronous calls go through contracts: fast in-process method calls. Asynchronous integration events (OrderPlaced) go through an in-process bus or mediator, so the publisher does not know its subscribers. An in-memory bus loses events if the process dies between commit and dispatch; the transactional outbox pattern stores them in the same transaction as the business data.
Enforcement. Mark module internals internal; give each module a database login restricted to its schema; run architecture tests (NetArchTest, ArchUnitNET) in CI that fail when a module references another module's non-contract types.
When to use it
- New products and teams whose domain boundaries are still being discovered: boundaries are cheaper to move inside one codebase.
- Small to medium organisations without a strong need for independent deployment per team.
- As a step away from a tangled monolith, before (or instead of) moving to microservices.
- As the target of a "monolith first" strategy, extracting services only where a module needs independence.
When not to
- Many teams that must deploy independently many times a day without coordinating releases.
- Modules with very different runtime needs: a GPU-heavy or memory-hungry component that should not share a process.
- Hard isolation requirements (regulatory, security, fault isolation) for a specific capability; extract that module.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| One deployable | Simple operations, one pipeline, easy local runs | One release train; everything scales together |
| In-process contracts | Fast calls, no network failures | Discipline needed to keep contracts narrow |
| Schema per module in one database | Data ownership, easier extraction | No cross-module joins; some data duplication |
| In-process events | Decoupled modules, no broker to run | Lost on crash unless stored (outbox) |
| Extracting a module | Independent deploy, scale and isolation | Network latency, partial failure, distributed data |
Alternatives
- Traditional (layered) monolith: simpler at first, but boundaries erode without modules.
- Microservices: independent deployment and scaling per service, at the cost of distribution.
- Vertical slices or clean architecture inside each module: these complement the modular monolith rather than replace it.
Failure modes
- Boundary erosion: cross-schema queries and references to internals accumulate until modules are modules in name only.
- Shared fate: a memory leak, a thread-pool starvation or a CPU-heavy job in one module degrades or crashes every module.
- Chatty contracts: fine-grained calls that are harmless in process become slow and fragile after extraction.
- Shared "common" module: a dumping ground that every module depends on, coupling them all.
- Lost events: in-memory integration events disappear if the process stops between commit and dispatch.
In production
Deploy as one unit, but monitor per module: tag logs, metrics and traces with the module name so a noisy neighbour is visible. Protect shared resources: separate connection pools per module, bounded background work, timeouts. Use an outbox for integration events that must not be lost. When extracting a module, keep its contract, replace the in-process implementation with a client (HTTP or gRPC), move its schema to its own database, and add the resilience patterns a network call needs (timeouts, retries with backoff, circuit breakers).
Common misconceptions
- "A modular monolith is a stepping stone you must leave." Many systems stay one for good; extraction is an option, not a goal.
- "Modules isolate failures." They isolate code and data. Runtime faults in one module still affect the process.
- "Schema per module means a database per module." Separate schemas with restricted logins in one server are usually enough.
- "Extraction is free if boundaries are good." The code change is contained; the latency, failure modes and data consistency work are new.
Interview angle
Propose a modular monolith when the scale and team size do not yet justify microservices, and show you know how to keep it modular: capability-aligned modules, private schemas, narrow contracts, events, enforcement in CI. Then discuss limits (shared runtime, one scaling unit, one release train) and the extraction path: which module you would extract first, why, and what changes when an in-process call becomes a network call.
Model assumptions
Contracts, schema ownership, the cross-schema shortcut and boundary enforcement are scripted illustrations with hand-placed packets, slowed to 30–40 ms per hop. The other steps are simulated: each module is a server with 16 workers and a 1–2 ms mean service time; module-to-module calls use 0 ms links (in process). Each schema is a database node with its own pool of 8 connections, 1 ms away, with about 4 ms per query (lognormal, cv 0.3). The shopper is 20 ms away; arrivals are random (Poisson). The in-process event bus is modelled as a queue consumed by a Notifications worker (4 workers, 20 ms each). The process crash fails every module at once for 3 s. The extracted Payments service is 3 ms away each way, an illustrative figure for a data-centre call including serialisation. In the challenge each charge takes 40 ms of work. Numbers show behaviour and orders of magnitude, not benchmarks.