DistributedBeginner

How do services find each other when instances come and go?

“Instances autoscale, get rescheduled and roll out several times a day. How does Service A find a healthy instance of Service B, and what goes wrong with stale information?”

What this tests

  • Why static configuration fails in a dynamic fleet
  • Registry with registration, heartbeats/TTL and health checks
  • Client-side vs server-side discovery, DNS and service mesh
  • The stale-registry failure and the registry as a dependency

Answers by level

Read the beginner answer first and notice what is missing.

Static config breaks the first time the autoscaler adds an instance nobody listed, or a rolling deploy replaces every IP. A service registry fixes it: each instance registers itself (or the orchestrator registers it) on start with its address and a health endpoint; it renews a heartbeat with a TTL (say 10–30 s); the registry drops entries whose TTL expires or whose health check fails. Service A queries the registry (or a local cache of it) and load-balances across healthy instances.

Two shapes: client-side discovery, where A holds the list and picks an instance (fewer hops, smarter routing, but every language needs the client library), and server-side, where A calls a stable name and a load balancer or proxy does the lookup (simpler clients, one more hop). Kubernetes services are server-side via DNS plus kube-proxy; a service mesh puts a sidecar next to each instance and does discovery, retries and mTLS there — see Service Discovery.

Green flags · Red flags

Strong green flag · Describes deregister-then-drain on shutdown and retry-on-refused as the two mechanisms that close the deploy and crash gaps.
Green flags
  • Explains why static config fails with autoscaling and rolling deploys
  • Registration, heartbeat TTL and health checks, with numbers
  • Client-side vs server-side discovery with a tradeoff
  • Stale-registry failure modes in both directions
  • Treats the registry as a dependency and caches the last good list
Red flags
  • "We keep the IPs in a config file and redeploy when they change."
  • Assumes DNS is refreshed per request in every runtime
  • No idea what happens when the registry is down
  • Picks a TTL without relating it to failure duration

Follow-up questions

F1
Heartbeat TTL is 5 minutes; the autoscaler kills 3 of 9 instances. Impact?
F2
The registry is unreachable for 10 minutes. Should calls fail?
F3
Why might a service mesh be overkill here?

Scenario

After moving to autoscaling, the order service sees 8–12% connection errors calling the inventory service for several minutes after every scale-down. The registry uses a 5-minute heartbeat TTL, instances do not deregister on SIGTERM, and the client library caches the instance list for an hour. Identify each contributing decision and specify the values you would use.

Learn this topic