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.7 How does service discovery work?
3 min read+10 XP
In the restaurant: The food court has a directory board: "Dosa: stall 14". When a stall moves or a new one opens, the board is updated. Customers check the board instead of memorising locations.
In the system: Service instances come and go constantly: deploys, autoscaling, crashes. The orders service can't hard-code the catalogue's IP addresses. Service discovery answers "where is a healthy catalogue instance right now?"
Figure: a registry tracks live instances; callers look up a name, not an address.
- Registration: each instance registers itself (or the platform registers it) and sends heartbeats. Instances that stop heartbeating are removed. In the chapter's code,
10.0.0.5stops heartbeating and drops out; calls go to10.0.0.6. - Client-side discovery: the caller asks the registry and load-balances across instances itself (Eureka-style).
- Server-side discovery: the caller calls a stable name or load balancer, which routes to a healthy instance (Kubernetes Services, cloud load balancers).
- DNS-based:
catalog.internalresolves to current instances (Kubernetes DNS, Consul DNS), with short TTLs (Chapter 4). - Service mesh: a sidecar proxy beside each service handles discovery, retries, timeouts, encryption (mTLS) and metrics, so application code doesn't have to (Istio, Linkerd).
Memory code: "Check the board, not your memory: stalls move."