Volume 6 · Chapter 7 — Enterprise Application Architecture and Design Patterns

Modular Monolith Architecture

Goutham Mayaluri 7 min read .NETC#

7.1 Introduction

A modular monolith is a single deployable application whose internals are divided into strongly bounded modules. It aims to capture the architectural benefits of microservices - clear ownership, high cohesion, independent evolution - while avoiding their operational tax: network calls, distributed transactions, and the complexity of running many services. For most teams it is the right default starting point, and often the right permanent home.

7.2 Monolith vs Modular Monolith vs Microservices

Architecture Boundaries Deployment Main cost
Big-ball-of-mud monolith None - everything coupled One unit Change is unsafe everywhere
Modular monolith Strong, in-process One unit Discipline to keep modules separate
Microservices Strong, over the network Many units Distributed-systems complexity

7.3 Module Boundaries by Business Capability

Modules should align with business capabilities, not technical layers. A module owns its domain model, its application services, and its data, and it exposes a narrow public contract. Everything else isinternal.

flowchart TB
  subgraph App["Banking Modular Monolith (one process)"]
      direction LR
      A["Accounts Module"]
      P["Payments Module"]
      L["Loans Module"]
      N["Notifications Module"]
  end
  A -- "public contract / events" --> P
  P -- "events" --> N
  L -- "public contract" --> A

7.4 Enforcing Isolation in .NET

The defining discipline of a modular monolith is that modules cannot reach into each other’s internals. In .NET you enforce this with separate projects,internalaccess modifiers, and a public contract assembly per module. Architecture tests fail the build if a module references another module’s internal namespace.


// Payments.Public - the ONLY thing other modules may reference.
namespace Payments.Public;
public interface IPaymentsApi
{
    Task<PaymentResult> PayAsync(PayRequest request, CancellationToken ct);
}

// Payments.Internal - hidden from everyone else.
namespace Payments.Internal;
internal sealed class PaymentsApi : IPaymentsApi { /* ... */ }

7.5 In-Process Communication

Modules communicate two ways: synchronously via each other’s public interfaces (for queries and commands that need an immediate answer), and asynchronously via an in-process event bus (for “something happened” notifications that should not couple modules). Because it is all in-process, calls are fast and can share a transaction when appropriate.

sequenceDiagram
  autonumber
  participant P as Payments Module
  participant Bus as In-Process Event Bus
  participant N as Notifications Module
  participant L as Loans Module
  P->>P: complete payment (one DB transaction)
  P-)Bus: publish PaymentCompleted
  Bus-)N: PaymentCompleted
  Bus-)L: PaymentCompleted
  N-->>N: send receipt
  L-->>L: update repayment schedule
  Note over P,L: Publisher knows no subscribers; add a module without touching Payments

7.6 Data Ownership Strategies

Even inside one database, modules should own their tables. The common progression is: shared database with a schema per module (no cross-schema foreign keys or queries), then separate databases if a module later needs independent scaling. Keeping data ownership clean now is what makes future extraction to a service possible.

7.7 The Migration Path to Microservices

A well-built modular monolith is the ideal launchpad for selective microservices. Because each module already has a narrow contract, its own data, and event-based integration, extracting one means swapping the in-process call for a network call and giving it its own database and deployment - not a rewrite. You extract only the modules that actually need independent scaling or team autonomy, and leave the rest in the monolith.

sequenceDiagram
  autonumber
  participant Client
  participant Mono as Monolith (Accounts, Loans)
  participant Pay as Extracted Payments Service
  Client->>Mono: request
  Mono->>Pay: HTTP/gRPC call (was in-process)
  Pay-->>Mono: result
  Mono-->>Client: response
  Note over Mono,Pay: Only the transport changed; the contract stayed the same
Senior Interview Question

Why choose a modular monolith instead of microservices?

Because it delivers most of the architectural benefit - bounded contexts, clear ownership, independent evolution - without the distributed-systems tax. You keep in-process calls (fast, debuggable, transactionally simple), one deployment pipeline, and one datastore to operate, while still enforcing strong internal boundaries. Microservices add network latency, partial failure, eventual consistency, and heavy operational tooling; those costs are only justified when you genuinely need independent scaling or team autonomy. Starting modular keeps that option open without paying for it prematurely.

Senior Interview Question

How do you stop a modular monolith from decaying into a big ball of mud?

Boundaries need active enforcement or they erode. Concretely: put each module in its own project with a public contract assembly andinternalimplementation; forbid cross-module references except to the public contract; add architecture tests (NetArchTest/ArchUnitNET) that fail CI on violations; give each module its own schema with no cross-module foreign keys; and integrate via events rather than shared tables. Code review and a shared understanding that the boundaries are the architecture matter as much as the tooling.

Senior Interview Question

How should modules share data if they cannot query each other's tables?

Through contracts and events, not shared reads. If a module needs another’s data for a decision, it calls the owner’s public API. If it needs a local copy for performance or its own read models, it subscribes to the owner’s events and maintains its own denormalised projection, accepting eventual consistency. This keeps ownership clear and mirrors how the modules would communicate if they were later extracted into services - which is exactly the property you want to preserve.

Principal Engineer Question

How do you design a modular monolith so it can scale to microservices later?

I design each module as if it were already a service: a narrow public contract, its own data ownership with a dedicated schema, no shared mutable state, and event-based integration for cross-module reactions. I avoid in-process shortcuts that would not survive a network boundary, such as spanning a transaction across modules or joining across schemas. I keep the event contracts serialisable and versioned. With that discipline, extraction becomes a mechanical change - replace the in-process dispatcher with a broker or an HTTP client - rather than a redesign, and I only extract the modules whose scaling or ownership needs actually justify it.

Principal Engineer Question

When is extracting a module into a service NOT worth it?

When the module does not have an independent scaling profile, an independent team, or an independent release cadence that the monolith is blocking. Extraction adds a network hop, a separate datastore, its own deployment and observability, and turns a simple in-process transaction into a distributed workflow requiring sagas and idempotency. If none of those costs buy you something concrete - the module is not a bottleneck, no team is contending for it - you are trading real complexity for architectural fashion. I extract on evidence (a measured bottleneck, a coordination cost), not on principle.

7.8 Banking Enterprise Scenario

A bank builds its new platform as a modular monolith with Accounts, Payments, Loans, and Notifications modules, each in its own project with a public contract and a dedicated database schema. Modules integrate through published events over an in-process bus. Eighteen months in, Payments becomes a throughput bottleneck during salary-day peaks; because it already owns its data and exposes a stable contract, the team extracts just Payments into its own service backed by its own database, swapping the in-process dispatch for a broker. The other three modules stay in the monolith, and the overall system keeps its low operational footprint.

Chapter Revision Notes

  • A modular monolith = strong in-process boundaries, single deployment.
  • Align modules with business capabilities; expose narrow public contracts.
  • Enforce isolation with projects, internal access, and architecture tests.
  • Give each module its own schema; integrate via contracts and events.
  • Extract to microservices selectively, on evidence, not by default.