Service Discovery

Client-side vs server-side discovery, DNS-based discovery in Kubernetes, registries like Consul and Eureka, and health-aware routing.

What it is

In dynamic environments, instances come and go (autoscaling, deployments, failures), so callers cannot hard-code addresses. Service discovery maps a logical service name to the current healthy instances.

Pattern How Examples
Server-side discovery Caller hits a stable address (LB/VIP); the LB knows instances Kubernetes Service (ClusterIP), AWS ALB target groups, Cloud Map + ALB
Client-side discovery Caller queries a registry and load balances itself Netflix Eureka + Spring Cloud LoadBalancer, Consul, gRPC xDS
DNS-based Service name resolves to instance or VIP addresses Kubernetes CoreDNS, AWS Cloud Map, Consul DNS
Mesh-based Sidecar or proxyless clients get endpoints from a control plane Istio, Linkerd, Consul Connect, Cloud Service Mesh
flowchart LR
  subgraph K8s cluster
    A[order-svc pod] -->|http://payment-svc| DNS[CoreDNS]
    DNS --> SVC[payment-svc ClusterIP]
    SVC --> P1[payment pod 1]
    SVC --> P2[payment pod 2]
    SVC --> P3[payment pod 3]
    CP[Endpoints controller<br/>watches readiness] -.updates.-> SVC
  end

Real-world example

On Kubernetes, order-service calls http://payment-service.payments.svc.cluster.local. CoreDNS resolves the Service name; kube-proxy or the eBPF data plane (e.g. Cilium) routes to ready pods. Across clusters or to VMs, a mesh or AWS Cloud Map/Consul provides a shared registry.

Advantages and disadvantages

Approach Advantages Disadvantages
Platform (K8s Service, LB) No client libraries, language-neutral Extra hop for some data planes; limited LB algorithms
Client-side registry Smart routing, no extra hop Library per language, registry is critical
Mesh Rich traffic control, mTLS Operational complexity, resource overhead

Architect-level considerations

[!ARCHITECT] On Kubernetes, use native Services and DNS; adding Eureka on top is usually redundant. Use a registry or mesh when spanning clusters, VMs and multiple platforms.

Interview questions and answers

Is Eureka still needed with Kubernetes?

Generally no. Kubernetes provides discovery and load balancing through Services. Spring Cloud Kubernetes can integrate config and discovery if needed.

How does discovery interact with health checks?

Only ready instances are registered or receive traffic. Readiness probes in Kubernetes remove pods from Service endpoints; registries use heartbeats or health checks and deregister failed instances.