Implementing Service Discovery Patterns

1. Client-Side Discovery Pattern

AspectDetail
MechanismClient queries registry, picks instance, calls directly
Load BalancingClient-side (round-robin, least-conn)
ProsNo extra hop; smart client decisions
ConsLogic in every client lib; multi-language overhead
ToolsEureka + Ribbon, Consul

2. Server-Side Discovery Pattern

AspectDetail
MechanismClient → Load Balancer → service instance
LB UpdatesLB queries registry or consumes service events
ProsClients dumb; central control
ConsExtra hop; LB itself must HA
ExamplesK8s Service + kube-proxy, AWS ELB, Envoy

3. Service Registry Pattern

Stored Per InstanceDetail
Service NameLogical identifier
Network AddressIP + port
HealthUP / DOWN / DRAINING
MetadataVersion, region, zone, weight
TTLAuto-expiry without heartbeat
ToolsConsul, etcd, ZooKeeper, Eureka, K8s API

4. Self-Registration Pattern

StepAction
StartupService registers itself with registry
HeartbeatPeriodic keep-alive
ShutdownDeregister gracefully (PreStop hook)
ProsService knows its own state
ConsRegistration code in every service

5. Third-Party Registration Pattern

AspectDetail
MechanismRegistrar (separate process) registers service on its behalf
ExamplesKubernetes (kubelet), Registrator for Docker
ProsService code unaware; cleaner SoC
ConsRegistrar is another moving part

6. Health Check Pattern

ProbePurpose
Liveness"Am I alive?" — restart if not
Readiness"Can I serve traffic?" — remove from LB if not
Startup"Have I finished booting?" — defer other probes
Deep HealthChecks dependencies; for ops only, not LB

Example: Spring Boot Actuator

management:
  endpoint.health.probes.enabled: true
  endpoints.web.exposure.include: health,info,metrics
  health.livenessstate.enabled: true
  health.readinessstate.enabled: true
Warning: Don't include downstream service status in liveness probe — one downstream outage will restart all your pods, amplifying the failure.

7. DNS-Based Discovery Pattern

AspectDetail
MechanismDNS A/SRV records map service name to IPs
K8s Exampleorder-svc.default.svc.cluster.local
ProsUniversal client support
ConsTTL caching delays updates; no health awareness in plain A records

8. Service Metadata Pattern

Metadata KeyUse
versionCanary / blue-green routing
region / zoneLocality-aware routing
capabilitiesFeature-flag-aware routing
weightWeighted load balancing
tenantMulti-tenant isolation

9. Lease Pattern

AspectDetail
DefinitionTime-bound registration; must renew before expiry
ExpiryRegistry auto-removes stale instances
BenefitSelf-healing against dead instances that didn't deregister
TuningLease TTL = 2-3× heartbeat interval

10. Heartbeat Pattern

AspectDetail
MechanismService sends periodic ping to registry
IntervalTypically 5-30s
Missed HeartbeatsN consecutive missed → mark DOWN
Combine WithLease TTL for self-healing