Handbooks / System Design / Chapter 10
Microservices and Service Communication
7 pages · ~5 min✓ Reviewed
Builds on CAP, Consistency and Reliability. Next up: Distributed Transactions and Consensus.
Monolith vs Microservices: The Mental Model
1. Two Shapes
- Monolith: one deployable, one codebase, one database
- Module calls are in-process functions: fast, transactional
- Microservices: many small, independently deployable services
- Each owns one business capability and its own data
- Services talk over the network
2. The Trade
| Monolith | Microservices | |
|---|---|---|
| Deploy | All-or-nothing | Per service |
| Scaling | Whole app | Per service |
| Tech stack | One | Polyglot |
| Debugging | One stack trace | Distributed tracing |
| Transactions | ACID | Sagas |
Microservices swap code complexity for distributed-systems complexity: network failures, latency, partial failure, eventual consistency.
3. Modular Monolith
- One deployable with strict module boundaries
Service Boundaries and Database per Service
1. Drawing the Boundary
| Split by | Example | Verdict |
|---|---|---|
| Business capability | Orders, Payments, Inventory | Good |
| Technical layer | UI, DB, logic | Bad |
- Bounded context (DDD) = one business capability
Service Discovery and Sync vs Async Calls
1. Service Discovery
- Instances come and go with autoscaling
- Callers look up healthy addresses by service name
- Registry: Consul, Eureka
- Platform DNS: Kubernetes Service name
| Client-side | Server-side | |
|---|---|---|
| Resolves | Caller queries registry |
Timeouts and Resilience
1. Timeouts on every call
- Set a timeout on every remote call
- Default is often infinite: one slow dependency eats all threads, outage cascades
- Base it on the dependency's observed p99 latency
- Caller timeout > callee timeout; total fits the user deadline
requests.get(url, timeout=(1, 2))
connect 1s, read 2s
2. Retries and circuit breaker
- Retry only idempotent calls
Service Mesh and Sidecars
1. Mesh and Sidecar
- Service mesh moves networking out of app code into the infrastructure layer
- Covers mTLS, retries, timeouts, load balancing, metrics, tracing
- Sidecar: a proxy container (Envoy) beside every service instance
- It intercepts all inbound and outbound traffic
- App Ano network code
- Sidecaroutbound
- Sidecarinbound
- App Bno network code
Strangler Fig Migration, When Not to Use, and Cheat Sheet
1. Strangler Fig
- Facadegateway or proxy
- Extractone capability
- Reroutethat path
- Retireempty monolith
2. Migration Order
- Pick a low-risk, loosely coupled capability first
- Run old and new in parallel
- Shift traffic gradually
- Then delete the old code
Check yourself
Service A calls B with a 2s timeout. B calls C with a 3s timeout. What goes wrong?
- A gives up at 2s, but B keeps waiting on C for up to 3s.
- That work is wasted, and A's retries pile more load onto B.
- Fix: the caller's timeout must be greater than the callee's.
Three services are called in a sync chain, each 99% available. About how available is the chain?
- 0.99 × 0.99 × 0.99 ≈ 97%.
- Sync calls multiply availability, so every extra hop lowers it.