Managing Service Discovery

1. Understanding Service Registry Pattern

ComponentRole
RegistryStores instance → endpoint mappings
RegistrarService registers self / sidecar registers
ResolverClient looks up endpoints
Health checkerRemoves unhealthy instances
ExamplesConsul, Eureka, etcd, ZooKeeper, Kubernetes API

2. Implementing Client-Side Discovery

ProsCons
No extra hopLogic per language
Smart load balancingLarger client footprint
Examples: Eureka + Ribbon, gRPC name resolverCoupling to registry

3. Implementing Server-Side Discovery

ProsCons
Polyglot clientsExtra hop, latency
Centralized policiesLB is bottleneck/SPOF
Examples: AWS ALB, Kubernetes Service, Envoy

4. Implementing Health Checks

TypeEndpointUse
Liveness/healthzRestart if failing
Readiness/readyRemove from LB pool
Startup/startupSlow boot grace period
Deep healthIncludes dependenciesBeware cascades
Shallow healthProcess up onlyDefault for liveness

5. Implementing Heartbeat Mechanisms

PatternDetail
Push heartbeatService POSTs to registry every N seconds
Pull (probe)Registry pings service health endpoint
Lease renewaletcd/ZooKeeper TTL refresh
Phi accrualProbabilistic suspicion (Akka, Cassandra)

6. Working with DNS-Based Discovery

RecordUse
A / AAAAHostname → IP
SRVService + port + priority + weight
CNAMEAlias for service endpoint
Headless service (k8s)Returns all pod IPs
TTL pitfallsStale cache delays failover

7. Implementing Service Registration

PatternDetail
Self-registrationService registers on startup
Third-party (registrar)Sidecar / orchestrator registers
KubernetesEndpoints auto-managed by controller

8. Handling Service Deregistration

TriggerAction
Graceful shutdown hookService deregisters
Lease expirationRegistry removes after TTL
Health check failureMark unhealthy → remove
Drain periodStop accepting new; finish in-flight

9. Implementing Service Metadata Management

MetadataUse
VersionCanary, traffic split
Region/AZLocality routing
Capacity / weightWeighted LB
TagsFiltering (env=prod)

10. Understanding Service Discovery Trade-offs

TradeDetail
Strong vs eventual consistencyetcd vs gossip
Push vs pullLatency vs simplicity
Centralized vs P2PSPOF vs complex
DNS vs APIUniversal vs feature-rich