Handbooks / Microservices / Chapter 1
Fundamentals and Decomposition
7 pages · ~5 min✓ Reviewed
Start here. Next up: Service Communication.
MicroservicesPage 1Monolith vs Microservices: The Mental Model
1. Two Shapes
- Monolith: one deployable, one process, usually one database
- Modules call each other in-process
- Microservices: small services, each owns one business function and its data
- They talk over the network: HTTP, gRPC, events
3. Modular Monolith
- One deployable with strict module boundaries
- The usual best starting point
- Cheapest path to splitting later
DefaultStart with a modular monolith. Extract a service only when a clear pain appears.
MicroservicesPage 2Bounded Contexts and Business Capabilities
1. Bounded context
- Bounded context (DDD): a boundary where a domain model and its terms have one consistent meaning
- Same word, different models: each context owns its own version
2. Drawing the boundary
- Decompose by business capability: Orders, Payments, Inventory, Shipping
- Capability = what the business does
- Not by technical layer: UI service, DB service
- Size rule: one team owns it, changeable with no coordinated release
MicroservicesPage 3Database per Service and Data Consistency
1. Own Your Data
- Database per service: only the owner touches its data
- Others use its API or events, never its tables
- Shared DB couples services by schema
- One column change breaks several teams
- Blocks independent deploys
2. Queries Across Services
3. Saga and Eventual Consistency
Saga: local transactions, each with an undo- 1Reserve stockundo: release
- 2Charge paymentundo: refund
- 3Ship orderundo: cancel
- Choreography: services react to each other's events
- Orchestration: a coordinator commands each step
MicroservicesPage 4Strangler Fig Migration
1. The Pattern
- Strangler fig: a facade (API gateway or reverse proxy) sits in front of the monolith
- Route traffic to new services one capability at a time
- Old code shrinks until nothing is left to serve
- Client
- Facadegateway / proxy
- New servicemigrated slice
- Monolitheverything else
2. The Loop
- 1Identify a seam
- 2Build new service
- 3Route a slicesmall % of traffic
- 4Verify
- 5Retire old codethen repeat
- First slice: low coupling, clear value (notifications, search)
- Leave the tangled core for last
MicroservicesPage 5Common Mistakes and Trade-offs
1. Splitting Mistakes
- Distributed monolith: services must deploy together or share a DB
- Pays all the cost of microservices, gets none of the benefit
- Split too early: guessed boundaries are costly to move
- Start with a modular monolith
2. Chatty Calls
- Clientone request
- Orderssync hop
- Pricingsync hop
- Stocksync hop
- Taxsync hop
- Each sync hop adds latency and failure rate
- Five hops at 99.9% each give about 99.5% available
- Fix: coarser services or async events
MicroservicesPage 6Cheat Sheet: Monolith vs Microservices
1. Default Path and Boundary Test
- Start with a modular monolith
- Extract only for a clear pain: scaling, team conflict, deploy cadence
- Boundary test: one team, one capability, one bounded context, one data store
2. Data Rules
- Own your data: only the owner touches its tables
- No shared tables, no cross-service joins
3. Migration Recipe
- 1Facadegateway or proxy
- 2New service
- 3Route slice
- 4Verify
- 5Retire old code
- 6Data ownershiplast
MicroservicesPage 7Check yourself
Orders and Payments share one database, each with its own deploy pipeline. Payments renames a column. What happens, and what is this called?
- Orders breaks, so the two must coordinate releases
- A distributed monolith: microservice costs, no independent deploys
- Fix: Payments owns its data; Orders uses its API or events
Checkout calls Payments, which calls Inventory. Inventory fails after Payments charged the card. How do you restore consistency?
- No cross-service ACID transaction exists
- Use a saga: run a compensating action, such as a refund
- Orchestrate via a coordinator or choreograph via events
A team of 4 engineers has an unclear domain. They plan 12 services. What is wrong, and what is the better start?
- Boundaries guessed early are wrong and costly to move
- More services than engineers: tracing and CI/CD overhead
- Start with a modular monolith, extract only on clear pain