Chapter 18 of 33

Monolith vs Microservices

Every application has to be split into pieces somehow. One option is to build it as one deployable program that contains every feature. Another is to build it as many small programs that each own one feature and talk over the network. These two styles are the monolith and microservices.

Microservices became famous through companies like Netflix and Amazon, and many students assume they are simply "the modern, correct way". They are not. They solve real problems for large organisations and create new ones for everyone. In 2023 an Amazon Prime Video team wrote publicly about moving one monitoring service from a distributed, serverless design to a single process and cutting that service's infrastructure cost by over 90%. The right choice depends on your team, your scale and your domain.

This chapter compares the two styles, shows what microservices really cost (network failures, distributed data, operations), introduces the saga pattern for transactions across services, and gives a practical way to decide.

Monolith                                  Microservices

┌──────────────────────────────┐          ┌───────┐  ┌─────────┐  ┌─────────┐
│  Users │ Orders │ Payments   │          │ Users │  │ Orders  │  │Payments │
│  Catalog │ Search │ Notify   │          └───┬───┘  └────┬────┘  └────┬────┘
│     (one codebase, one       │              │ DB         │ DB         │ DB
│      process, one deploy)    │          ┌───┴────┐ ┌─────┴───┐ ┌──────┴──┐
└──────────────┬───────────────┘          │Catalog │ │ Search  │ │ Notify  │
               │                          └────────┘ └─────────┘ └─────────┘
         ┌─────┴─────┐                    each: own process, own data, own deploy,
         │ one DB    │                    talks to others over HTTP/gRPC or messages
         └───────────┘

Definitions

Monolith: an application built and deployed as a single unit. All features share one codebase, run in one process (copied onto many servers if needed), and usually share one database.

Microservices: an architectural style in which an application is a set of small services, each running in its own process, built around one business capability, owning its own data, and deployable independently. Services communicate through lightweight mechanisms such as HTTP APIs, gRPC, or messages.

Two points are often misunderstood:

  • A monolith can scale horizontally. You can run 50 copies of the same monolith behind a load balancer, as described in the Scalability: Vertical vs Horizontal chapter. "Monolith" is about how the code is built and deployed, not about running on one server.

  • "Micro" is not about lines of code. A service is sized around a business capability (payments, catalog, delivery tracking) that one team can own end to end.

The middle ground: modular monolith

A modular monolith is one deployable unit whose code is split into strict modules with clear interfaces (for example, the Orders module may call only the public API of the Payments module, never its tables). It keeps the simplicity of one deployment while preparing clean boundaries. If one module later needs to become a service, the seam is already there.


Monolith: Strengths and Weaknesses

Strengths

  • Simple to build, test and run. One repository, one build, one deployment, one log file to read.

  • Fast internal calls. A function call takes nanoseconds; a network call takes milliseconds and can fail.

  • Easy transactions. One database means one ACID transaction can update orders, stock and payment together.

  • Easy refactoring. Your IDE can rename a method across the whole system.

Weaknesses (that appear as the team and code grow)

  • Deployment coupling. A one-line change to the search feature requires redeploying everything, so releases become slow, risky, and batched.

  • Scaling is all-or-nothing. If image processing needs 10× CPU, you must scale the entire app.

  • Fault isolation is weak. A memory leak in one feature can crash the process that serves every feature.

  • Team coordination. 200 engineers in one codebase step on each other: merge conflicts, long builds, unclear ownership.

  • One technology stack for everything.


Microservices: Strengths and Costs

Strengths

  • Independent deployment. The payments team ships five times a day without waiting for anyone.

  • Independent scaling. Run 40 instances of Search and 3 of Admin.

  • Fault isolation. If Recommendations crashes, checkout still works (if designed well).

  • Team autonomy and clear ownership. One team, one service, one on-call rotation.

  • Technology freedom where it matters (for example a Python ML service beside Java services).

