Implementing Service Discovery
1. Implementing Client-Side Discovery
| Aspect | Detail |
| Flow | Client queries registry → picks instance → calls directly |
| Pros | No extra hop; client controls LB |
| Cons | Logic in every client (multi-language) |
| Examples | Eureka + Ribbon, Consul + native client |
2. Implementing Server-Side Discovery
| Aspect | Detail |
| Flow | Client → LB/proxy → looks up + forwards |
| Pros | Language agnostic; one place to manage |
| Cons | Extra network hop |
| Examples | Kubernetes Service, AWS ALB, Envoy |
3. Using Service Registry
| Registry | Backed By | Notes |
| Consul | Raft KV | DNS + HTTP API |
| Eureka | AP (peer replication) | Spring Cloud staple LEGACY |
| etcd | Raft | Used by Kubernetes |
| Zookeeper | ZAB | Older; Kafka's coordination (replaced by KRaft) |
| K8s API | etcd | Endpoints/EndpointSlices via DNS |
4. Implementing Health Checks
| Probe | Use | Endpoint |
| Liveness | Restart if dead | /livez |
| Readiness | Remove from LB until ready | /readyz |
| Startup | Long startup grace | /startupz |
| Deep check | Verify deps (DB, cache) | /health |
Warning: Don't fail liveness on transient downstream errors — this can cascade restarts.
5. Handling Service Registration
| Pattern | Detail |
| Self-registration | Service calls registry on startup/shutdown |
| Third-party registration | Sidecar / orchestrator manages it |
| Sidecar | Envoy / Consul agent |
| K8s | Pod readiness → endpoint addition |
6. Using DNS-Based Discovery
| Mechanism | Detail |
| A/AAAA records | Multiple IPs returned per name |
| SRV records | Host + port + weight + priority |
| K8s DNS | my-svc.my-ns.svc.cluster.local |
| TTL | Short TTL (5–30s) for dynamic infra |
| Caveat | Java DNS caching; set networkaddress.cache.ttl=30 |
| Metadata | Use |
| version | Routing (canary, A/B) |
| region/zone | Affinity, locality |
| protocol | http, grpc, h2c |
| tags | Custom (tier, tenant, capability) |
8. Handling Dynamic Port Allocation
| Strategy | Detail |
| Bind to 0 | OS picks free port; service registers actual port |
| Discover via SRV | Includes port |
| K8s container ports | Pod IP + container port (no host port) |
| Avoid | Hard-coded ports across replicas on same host |
9. Implementing Heartbeat Mechanisms
| Aspect | Detail |
| Frequency | 5–30s typical |
| Timeout | 2–3× heartbeat interval |
| Action on miss | Mark unhealthy, remove from LB |
| TTL renewal | Consul: re-PUT key before TTL expires |
10. Managing Service Instances
| Operation | Detail |
| Drain | Mark unhealthy, finish in-flight |
| Deregister | On graceful shutdown (SIGTERM) |
| Cordon | Stop new traffic, keep existing |
| Scale | HPA / KEDA based on metrics |
| Tool | Best For |
| Consul | Multi-DC, mixed VM + container |
| Kubernetes Service | K8s-native (default choice) |
| Istio / Linkerd | Mesh-driven discovery + mTLS |
| AWS Cloud Map | AWS-native |
12. Implementing Service Health Aggregation
| Aggregation | Detail |
| Per-instance | Liveness/readiness |
| Per-service | % healthy instances |
| Composite | Self + downstream deps |
| Dashboard | RED metrics + heatmap |