Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 65 ยท Building Blocks of the Web Stack

Monolith vs Microservices

Compare monolithic and microservice architectures, modular monoliths, service boundaries, costs of distribution and a migration path, with Python demos.

How should you divide an application into deployable pieces? At one extreme is a monolith: one program, one codebase, one deployment containing everything. At the other is microservices: many small, independently deployed services, each owning one business capability and its data. Neither is "modern" or "outdated"; each fits certain teams and stages. Choosing wrongly costs years, so this lesson gives you a way to reason about it.

The monolith

A monolith packages all features (users, catalog, cart, payments, notifications) in one application, usually backed by one database. A function call connects any two parts.

Strengths

  • Simple to build and run: one repo, one build, one deploy, one thing to monitor.
  • Fast calls: in-process calls take nanoseconds, with no network failures.
  • Easy transactions: one database makes "charge the card and create the order" atomic.
  • Easy refactoring and debugging: your IDE sees everything; one stack trace shows the whole story.
  • Low operational cost: great for small teams and new products.

Weaknesses (as it grows)

  • Scaling is all-or-nothing: to scale the busy search feature you must scale the whole app.
  • Deploys get risky: a small change in one area ships with everything, so releases slow down.
  • Coupling creeps in: without discipline, modules reach into each other's internals ("big ball of mud").
  • Large teams collide in one codebase; build and test times grow.
  • One technology stack for everything; one bad memory leak or crash takes all features down.

Microservices

In a microservice architecture each service is small, owns its data, is deployed on its own and talks to others over the network (HTTP, gRPC, queues). Usually an API gateway fronts them.

flowchart LR
  G["API gateway"] --> U["Users"]
  G --> C["Catalog"]
  G --> O["Orders"]
  G --> P["Payments"]
  U --> UD[("Users DB")]
  C --> CD[("Catalog DB")]
  O --> OD[("Orders DB")]
  P --> PD[("Payments DB")]
  O -. "event: order placed" .-> N["Notifications"]

Figure: each service owns its database; services communicate through APIs and events, never through each other's tables.

Strengths

  • Independent scaling: scale only the hot service.
  • Independent deployment: small, frequent, low-risk releases by separate teams.
  • Fault isolation: a failing recommendation service need not take down checkout (if designed for it).
  • Technology freedom: the right language or database per service.
  • Team autonomy: a team owns a service end to end.

Costs, which are large

  • Distributed-system problems: network latency, timeouts, partial failure, retries, duplicate messages.
  • No easy transactions: updating several services' data needs sagas or eventual consistency.
  • Operational load: dozens of deployments, pipelines, dashboards, secrets, versions.
  • Harder debugging: one user action touches many services; you need tracing and good logs.
  • Data duplication and consistency between services.
  • Testing across services is harder.

Monolith or microservices?

Monolith Simple, fast calls, easy transactions. Scale and deploy as one.
Microservices Independent scale and deploys. Network and data complexity.
Modular monolith One deployment, strict internal boundaries. A strong middle path.

Start simple. Split when pain justifies the cost.

1 / 2

The modular monolith: the underrated middle

A modular monolith is still one deployable program, but it is divided into well-defined modules with clear public interfaces and no reaching into each other's data. It gives most of the maintainability of microservices without the network. Many successful systems stay here for years, and when one module truly needs to be separate, its clean boundary makes extraction straightforward.

Rules that keep a modular monolith honest:

  • Each module exposes a small interface; other modules never touch its tables.
  • Dependencies point one way; no cycles between modules.
  • Cross-module communication uses explicit calls or in-process events.
  • Enforce it with code structure and automated checks.

Defining service boundaries

The hardest part is where to cut. Good boundaries follow the business, not the technology:

  • Split by business capability (orders, payments, inventory), not by layer (all controllers here, all databases there).
  • Each service should have high cohesion (things that change together stay together) and low coupling (few calls to others).
  • A service owns its data; others must ask via its API. A shared database between services is a hidden monolith.
  • Prefer boundaries where a team can own the service end to end.
  • If two services must always be changed and deployed together, they are really one service.

What a network hop costs

In a monolith, "get the user" is a function call. In microservices it is a network request that can be slow, fail or time out. Latency adds up across call chains, and availability multiplies down, as the availability lesson showed.

Python In-process call vs a chain of service calls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Even with healthy 99.9% services, a deep call chain loses availability and gains latency. This is why microservice designs favor shallow call graphs, asynchronous events, caching and timeouts with fallbacks.

The transaction problem

In a monolith with one database, placing an order can be a single transaction: either everything happens or nothing does. When orders, inventory and payments are separate services with separate databases, no single database transaction spans them. If payment succeeds but inventory reservation fails, you must undo the payment with a compensating action. This is the saga pattern, and it demands careful design.

Python Atomic in a monolith, compensation across services

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The monolith simply rolls back. The services version needs a deliberate refund step, and between the charge and the refund the customer briefly sees an inconsistent state. Designing and testing such flows is one of the real costs of splitting.

Migrating: the strangler approach

You rarely rewrite a monolith from scratch (that usually fails). Instead:

  1. Put a gateway or proxy in front so routing can change without clients noticing.
  2. Pick one well-bounded capability (one that changes often, or needs separate scaling).
  3. Build it as a new service with its own data; route its traffic to the new service.
  4. Remove the old code, then repeat for the next piece.

This "strangler fig" approach delivers value incrementally and lets you stop whenever the benefit no longer justifies the cost.

How to decide

Situation Lean toward
New product, small team, unclear domain Monolith (modular)
Need to move fast with few people Monolith
Many teams blocked by one codebase Split into services
One component needs very different scale Extract that component
Strong regulatory isolation needs Separate that service
No ops or monitoring maturity yet Stay monolithic until ready

A common failure is the distributed monolith: services that are deployed separately but are tightly coupled, share a database or must release together. It has all the costs of microservices and none of the benefits.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which is a genuine advantage of a monolith?

    Calls are function calls and one database makes transactions easy; the other options are microservice benefits.
  2. 2. What is a modular monolith?

    It keeps a single deployment but enforces clear interfaces between modules, making later extraction easy.
  3. 3. What is a 'distributed monolith'?

    It combines the operational cost of distribution with the coupling of a monolith.
  4. 4. Why is a payment and inventory update hard across two services?

    Without a shared transaction you design sagas with compensation steps and accept eventual consistency.
  5. 5. How should you usually migrate from a monolith?

    Incremental extraction keeps the system running and lets you stop when benefits fade.

Summary

  • A monolith is simple, fast and transactional; it scales and deploys as one unit and can become tangled.
  • Microservices give independent scaling, deployment and team autonomy at the price of distributed-system complexity.
  • A modular monolith captures many benefits cheaply and eases a later split.
  • Draw boundaries around business capabilities; each service owns its data.
  • Network calls cost latency and availability; cross-service workflows need sagas and compensation.
  • Start simple, split for a concrete reason, and migrate incrementally with a gateway.

Next: Types of Load Balancers