Costs

  • The network is now inside your app. Calls can be slow, time out, or fail halfway. Every call needs timeouts and retry logic.

  • Distributed data. No joins across services and no single ACID transaction across them. Data is often only eventually consistent (see the Consistency Models chapter).

  • Operational overhead. Dozens of deployments, containers, an orchestrator such as Kubernetes, CI/CD per service, service discovery, an API gateway, distributed tracing, centralised logs.

  • Harder debugging and testing. One user request may cross ten services; you need trace IDs to follow it (distributed tracing is covered later in Monitoring, Logging & Observability).

  • Versioning. Changing an API means coordinating with every caller.

Worked example: what the network costs

If a request must pass through several services in series and each is 99.9% available, the total availability is the product (as in the Latency, Throughput & Availability chapter). Network hops also add latency, unless calls can be made in parallel.

# A request that must pass through n services in a chain, each 99.9% available
per_service = 0.999
minutes_per_month = 30 * 24 * 60
for n in (1, 5, 10, 20):
    a = per_service ** n
    down = (1 - a) * minutes_per_month
    print(f"{n:>2} services in series: availability {a:.4%}, downtime ~{down:,.0f} min/month")

# latency: each network hop adds ~2 ms on top of the work itself
hop_ms, work_ms = 2, 5
print("monolith call chain of 5 steps :", 5 * work_ms, "ms (in-process calls)")
print("5 sequential microservice calls:", 5 * (work_ms + hop_ms), "ms")
print("5 parallel microservice calls  :", work_ms + hop_ms, "ms (fan-out, wait for slowest)")

Output:

 1 services in series: availability 99.9000%, downtime ~43 min/month
 5 services in series: availability 99.5010%, downtime ~216 min/month
10 services in series: availability 99.0045%, downtime ~430 min/month
20 services in series: availability 98.0189%, downtime ~856 min/month
monolith call chain of 5 steps : 25 ms (in-process calls)
5 sequential microservice calls: 35 ms
5 parallel microservice calls  : 7 ms (fan-out, wait for slowest)

A chain of 20 "three nines" services is down about 14 hours a month. That is why microservice designs keep call chains short, call services in parallel where possible, cache, and use asynchronous messages (see Message Queues & Pub/Sub) so that a failing service does not block the user.


Side-by-Side Comparison

Aspect

Monolith

Microservices

Deployment

One unit; all or nothing

Each service independently

Scaling

Whole app together

Per service

Data

Usually one shared database

Database per service

Transactions

Local ACID transactions

Sagas, eventual consistency

Internal communication

In-process function calls

Network: REST, gRPC, messages

Failure isolation

Weak

Strong, if designed for it

Operational complexity

Low

High

Debugging

Stack trace in one process

Distributed tracing across services

Best for

Small/medium teams, new products, unclear domain

Many teams, mature domain, very different scaling needs


Designing Microservices Well

1. Draw boundaries around business capabilities

Split by what the business does (ordering, payments, delivery), not by technical layer (a "database service" and a "UI service" is a bad split, because every feature change touches both). Domain-Driven Design calls such a boundary a bounded context. A good boundary means most changes touch only one service.

Conway's law says a system's structure tends to mirror the communication structure of the organisation that builds it. Microservices work best when service boundaries match team boundaries.

2. Database per service

Each service owns its data, and other services access it only through its API or events, never by querying its tables. Otherwise a schema change in one service silently breaks another, and you lose independent deployment.

3. Prefer asynchronous communication between services

Synchronous calls (REST, gRPC; compared in detail in the next chapter, API Design: REST, GraphQL & gRPC) are right when the caller needs an answer now. For "something happened, react to it", publish an event. This keeps services working even when others are down.

4. Design for failure

  • Timeouts on every remote call; never wait forever.

  • Retries with exponential backoff and jitter, only for idempotent operations.

  • Circuit breaker: after many failures, stop calling the sick service for a while and return a fallback, so threads are not all stuck waiting.

  • Bulkheads: separate thread or connection pools per dependency, so one slow dependency cannot exhaust everything.

  • Graceful degradation: if Recommendations is down, show "popular items" instead of an error page.

The anti-pattern: the distributed monolith

