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.
Example
// In Kubernetes, the "orders" service in the "shop" namespace gets a stable DNS name,
// which always points at its healthy copies, wherever they run now
const order = await fetch("http://orders.shop.svc.cluster.local/api/orders/42").then((r) => r.json());
// With Consul, you can also ask the registry which copies are healthy
const healthy = await fetch("http://localhost:8500/v1/health/service/orders?passing=true").then((r) => r.json());
console.log(healthy.map((entry) => `${entry.Service.Address}:${entry.Service.Port}`));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. 28Microservices are an architectural style where an application is split into small, independently deployable services that communicate over a network.
- Load BalancerDevOps & Cloud, p. 40A 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. 37Kubernetes is an open-source system that automates deploying, scaling, and managing containerized applications across a cluster of machines.
- Service MeshDevOps & Cloud, p. 55A service mesh is an infrastructure layer that manages traffic between microservices, adding encryption, retries, routing, and monitoring without code changes.
- Health CheckBackend & APIs, p. 24A 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. 19DNS is the internet's naming system that translates human-readable domain names like example.com into the numeric IP addresses computers use to connect.
Free · once a week
Software and AI, explained once a week
The week's software and AI news with every term explained, plus a term to learn and a quick quiz. A two-minute read.
See this week's issueAbout this page
Page history
Added a code example: finding a service by name instead of address.
Changes that altered what this page says. Typo fixes are not listed.
Spotted a mistake or something missing on this page?Suggest an edit
How pages are made