LogoMasst Docs

Service Discovery

Understanding service discovery in microservices architecture.

What is Service Discovery?

Service Discovery is the mechanism by which services locate each other in a distributed system. In dynamic environments where services scale up/down and IP addresses change, hardcoding addresses is impractical.


The Problem

Without Service Discovery:
┌─────────┐     ┌─────────────────────┐
│Service A│────►│ 192.168.1.10:8080   │  ← Hardcoded!
└─────────┘     │ (Service B)         │
                └─────────────────────┘

What if Service B scales or moves?

Service Discovery Types

Client-Side Discovery

Client queries registry and chooses which instance to call:

┌─────────┐     ┌──────────────┐
│ Client  │────►│   Registry   │
└────┬────┘     │ Service B:   │
     │          │  - 10.0.0.1  │
     │          │  - 10.0.0.2  │
     │          └──────────────┘

     │ (client chooses)

┌─────────┐
│Service B│
│10.0.0.1 │
└─────────┘

Examples: Netflix Eureka, Consul (client mode)

Server-Side Discovery

Load balancer queries registry and routes request:

┌─────────┐     ┌─────────────┐     ┌──────────┐
│ Client  │────►│Load Balancer│────►│Registry  │
└─────────┘     └──────┬──────┘     └──────────┘


                ┌─────────────┐
                │  Service B  │
                └─────────────┘

Examples: AWS ALB, Kubernetes Services


Service Registry

Central database of service instances:

ServiceInstanceStatusMetadata
user-svc10.0.0.1:8080Healthyv2.1
user-svc10.0.0.2:8080Healthyv2.1
order-svc10.0.0.3:8080Healthyv1.5

Registration Patterns

Self-Registration

Service registers itself on startup:

Service starts ──► Register with registry
Service stops  ──► Deregister from registry
Health check   ──► Send heartbeats

Third-Party Registration

Separate registrar watches and registers services:

┌──────────┐     ┌───────────┐     ┌──────────┐
│Registrar │────►│  Watch    │────►│ Registry │
└──────────┘     │ containers│     └──────────┘
                 └───────────┘

Health Checking

Ensure only healthy instances receive traffic:

MethodDescription
HeartbeatService sends periodic "I'm alive"
Active probeRegistry pings service endpoint
TTLEntry expires if not renewed

ToolTypeFeatures
ConsulGeneral purposeService mesh, KV store
etcdKey-valueKubernetes backing store
ZooKeeperCoordinationLeader election
EurekaNetflix OSSJava-focused
Kubernetes DNSNativeBuilt into K8s

Kubernetes Service Discovery

# Service definition
apiVersion: v1
kind: Service
metadata:
  name: user-service
spec:
  selector:
    app: user
  ports:
    - port: 80
      targetPort: 8080

Access via DNS: user-service.namespace.svc.cluster.local


Interview Tips

  • Explain client-side vs server-side discovery
  • Describe service registry and health checks
  • Know tools: Consul, etcd, Kubernetes DNS
  • Discuss registration patterns (self vs third-party)
  • Mention challenges: consistency, availability of registry