System Design · 06. Architecture · 8 min · Intermediate
Service Discovery
How one service finds the live addresses of another when machines come and go.
In short
Addresses are temporary, so callers look services up by name. Instances register themselves and send heartbeats; a registry keeps the live list.
Client-side discovery has no extra hop but needs library code; server-side and DNS keep callers simple; a mesh moves it all into proxies. Each one is a different answer to how quickly a change is noticed: 15 to 45 s here, and retries cover the gap.
Key ideas
- In the cloud, addresses are temporary. Callers must look them up, not remember them. (step 2)
- The service name stays fixed; the addresses behind it change freely. (step 3)
Good to know
- Fast and simple on the network, but the discovery logic is copied into every service. (step 5)
- This is what Kubernetes Services and cloud load balancers do for you. (step 6)
Watch out
- A hard-coded address is a promise that the machine at that address will exist forever. (step 1)
- A shorter heartbeat finds failures faster but adds traffic and can drop a healthy but busy instance. Callers should also retry on another instance. (step 4)
- A low TTL (5 to 30 s) helps, but cannot be trusted on every client. DNS also gives no health or load information. (step 7)
- A mesh is powerful but heavy: another layer to run, learn and debug. Use it when many services need the same rules. (step 8)
Key numbers
- Change noticed in
- 15–45 s
- Instances up
- 3
Terms
- Service discovery
- Finding the current addresses of a service by name instead of keeping them in a file.
- Service registry
- A small, highly available database of service names and the addresses of their live instances.
- Self-registration
- An instance adds itself to the registry when it starts.
- Heartbeat
- A regular signal from an instance saying it is alive; missed ones get it removed from the registry.
- Client-side discovery
- The caller asks the registry for the list and chooses an instance itself.
- Server-side discovery
- The caller uses one stable address; a load balancer finds the live instances.
- DNS-based discovery
- A service name resolves through DNS to the addresses of live instances, cached for the TTL.
- Service mesh
- A proxy beside each service handles lookup, retries, encryption and metrics, driven by a control plane.