Implementing Service Discovery

1. Implementing Client-Side Discovery

AspectDetail
FlowClient queries registry → picks instance → calls directly
ProsNo extra hop; client controls LB
ConsLogic in every client (multi-language)
ExamplesEureka + Ribbon, Consul + native client

2. Implementing Server-Side Discovery

AspectDetail
FlowClient → LB/proxy → looks up + forwards
ProsLanguage agnostic; one place to manage
ConsExtra network hop
ExamplesKubernetes Service, AWS ALB, Envoy

3. Using Service Registry

RegistryBacked ByNotes
ConsulRaft KVDNS + HTTP API
EurekaAP (peer replication)Spring Cloud staple LEGACY
etcdRaftUsed by Kubernetes
ZookeeperZABOlder; Kafka's coordination (replaced by KRaft)
K8s APIetcdEndpoints/EndpointSlices via DNS

4. Implementing Health Checks

ProbeUseEndpoint
LivenessRestart if dead/livez
ReadinessRemove from LB until ready/readyz
StartupLong startup grace/startupz
Deep checkVerify deps (DB, cache)/health
Warning: Don't fail liveness on transient downstream errors — this can cascade restarts.

5. Handling Service Registration

PatternDetail
Self-registrationService calls registry on startup/shutdown
Third-party registrationSidecar / orchestrator manages it
SidecarEnvoy / Consul agent
K8sPod readiness → endpoint addition

6. Using DNS-Based Discovery

MechanismDetail
A/AAAA recordsMultiple IPs returned per name
SRV recordsHost + port + weight + priority
K8s DNSmy-svc.my-ns.svc.cluster.local
TTLShort TTL (5–30s) for dynamic infra
CaveatJava DNS caching; set networkaddress.cache.ttl=30

7. Implementing Service Metadata

MetadataUse
versionRouting (canary, A/B)
region/zoneAffinity, locality
protocolhttp, grpc, h2c
tagsCustom (tier, tenant, capability)

8. Handling Dynamic Port Allocation

StrategyDetail
Bind to 0OS picks free port; service registers actual port
Discover via SRVIncludes port
K8s container portsPod IP + container port (no host port)
AvoidHard-coded ports across replicas on same host

9. Implementing Heartbeat Mechanisms

AspectDetail
Frequency5–30s typical
Timeout2–3× heartbeat interval
Action on missMark unhealthy, remove from LB
TTL renewalConsul: re-PUT key before TTL expires

10. Managing Service Instances

OperationDetail
DrainMark unhealthy, finish in-flight
DeregisterOn graceful shutdown (SIGTERM)
CordonStop new traffic, keep existing
ScaleHPA / KEDA based on metrics

11. Using Service Discovery Tools

ToolBest For
ConsulMulti-DC, mixed VM + container
Kubernetes ServiceK8s-native (default choice)
Istio / LinkerdMesh-driven discovery + mTLS
AWS Cloud MapAWS-native

12. Implementing Service Health Aggregation

AggregationDetail
Per-instanceLiveness/readiness
Per-service% healthy instances
CompositeSelf + downstream deps
DashboardRED metrics + heatmap