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
27.10 When should you split?
3 min read+10 XP
Deciding
In the restaurant: A small restaurant with three cooks doesn't need a food court. But when there are forty cooks, ten menus and constant collisions, splitting into stations, and later into stalls, starts to pay off.
In the system: Split for organisational and operational reasons you can name, not because microservices are fashionable.
Good reasons to extract a service:
- Team scaling: many teams blocking each other in one deploy pipeline. (The bookstore at 60 engineers, not at 5.)
- Different scaling needs: search needs 50 machines at peak; the rest needs 5.
- Different reliability or security needs: payments needs strict isolation, audits and its own release process.
- Different technology: recommendations needs a Python ML stack; the rest is Java.
- Fault isolation: a crash-prone feature shouldn't take down checkout.
Bad reasons:
- "Microservices are best practice."
- "Our monolith's code is messy" (fix the modules first; a messy monolith becomes messy services plus network calls).
- "We might need to scale someday."
How to split safely:
- Start with a (modular) monolith. Martin Fowler's "MonolithFirst" argument: boundaries are easier to find once the domain is understood.
- Enforce module boundaries inside the monolith first (27.3).
- Extract one service at a time, starting with a well-bounded, high-value candidate (payments, search). Use the strangler fig pattern: route some traffic to the new service, grow it, retire the old code.
- Have the platform ready: CI/CD per service, observability and tracing, service discovery, and on-call ownership.
- Measure: deploy frequency, lead time, incidents. If they don't improve, rethink.
In interviews, the strongest answer is rarely "microservices" or "monolith". It's "a modular monolith, with these one or two services split out, for these reasons".
Memory code: "Split for teams and needs you can name, one stall at a time."