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.9 What does "database per service" mean?
3 min read+10 XP
In the restaurant: Each stall has its own pantry. The biryani stall doesn't walk into the dosa stall's pantry to take rice; it asks, or keeps its own rice. That's what lets each stall change its recipes freely.
In the system: Each service owns its data and is the only one that reads or writes it directly. Others go through its API or its events.
Figure: own your data; share it through APIs and events.
Why:
- A team can change its schema, pick its own store (PostgreSQL for orders, Elasticsearch for search, Redis for carts) and scale it, without breaking anyone.
- A shared database quietly couples every service: one team's migration breaks another's queries, and nobody can change anything.
What it costs, and the usual answers:
- No cross-service joins. "Order history with book titles" needs data from orders and catalogue:
- API composition: fetch orders, then fetch the titles in one batched call. In the chapter's code, 50 orders with one catalogue call each take about 150 ms; one batched call takes about 3 ms.
- Keep a copy: orders stores the title at purchase time, or keeps a read model updated by
book_updatedevents (Chapter 23).
- No cross-service transactions. Use sagas with compensations (Chapter 17), the outbox to publish events reliably (Chapter 23), and idempotent consumers (Chapter 22).
- Duplicated data is normal and fine, as long as one service is clearly the source of truth.
A pragmatic middle step: one database server, but separate schemas per service with separate credentials, so no one can read another's tables.
Memory code: "Each stall keeps its own pantry and asks politely for the rest."