Service Discovery
- Pronunciation
- SUR-viss dih-SKUV-uh-ree
In short
Service discovery is how services in a distributed system find the current addresses of other services, which change as instances start, stop and move.
What is service discovery?
In a microservice system running on containers or autoscaling groups, instances come and go all the time and get new IP addresses. Hard-coding an address such as 10.0.3.17:8080 would break as soon as that instance is replaced. Service discovery maintains an up-to-date list of healthy instances for each service name, such as payments, and lets callers look them up.
At its center is a service registry, such as Consul, etcd, ZooKeeper or Netflix's Eureka. Instances register themselves when they start, or a platform registers them, and health checks remove instances that stop responding. With client-side discovery, the caller asks the registry and picks an instance itself; with server-side discovery, the caller sends the request to a load balancer or router that does the lookup.
Kubernetes builds this in. A Service gives a stable name and virtual IP to a changing set of pods, and the cluster's DNS resolves names such as payments.default.svc.cluster.local, so applications simply call a name. Service meshes go further, handling discovery, load balancing, retries and encryption in sidecar proxies next to each service.
A common misconception is that DNS alone solves discovery. DNS works for names that change rarely, but caching can keep returning dead instances for minutes and it carries no health information. Discovery systems update within seconds and only return instances that pass their health checks.
Key takeaways
- Service discovery finds the current addresses of other services.
- A registry such as Consul or etcd tracks healthy instances by name.
- Client-side discovery picks instances in the caller; server-side uses a router.
- Kubernetes Services and cluster DNS provide discovery built in.
- Plain DNS is too slow and has no health checks for fast-changing systems.
Readers ask
Why do microservices need service discovery?
Because instances are created, destroyed and moved constantly, so their addresses change. Discovery lets services call each other by name and always reach a healthy instance.
What is the difference between client-side and server-side discovery?
In client-side discovery, the calling service queries the registry and chooses an instance. In server-side discovery, it sends requests to a load balancer or router, which queries the registry and forwards the request.
How does Kubernetes do service discovery?
Each Kubernetes Service gets a stable DNS name and virtual IP. The cluster keeps track of which healthy pods back it, and traffic sent to the name is spread across them.
See also
- MicroservicesSoftware Architecture, p. 27Microservices are an architectural style where an application is split into small, independently deployable services that communicate over a network.
- Load BalancerDevOps & Cloud, p. 34A load balancer is a server or service that spreads incoming traffic across several backend servers so no single one is overloaded and the app stays available.
- KubernetesDevOps & Cloud, p. 32Kubernetes is an open-source system that automates deploying, scaling, and managing containerized applications across a cluster of machines.
- Service MeshDevOps & Cloud, p. 48A service mesh is an infrastructure layer that manages traffic between microservices, adding encryption, retries, routing, and monitoring without code changes.
- Health CheckBackend & APIs, p. 21A health check is a small automated test, usually an HTTP endpoint, that reports whether a service is up and able to handle requests, so failures show fast.
- DNSDevOps & Cloud, p. 16DNS is the internet's naming system that translates human-readable domain names like example.com into the numeric IP addresses computers use to connect.
Spotted a mistake or something missing on this page?Suggest an edit