Skip to content

Monolith & microservices · 3 of 4

How services find each other

Step 1 · The pattern

One service, several instances

Pricing runs as two instances, A and B, and checkout spreads its calls between them. Each has its own address. On every deploy, those addresses change. (All numbers are illustrative.)

  • a call
  • answered

In short

How services find each other: how it works and how it breaks

A service runs as several instances, and every deploy, crash or scale-up changes their addresses. A caller needs the current list. The simplest answer is DNS: look up the name, get addresses, cache them for the record's time to live. Between a deploy and the end of that TTL, calls go to instances that are gone. Worse, many clients resolve a name once at startup and reuse the addresses or the open connections until they restart, so a deploy breaks them for good. Service discovery keeps a registry that every instance joins on start and leaves on stop, that health-checks them and drops the ones that stop answering, and that tells clients when the list changes. Clients still meet a dead instance now and then, so a refused connection should be retried once on another instance.

What this simulation simplifies

  • Checkout here keeps either the old list or the new one; real clients refresh per connection, per pool and per cache, so a mix of old and new addresses is common.
  • Refused connections fail instantly in this picture; in a real network a call to a gone address can also hang until a timeout.

How this comes up in interviews

"Instances of a service come and go with every deploy. How do callers find a working one?"

Then they ask

  1. What goes wrong with plain DNS and its time to live?
  2. Client-side or server-side discovery: which would you pick, and why?
  3. What should a caller do when it reaches an instance that is gone?

A strong answer covers

  • A registry tracks instances: they join on start, leave on stop, and are dropped when health checks fail.
  • Clients either read the registry and choose (client-side), or call a load balancer or proxy that does (server-side).
  • DNS answers are cached for their TTL, and many clients cache longer or pin connections at startup.
  • Callers should follow changes, cap connection age, and retry a refused connection on another instance.
  • Instances should leave the registry before they stop, then drain in-flight requests.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.