If services share one database, must be deployed together, or every request calls five others synchronously, you have all the costs of microservices and none of the benefits. This is called a distributed monolith, and it is worse than a plain monolith.


Transactions Across Services: the Saga Pattern

Placing an order touches three services: Inventory (reserve stock), Payments (charge), and Orders (confirm). In a monolith this is one database transaction. With database-per-service it cannot be. Two-phase commit (2PC) across services is possible in theory, but it locks resources across the network and blocks if the coordinator fails, so it is rarely used between microservices.

A saga is a sequence of local transactions, one per service. If a step fails, the saga runs compensating transactions that semantically undo the steps already completed.

Compensation is not a rollback: the charge really happened, so the compensation is a refund. Between steps, other users can see intermediate states (stock reserved but order not yet confirmed), so sagas give eventual consistency, not isolation.

There are two ways to coordinate a saga:

Style

How it works

Good

Bad

Choreography

Each service listens for events and publishes the next one ("StockReserved" → Payments charges → "PaymentDone" → ...).

No central component; loose coupling

The overall flow is hard to see and debug when there are many steps

Orchestration

A central orchestrator tells each service what to do and decides on compensation.

The flow is explicit in one place

The orchestrator is extra logic to build and keep available

Worked example: an orchestrated saga

Each step has an action and a compensation. The orchestrator runs actions in order; on failure it runs the compensations of completed steps in reverse.

class StepFailed(Exception):
    pass

# Each service owns its own data. No shared database, no distributed transaction.
inventory = {"phone": 1}
wallet = {"asha": 500}
orders = {}

def reserve_stock(o):
    if inventory[o["item"]] < 1:
        raise StepFailed("out of stock")
    inventory[o["item"]] -= 1

def release_stock(o):
    inventory[o["item"]] += 1

def charge(o):
    if wallet[o["user"]] < o["price"]:
        raise StepFailed("insufficient balance")
    wallet[o["user"]] -= o["price"]

def refund(o):
    wallet[o["user"]] += o["price"]

def create_order(o):
    orders[o["id"]] = "CONFIRMED"

def cancel_order(o):
    orders[o["id"]] = "CANCELLED"

# (action, compensation) pairs, run in order by the orchestrator
SAGA = [(reserve_stock, release_stock), (charge, refund), (create_order, cancel_order)]

def place_order(o):
    done = []
    for action, compensate in SAGA:
        try:
            action(o)
            done.append(compensate)
            print(f"  ok   {action.__name__}")
        except StepFailed as e:
            print(f"  FAIL {action.__name__}: {e}")
            for comp in reversed(done):          # undo completed steps, newest first
                comp(o)
                print(f"  undo {comp.__name__}")
            orders[o["id"]] = "REJECTED"
            return

def print_state():
    print(f"  state: stock={inventory}, wallet={wallet}, orders={orders}")


print("Order 1: phone for 800 (balance 500)")
place_order({"id": 1, "user": "asha", "item": "phone", "price": 800})
print_state()
print("Order 2: phone for 400")
place_order({"id": 2, "user": "asha", "item": "phone", "price": 400})
print_state()

Output:

Order 1: phone for 800 (balance 500)
  ok   reserve_stock
  FAIL charge: insufficient balance
  undo release_stock
  state: stock={'phone': 1}, wallet={'asha': 500}, orders={1: 'REJECTED'}
Order 2: phone for 400
  ok   reserve_stock
  ok   charge
  ok   create_order
  state: stock={'phone': 0}, wallet={'asha': 100}, orders={1: 'REJECTED', 2: 'CONFIRMED'}
  • Order 1: stock is reserved, the charge fails, so the orchestrator releases the stock. The system ends where it started.

  • Order 2: all three local steps succeed.

In a real system each step is a remote call or message, the orchestrator saves its progress durably (so it can resume after a crash), and every action and compensation is idempotent because it may be retried. Workflow engines such as Temporal or AWS Step Functions are often used as orchestrators.


Migrating: the Strangler Fig Pattern

