Module 2 · Backend architectureLesson 16 of 886 minIntermediate
Service discovery
Servers come and go all the time. Service discovery keeps an up-to-date list so services can find each other.
Play "Look up and call", then "Instance dies". Watch the registry notice the missing heartbeats and stop handing out the dead address.
In the cloud, servers are not pets with permanent addresses. Autoscaling adds and removes them, deploys replace them, and crashes take them away. Hard-coding payment = 10.0.1.9 in a config file breaks within hours.
Service discovery keeps a live list of where each service is running.
How it works
- Register: when a payment instance starts, it tells the registry "payment is at 10.0.1.9".
- Heartbeat: every few seconds it says "still alive".
- Expire: if heartbeats stop, the registry removes the entry.
- Look up: when orders needs payment, it asks the registry and gets the current list.
Client-side vs server-side discovery
- Client-side: the caller asks the registry and picks an instance itself. Fewer hops, but every client needs the logic.
- Server-side: the caller sends to a load balancer or a fixed name, and that layer asks the registry. Clients stay simple.
Tools you'll hear about
- Consul, etcd and ZooKeeper: registries with health checks.
- Kubernetes: every service gets a stable DNS name, and Kubernetes keeps the list of healthy pods behind it updated.
- Service meshes (like Istio or Linkerd) add discovery, retries and encryption through a small proxy next to each service.
Watch out for
The registry is now something everyone depends on, so it runs as a replicated cluster. Clients also cache results briefly, so a short registry outage doesn't stop all traffic.