How services find each other when machines and addresses keep changing.
Service discovery is how one service finds the current address (IP and port) of another service, when those addresses keep changing.
In a monolith, one part calls another by a normal function call. In microservices, the Orders service must call the Payments service over the network. To do that it needs an address, such as 10.0.3.17:8080.
You could write that address in a config file. But in the cloud there may be ten Payments instances today and thirty tomorrow. Containers get new IP addresses each time they restart. Autoscaling adds and removes machines. A fixed list is wrong within minutes.
The fix is a service registry: a small, fast database of "service name β list of live instances". Instances register when they start, send heartbeats while they run, and are removed when the heartbeats stop (or when they shut down cleanly). Callers use the name ("payments"), and discovery turns it into a real address.
| Style | How it works | Good | Bad |
|---|---|---|---|
| Client-side | The caller queries the registry and chooses an instance (Netflix Eureka with Ribbon, gRPC client-side balancing) | One less hop. Smart choices are possible. | Every language needs discovery code. Clients must be kept up to date. |
| Server-side | The caller sends to a fixed address. A load balancer or proxy looks up the instances (AWS ALB, Kubernetes Service, NGINX) | Callers stay simple and language-free. | An extra hop, and the proxy must be highly available. |
| Pattern | Who registers | Example |
|---|---|---|
| Self-registration | The service registers and sends heartbeats itself | A Spring Boot app with Eureka client |
| Third-party registration | A separate agent watches the platform and updates the registry | Kubernetes (the control plane tracks Pods), Registrator for Docker |
| Tool | Notes |
|---|---|
| Consul | Registry with health checks, key-value store and DNS interface |
| Eureka | Netflix registry. Favours availability, so it can show stale data during a network split. |
| etcd / ZooKeeper | Strongly consistent stores. Often used under the hood (Kubernetes uses etcd) or for leader election. |
| Kubernetes DNS | Built in. A Service name resolves to a stable virtual IP or to the Pod addresses. |
| Cloud Map / DNS-based | Managed services from cloud providers, or plain DNS with a short TTL. |
A simplified model. Running instances send heartbeats all the time. A crashed one stops, and the registry removes it after the timeout. The client rotates through its targets in order (round-robin). A real client may also retry on another instance, which hides most of the stale window.
| Term | Meaning in easy words |
|---|---|
| Service registry | A database of service names and their live instance addresses |
| Registration | An instance adds its name and address to the registry |
| Deregistration | Removing an instance from the registry, on shutdown or after it fails |
| Heartbeat | A small, regular message that says the instance is still alive |
| TTL (time to live) | How long the registry waits without a heartbeat before it removes an instance |
| Health check | A test that shows whether an instance can do real work, not just that it runs |
| Client-side discovery | The caller queries the registry and picks an instance itself |
| Server-side discovery | A load balancer or proxy queries the registry for the caller |
| Service name | A stable logical name (payments) that callers use instead of an IP address |
| Stale entry | A registry record for an instance that is already gone |
| Readiness vs liveness | Ready: can take traffic now. Live: the process is running and not stuck. |
| Choice | We gain | We lose |
|---|---|---|
| Short heartbeat timeout | Dead instances disappear quickly | Healthy instances can be removed by a short network glitch |
| Long heartbeat timeout | Stable list, few false removals | Callers hit dead instances for longer |
| Client-side discovery | No extra hop, smart routing | Library code in every service |
| Server-side discovery | Simple clients | An extra network hop and a proxy to maintain |
| Consistent registry (etcd, ZooKeeper) | Everyone sees the same list | May refuse requests during a network split |
| Available registry (Eureka style) | Always answers, even if the data is old | Callers may see outdated instances |