Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Monolithic

Intent

One codebase, one executable, one deployment: the whole system ships and runs as a single unit.

When It Helps

  • A small team (roughly 1–8 developers) building the first version of a product.
  • The domain boundaries are still unclear and likely to shift.
  • Operational simplicity matters more than independent scaling of parts.

Structure

flowchart TB
  Clients --> App[Single Application]
  App --> UI[UI Module]
  App --> Logic[Business Logic Module]
  App --> DataAccess[Data Access Module]
  DataAccess --> DB[(Shared Database)]

Deployment unit: one artifact (single binary, container, or package) replicated as a whole. Communication: in-process function calls between modules; external clients use one entry point. Data ownership: a single shared database owned by the whole application.

Rules

  1. All modules ship together — no module may be deployed or versioned alone.
  2. Cross-module calls stay in-process; do not add network hops inside the monolith.
  3. All data access goes through the data-access module, never directly from UI code.
  4. Scaling means replicating the entire application behind a load balancer.

Illustrative Snippet

Illustrative pseudocode — not runnable.

# sketch: one deployable, modules call each other in-process
deployable: shop-app v1.4
  modules: [web-ui, catalog, checkout, data-access]

request -> web-ui -> catalog.list_items()
request -> web-ui -> checkout.place_order()
checkout.place_order() -> data-access.save(order)

Basic Recap

A monolith packages its user interface, business capabilities, and data access into one deployable application. Internal calls remain in-process and modules commonly share one database. Replicas run the same artifact, which keeps deployment, transactions, and debugging comparatively simple. The pattern trades independent scaling and fault isolation for low operational overhead.

Complex Scenario

A municipal permitting system runs applicant pages, staff review, payments, and monthly compliance reports in one application. Most requests are short, but a year-end report scans permit history and builds large in-memory summaries. The same database and runtime serve every workload. The operations team must absorb reporting demand without turning the whole product into separate services.

flowchart TB
  Applicants["Applicants"] --> LB["Load balancer"]
  Staff["Permit staff"] --> LB
  LB --> App1["Permit application replica A"]
  LB --> App2["Permit application replica B"]
  App1 --> Modules["UI, review, payments, reporting modules"]
  App2 --> Modules
  Modules --> DB[("Primary permit database")]
  Modules --> Files[("Document storage")]
  Reporter["Scheduled report trigger"] --> Modules

Failure walkthrough. A tenfold increase in requested reports consumes heap and database connections until applicant requests time out; one out-of-memory termination also removes payment capacity on that replica. Mitigate with a bounded report queue, streaming instead of in-memory aggregation, per-workload connection pools, and a small always-on request reserve. If reporting remains disruptive, run the same artifact with report-only routing and resource limits rather than immediately splitting domain services.

Edge cases — concurrent staff edits need record versions, payment retries need idempotency, and partial failure needs a durable report checkpoint. Memory pressure is a cross-module concern because every module shares the process heap.

Sketch (illustrative pseudocode, not runnable):

deployment permit-app:
  artifact: permits-2026.11
  request_replicas: 3
  report_replicas: 1
  shared_database: permits_primary

report_worker:
  queue: compliance-reports
  concurrency: 2
  result_streaming: true
  checkpoint_every: 1000_permits
  db_pool: 4

request_path:
  reserved_db_connections: 12
  payment_idempotency: required

Trade-offs

  • Benefit: simplest to build, test, and deploy — one pipeline, one artifact, and easy end-to-end debugging.
  • Benefit: in-process calls keep latency low and refactoring across modules cheap while the product is young.
  • Cost: scaling is coarse — a busy checkout path forces replication of the entire app, wasting resources.
  • Cost: a single failure or slow dependency can take down everything, and the codebase gets harder to split as it grows.

When to Use / When Not to Use

Use when:

  • Solo project or early product with one team and one deployable.

Avoid when:

  • Independent scaling or deploy per capability outweighs ops cost — consider Modular Monolith first, then Microservices.

Anti-pattern / Failure Mode

  • Misuse: A ‘modular’ monolith with no boundaries — big ball of mud.
  • Review signal: One slow dependency stalling the whole deploy.

Failure Modes / Trade-offs

  • Big-ball-of-mud drift: unrestricted imports and shared tables erase module ownership, making a small change require system-wide knowledge.
  • Coarse failure domain: a leak, deadlock, or CPU-heavy task in one feature harms unrelated features because runtime resources are shared.
  • Deployment congestion: every fix ships the entire artifact, so a large team serializes releases and expands rollback scope.
  • Operational trade-off: one pipeline and local calls are inexpensive to understand, but scaling replicates idle capabilities alongside busy ones.

Capacity review. Load-test reports and applicant traffic together, because separate benchmarks hide contention for heap, worker pools, caches, and database connections.

Recovery review. A replica restart must resume queued reports and preserve payment idempotency; running one artifact does not automatically make recovery atomic.

Ownership review. Even without service boundaries, assign modules to code owners and forbid direct cross-feature table writes to keep future changes understandable.

  • Data trade-off: one database supports easy joins and transactions, but large reports can block online writes and broad credentials increase the impact of a flaw.
  • Release trade-off: a single rollback is conceptually simple, yet reverting the artifact may revert unrelated urgent features at the same time.
  • Scaling trade-off: adding replicas is effective until one database or shared external dependency becomes the actual bottleneck.

Constraint-based use / avoid

ConstraintRecommendation
Team of 1–8 with uncertain product boundariesUse a monolith; refactoring in one codebase is cheaper than premature distribution.
Limited operations budget and no 24/7 platform teamUse one well-instrumented deployment with managed database backups.
Strict latency for checkout beside heavy analyticsUse cautiously: isolate workload resources, or avoid one runtime if isolation cannot meet the budget.
Compliance demands independent payment certification and release cadenceAvoid a single deployment for that scope when certification boundaries cannot be isolated.
  • Modular Monolith — keeps one deployment but enforces strict module boundaries as a stepping stone out.
  • Microservices — splits the monolith into independently deployable services when teams and scale demand it.
  • Serverless — removes server management entirely, opposite extreme from managing one server app yourself.
  • Single Source of Truth — one codebase, one truth.
  • Layered — typical in-process structure.