27. Monolith vs Microservices
1
The picture to keep in your head2
27.1 What is a monolith?3
27.2 What are a monolith's strengths?4
27.3 What is a modular monolith?5
27.4 What are microservices?6
27.5 What is the real cost of microservices?7
27.6 How do you draw service boundaries?8
27.7 How does service discovery work?9
27.8 Sync vs async calls between services: when to use which?10
27.9 What does "database per service" mean?11
27.10 When should you split?12
Memory card and where the analogy breaks13
Practice: five levels14
Interview questions15
Solutions16
Sources17
Code lab27. Monolith vs Microservices
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Monolith, modular monolith, or microservices? One line each.
- A 4-person startup building the first version of the bookstore.
- A 60-engineer company where 8 teams block each other's releases daily.
- A 15-engineer team with a messy monolith where every change breaks something else.
- A payments feature that needs separate audits and stricter deploy controls.
B. True or false? Explain in one line.
- A monolith can't scale beyond one server.
- Microservices make cross-service transactions easy.
- Two services that always deploy together are a sign of a wrong boundary.
- Each microservice should own its data.
- Every call between services should be synchronous so errors are visible.
Level 2: Calculate
- A request passes through 6 services, each available 99.9% of the time. What's the combined availability, and roughly how many minutes of failure in a 30-day month?
- A page calls 10 services in parallel; each is slow 1% of the time. What fraction of page loads hits at least one slow call? With 100 calls?
- Order history shows 50 orders; each needs a catalogue lookup taking 3 ms. How long one by one, and with one batched call?
- One order involves 21 calls between components. Under the two splits in 27.6, how many cross the network?
- The catalogue becomes slow (5 s per call). The order service gets 100 requests a second and has 200 threads. With no timeout, how many threads are busy? With a 300 ms timeout?
Level 3: Build
In Python:
chain_availability(per_service, services)andp_any_slow(p, fanout).boundary_violations(imports)for a modular monolith with declared allowed dependencies.cross_calls(split)over a call graph, comparing two service splits.- A
Registrywith heartbeats, TTL expiry and round-robinpick. place_order_syncvsplace_order_asyncwhen the email service is down.
Level 4: Debug
The bookstore split out a catalogue service. During a catalogue slowdown, checkout went down too, even though checkout only needs the catalogue for book titles on the confirmation page. The order service code:
python
- Explain how a slow catalogue took checkout down.
- Fix it: name at least three changes.
Level 5: Design
The bookstore has a 60-engineer, 8-team organisation and a monolith. Search and recommendations need far more machines than the rest; payments needs separate audits; teams block each other's deploys.
Deliver:
- Your target architecture: what stays together, what's split out, and why.
- The migration plan, step by step.
- How services find each other and communicate (sync and async).
- How data is split, and how "order history with titles" and "place an order" work afterwards.
- What you'll measure to know the change worked.