Lesson 8 of 17
27. 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?"

instances heartbeat to a registry; the orders service looks up catalog and calls a healthy instance.

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.5 stops heartbeating and drops out; calls go to 10.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.internal resolves 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."