Skip to main content
App basics: HTTP, auth, dataLesson 11 of 11

Monolith, Modular Monolith and Microservices

A shop starts as one application. As it grows, someone suggests splitting it into services. Follow the same checkout request through a monolith, a modular monolith and microservices, and see what each split buys you and what it costs.

Deploy units1
Internal callFunction call
DataOne DB, one transaction
Ops load1 pipeline
Monolith · one deployMicroservicesShopperCheckoutShop appOrders·pay·stockShared DBAll tablesAPI gatewayRoutes by pathOrdersTeam APaymentsTeam BStockTeam COrders DBOwned by APayments DBOwned by BStock DBOwned by CPOST /checkout1 transaction
RequestAppears in a later step

What you will learn

  • What a monolith and a modular monolith are, and why they work
  • What microservices buy: independent deploys, scaling, ownership
  • Database per service, and why a shared database couples services
  • How call chains multiply latency and failure
  • Strangler fig migration, and when to stay a monolith

Gains and costs​

MonolithModular monolithMicroservices
Deploy unitOneOneOne per service
Internal callsFunction callsFunction calls through module interfacesNetwork calls (HTTP, gRPC, events)
DataOne database, shared tablesOne database, tables owned per moduleDatabase per service
TransactionsLocal, across everythingLocal, across modulesOnly inside a service; sagas across
ScalingWhole appWhole appPer service
Team independenceLowMediumHigh, if data and APIs are separate
DebuggingOne process, one logOne process, one logDistributed tracing needed
Ops costLowLowHigh: pipelines, dashboards, on-call per service

Try it yourself​

# Availability of a chain: 5 required services at 99.9% each
python3 -c 'print(round(0.999**5 * 100, 2), "%")' # about 99.5%

# Chance that a request needing 10 calls hits at least one 1%-slow call
python3 -c 'print(round((1 - 0.99**10) * 100, 1), "%")' # about 9.6%

# How many deploy units, and does each ship on its own?
kubectl get deploy -n shop
kubectl rollout history deploy/payments -n shop

# Do services share a database? Compare the DB host each one is given
kubectl get deploy -n shop -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.template.spec.containers[*].env[?(@.name=="DB_HOST")].value}{"\n"}{end}'

# How long does one checkout take end to end?
curl -s -o /dev/null -w 'total %{time_total}s\n' https://shop.example.com/checkout

Check yourself

  1. Three microservices read and write the same tables in one shared database. A team renames a column. What happens?
  2. A checkout request calls five services in a chain, each 99.9% available, and all are required. Roughly how available is checkout?
  3. A team of 6 is building a new product whose domain is still changing every month. What is usually the best start?

Further reading: Microservices (Lewis and Fowler) · MonolithFirst · Strangler Fig Application · Database per service (microservices.io) · Saga (microservices.io) · Designing HTTP APIs · Making Service Calls Survive Failures · How Logs, Metrics and Traces Leave a Pod