Rewriting a big monolith from scratch is risky. The strangler fig approach (named by Martin Fowler after a vine that slowly grows around a tree) migrates gradually:

  1. Put a routing layer (proxy or API gateway) in front of the monolith.

  2. Build one capability as a new service, for example Notifications.

  3. Route that capability's traffic to the new service; everything else still goes to the monolith.

  4. Repeat, one capability at a time, until the monolith is small or gone.

           ┌──→ /notifications/*  ──→ [ Notification service ]  (new)
Clients ──→ Gateway
           └──→ everything else   ──→ [ Monolith ]              (shrinking)

The gateway and how it finds services are covered in a later chapter, API Gateway & Service Discovery.


How to Choose

Situation

Lean towards

New product, small team (under ~10 engineers), requirements changing weekly

Monolith (ideally modular)

Domain boundaries still unclear

Monolith: wrong service boundaries are very expensive to move

Many teams blocked on each other's releases

Microservices

One component has very different load or hardware needs (video transcoding, ML inference)

Extract that one component as a service

Strong need for fault isolation between features

Microservices

No CI/CD, monitoring or container platform yet

Monolith, until that foundation exists

A widely shared piece of advice is "monolith first": start with a well-structured monolith, learn where the real boundaries are, and extract services when a concrete pain (team scaling, independent scaling, fault isolation) justifies the cost.


Common Mistakes

  • Choosing microservices for a college project or an early startup because big companies use them. You pay the full distributed-systems cost with none of the organisational benefit.

  • Sharing one database between services. This creates a distributed monolith.

  • Long synchronous call chains. Latency adds up and availability multiplies down.

  • Splitting by technical layer instead of business capability.

  • Expecting ACID across services. Use sagas and design for eventual consistency.

  • Saying "a monolith cannot scale". It can scale horizontally behind a load balancer; what it cannot do is scale or deploy parts independently.


Interview Questions

Q1. What are the main advantages and disadvantages of microservices?

Advantages: independent deployment, per-service scaling, fault isolation, team autonomy, freedom to pick technology per service. Disadvantages: network latency and partial failures, distributed data with no cross-service ACID transactions, much higher operational complexity (deployment, discovery, monitoring, tracing), harder testing and debugging, and API versioning across teams.

Q2. Why should each microservice have its own database?

So that services stay loosely coupled. If two services share tables, a schema change in one can break the other, they must be deployed together, and you lose independent scaling and ownership. With database-per-service, other services access data only through the owner's API or events, and each team can change its storage freely.

Q3. How do you implement a transaction that spans the Order, Payment and Inventory services?

With a saga: a sequence of local transactions, each in one service. If a later step fails, compensating transactions undo earlier steps (release stock, refund payment). Coordinate it by choreography (services react to each other's events) or orchestration (a central coordinator). Make every step and compensation idempotent, and use the outbox pattern so events are published reliably.

Q4. What is a distributed monolith?

A system split into services that are still tightly coupled: they share a database, must be released together, or depend on long chains of synchronous calls. It has the network and operational costs of microservices but none of the benefits of independent deployment and scaling.

Q5. Five services are called in series for each request, each 99.9% available. What is the end-to-end availability?

0.9995 ≈ 0.99501, about 99.5%, or roughly 216 minutes of downtime in a 30-day month, five times the downtime of a single service. Shorter chains, parallel calls, caching and asynchronous messaging reduce this effect.


Summary

  • A monolith is one deployable unit; microservices are independently deployable services, each owning one business capability and its own data.

  • Monoliths are simpler, faster internally, and have easy transactions. Microservices give independent deployment, scaling, fault isolation and team autonomy.

  • Microservices pay with network failures, distributed data, and heavy operational tooling. Availability multiplies down along call chains.

  • Use database per service, boundaries around business capabilities, async events where possible, and timeouts, retries, circuit breakers everywhere.

  • Cross-service transactions use sagas with compensating actions.

  • Start with a modular monolith; migrate gradually with the strangler fig pattern when real pain appears.

Microservices are a solution to an organisational scaling problem. If you do not have that problem yet, you are only buying the costs.