Service Discovery in Microservices
In a cloud or container environment (Kubernetes, AWS ECS, autoscaling VMs), service instances spin up, crash, redeploy, and get brand-new ephemeral IP addresses every few minutes. Hardcoding IPs in config files breaks immediately. Service discovery solves this by maintaining a live directory — a service registry — of every healthy service instance and its current IP:port, so callers can locate dependencies by logical name (like payment-service) instead of static addresses.
Client-Side vs. Server-Side Discovery
There are two primary ways a caller (Order Service) finds and talks to a healthy instance of Payment Service:
Client-Side Discovery
The calling service embeds a discovery SDK (e.g., Netflix Eureka client, Consul SDK, or a gRPC resolver). It queries the Service Registry, caches the list of healthy Payment IPs locally, picks one using a client-side load-balancing algorithm (round robin, least latency), and calls that IP directly.
- Pros: One fewer network hop (no extra proxy in the middle); caller can implement custom routing (like consistent hashing or zone-aware selection).
- Cons: Couples every microservice to a discovery library — painful in polyglot organizations mixing Go, Java, Python, and Node.js.
Server-Side Discovery
The calling service stays completely dumb — it simply sends requests to a stable virtual address (a Kubernetes Service DNS name, an internal Load Balancer, or a service mesh sidecar like Envoy/Istio). That router queries the registry (etcd, Consul, AWS Cloud Map) and forwards traffic to a healthy instance.
- Pros: Language-agnostic — callers just make plain HTTP/gRPC requests; discovery and routing logic live in one central infrastructure layer.
- Cons: Adds an extra network hop (unless implemented via local node
iptables/eBPF like Kuberneteskube-proxyor Cilium) and makes the router itself a critical component.
| Dimension | Client-Side Discovery | Server-Side Discovery |
|---|---|---|
| Who queries the registry? | Calling service (via SDK) | Load balancer, K8s kube-proxy, or sidecar proxy |
| Extra network hop | None (direct caller → target) | 1 hop (or local kernel/sidecar hop) |
| Polyglot language support | Harder — needs SDK in every language | Effortless — plain DNS / HTTP works everywhere |
| Examples | Netflix Eureka + Spring Cloud, gRPC custom resolver | Kubernetes Services (CoreDNS + etcd), AWS ALB / Cloud Map, Consul + Envoy |
Registration and Health Checking
A service registry is only useful if dead instances disappear fast:
- Self-Registration vs. Third-Party Registration: Either the service registers itself on startup and deregisters on graceful shutdown, or the orchestrator (like Kubernetes’ control plane) automatically registers and removes pods on the service’s behalf (preferred, because app code stays clean).
- Heartbeats & TTL Leases: Each instance periodically sends a heartbeat (
every 5–10s) to renew its lease, or the registry actively probes/healthz(readiness probe). If heartbeats stop, the registry evicts the IP so traffic stops flowing to the dead container. - Graceful Draining: Before shutting down during a deployment, remove the instance from the registry first, wait a few seconds for cached clients and load balancers to stop sending new requests, finish in-flight requests, and then exit.
Consistency Trade-Offs in Service Registries (CP vs. AP)
Because the service registry is itself a distributed system, it must make a CAP theorem choice during network partitions:
- CP Registries (
etcd, Apache ZooKeeper, Consul): Powered by Raft consensus. Guarantees every node sees a consistent view of registered services, at the cost of rejecting new registrations on the minority side of a network split. - AP Registries (Netflix Eureka): Prioritizes Availability — during a partition, each Eureka node keeps serving its last-known registry cache so services can keep calling existing healthy peers even if updates temporarily lag.
FAQs
Why can’t I just use standard DNS for microservice discovery?
Standard DNS works well for coarse external routing (see DNS), but DNS clients often cache records aggressively and ignore low TTLs, meaning traffic keeps hitting crashed containers for seconds or minutes. Modern platforms like Kubernetes combine internal DNS (pointing to a stable virtual IP) with real-time kernel/proxy endpoint tables (etcd → kube-proxy/Envoy) to avoid stale DNS caches.What happens if the service registry itself goes down?
Well-designed discovery clients and proxies cache the last known healthy endpoint list in memory and keep routing using that snapshot if the registry becomes temporarily unreachable. You lose the ability to register new instances while the registry is down, but existing traffic keeps flowing.How does Kubernetes handle service discovery out of the box?
When you create a KubernetesService, the control plane assigns it a stable virtual IP (ClusterIP) and registers its DNS name in CoreDNS (e.g., payment.prod.svc.cluster.local). As pods pass readiness probes, Kubernetes updates EndpointSlices stored in etcd, and kube-proxy (or Cilium eBPF) on every node routes packets directly to healthy pod IPs.How is service discovery different from an API Gateway?
An API Gateway sits at the north-south edge handling external clients (authentication, rate limiting, routing/orders/*). Service discovery operates internally (east-west and behind the gateway) to tell both the gateway and microservices which dynamic IP addresses are currently healthy for a given service name.