Stage 2 · Building blocks · Medium · 13 min

Service discovery: how services find each other

How one service finds the live copies of another, when copies start and stop all day. A registry keeps the list, and heartbeats keep it fresh.

What each interview level is expected to know

Learn first: DNS and geo-routing, Load balancing and health checks

After this lesson you can:

  • Explain how a registry, heartbeats and health checks keep a list of live copies.
  • Work out how long a caller can keep using a copy that has died.
  • Design callers that cope with an old or wrong list.

Start here

See it in 60 seconds

How a caller finds a live copy

Copies of a service start and stop all day, so a registry keeps the list of live addresses and callers ask it.

Scene 1 of 8 · Each copy registers

On starting, each of the 3 payments copies registers: it gives the registry its own address.

Orders service, has no list yetOrders servicehas no list yetknows no addressRegistry, keeps the live listRegistrykeeps the live listlists 3 copiesPayments 1Payments 110.0.0.1:8080Payments 2Payments 210.0.0.2:8080Payments 3Payments 310.0.0.3:8080
  • Orders service — has no list yet — healthy — knows no address
  • Registry — keeps the live list — healthy — lists 3 copies
  • Payments 1 — healthy — 10.0.0.1:8080
  • Payments 2 — healthy — 10.0.0.2:8080
  • Payments 3 — healthy — 10.0.0.3:8080
The registry's list
AddressLast heartbeatCheck
10.0.0.1:8080none yet on the list
10.0.0.2:8080none yet on the list
10.0.0.3:8080none yet on the list
Heartbeats, second by second

Payments 1

    Payments 2

      Payments 3

        Requests sent to the dead copy
        0
        1. Each copy registers. On starting, each of the 3 payments copies registers: it gives the registry its own address.
        2. A heartbeat every 5 s. Every 5 s each copy sends a heartbeat, a short message that says "I am alive".
        3. The caller asks. The orders service asks the registry where payments is, and gets 3 addresses back.
        4. It calls one directly. It then sends 3 requests a second straight to the copies, 1 to each in turn.
        5. One copy stops. At 12 s Payments 2 stops without a word, and the caller's next request to it fails.
        6. Two heartbeats missed. Its heartbeats at 15 s and 20 s never come, so the registry drops it at 20 s.
        7. The caller's list is old. The caller still holds the list from 11 s, so 1 more request goes to the dead copy.
        8. A fresh list. At 21 s the caller fetches the list again, and from then on sends only to live copies.
        9. Now you try. The caller fetches a fresh list this often. A copy stops. For how long can the caller keep sending to it? For example: With a fresh list every 10 seconds, the caller can send to the dead copy for 5 to 20 seconds. Up to 20 requests fail. The registry needs up to 10 seconds to drop the copy, and the caller's own list can be 10 seconds old on top of that.

        Scene 1 of 8: Each copy registers. On starting, each of the 3 payments copies registers: it gives the registry its own address.

        Key points

        1. A copy gives the registry its address when it starts, then sends heartbeats.
        2. The registry drops a copy only after missed heartbeats, so it is always a little late.
        3. A caller that keeps its own copy of the list adds more delay.
        4. Shorter gaps find a dead copy sooner, at the price of more messages.

        Go deeper in the Service discovery: a registry, heartbeats and old lists lab →

        In plain words

        Say an orders service must call a payments service, which runs as 3 copies at 10.0.0.1, 10.0.0.2 and 10.0.0.3. You could type those 3 addresses into the orders service. But tomorrow a copy is replaced and gets a new address, and the typed list is wrong. fixes this with a registry: one place that keeps the list of live copies. Each copy tells the registry its address when it starts. The orders service asks the registry "where is payments?" and gets the current list. Each copy also sends a , a short "I am alive" message every few seconds. When the heartbeats stop, the registry takes that copy off the list.

        The caller does not ask the registry before every call. It keeps the list for a few seconds. So a copy that died a moment ago can still get a request or two. From that list it picks one address, often each in turn, and sends the call. Some setups move this work out of the caller, into a or a small proxy that runs beside each service.

        The problem it solves

        The list is never fully up to date. A copy that crashes cannot say goodbye, so the registry only learns of it when heartbeats go missing. With a heartbeat every 5 seconds, and a copy dropped at its second miss in a row, that takes 5 to 10 seconds. A caller that keeps its own copy of the list for 10 seconds can be 10 seconds later still. For all that time, some requests go to a copy that is gone.

        Why it matters

        In a real system, the copies of a service start, stop and move all day. Releases replace them, more are added when traffic grows, and machines fail. An interviewer who asks "how does service A reach service B?" wants to hear where the list of addresses lives, and how it stays fresh.

        A worked example

        Say payments runs as 3 copies, and each sends a heartbeat every 5 seconds. Payments 2 stops at 12 seconds. Its heartbeats at 15 and 20 seconds never arrive, so the registry drops it at 20 seconds. The caller sends 1 request a second to each copy. It fetched its list at 11 seconds and fetches again at 21. So from 12 to 21 seconds it sends 9 requests to the dead copy, and all 9 fail.

        Think of it like…

        Think of a school office with a sign-in sheet. Each teacher signs in on arrival, and a visitor asks the office which room a teacher is in.

        Where this analogy stops working

        One trusted list that everyone asks is the useful part. But a teacher who leaves can sign out. A copy that crashes cannot say anything. The registry has to notice the silence by itself, which is why it counts missed heartbeats.

        Try it

        See it working

        Live simulationOpen the full lab →

        Lab · Building blocks

        Service discovery: a registry, heartbeats and old lists

        A registry keeps the list of a service's live copies. Three copies of a payments service tell it they are alive, and a caller, the orders service, keeps its own copy of that list. Stop a payments copy, or the registry, and see how long the caller keeps sending to something that is gone.

        Try it

        Lab time 3.0 s
        The caller, the orders serviceThe callerthe orders servicelist of 3, 0.8 s oldRegistry, lists 3 copiesRegistrylists 3 copiesdrops after 2 missedPayments 1, 10.0.0.1:8080Payments 110.0.0.1:8080on the listPayments 2, 10.0.0.2:8080Payments 210.0.0.2:8080on the listPayments 3, 10.0.0.3:8080Payments 310.0.0.3:8080on the list
        • The caller — the orders service — healthy — list of 3, 0.8 s old
        • Registry — lists 3 copies — healthy — drops after 2 missed
        • Payments 1 — 10.0.0.1:8080 — healthy — on the list
        • Payments 2 — 10.0.0.2:8080 — healthy — on the list
        • Payments 3 — 10.0.0.3:8080 — healthy — on the list
        • Request
        • Response
        • Error / rejected
        • Event / message
        • Health check
        • Write
        Failing now
        0%
        of requests, over the last 2 s
        Requests failed
        0/ 26
        since the run began
        Sent to a copy that cannot answer
        0
        0 of them tried again on another copy
        Registry dropped it after
        –
        no copy is stopped or broken now
        Caller sent to it for
        –
        no copy is stopped or broken now
        Copies on the registry's list
        3/ 3
        a copy is dropped after 2 s of silence
        Copies on the caller's list
        3/ 3
        fetched 0.8 s ago

        Numbers add up:26 arrived = 22 answered + 0 failed at a stopped or broken copy + 0 failed with no copy known + 4 in progressThe lab checks this sum as it runs, so a counting mistake would show here.

        Controls

        The registry
        How the registry learns that a copy is alive

        Each copy tells the registry "I am alive". A copy that runs but cannot answer still sends them.

        1 s
        2

        With these two settings, a copy is dropped after 2 s with no good word from it.

        Registry

        Heartbeats: each copy reports in by itself. Health checks: the registry asks each copy to do a small piece of real work.

        The callers
        2 s

        After one failed call, a caller stops using that copy until its next fresh list, and tries the request once more on another copy.

        A cart service with its own copy of the list. It fetches half a gap after the orders service.

        6/s
        Copies of payments
        Payments 1
        Payments 2
        Payments 3

        A copy that stops says nothing to anyone. A broken copy still runs, and still sends heartbeats when those are in use, but every request to it fails.

        Failed requests (%)
        02550751001s2s3s
        Show data table
        TimeFailed requests
        1s0
        1s0
        2s0
        2s0
        3s0
        3s0
        Copies on each list
        The registry's listThe first caller's list
        00.81.52.331s2s3s
        Show data table
        TimeThe registry's listThe first caller's list
        1s33
        1s33
        2s33
        2s33
        3s33
        3s33

        What just happened

        Nothing yet. Start traffic or run an experiment.

        What this model simplifies
        • The times are short so you can watch them: a heartbeat every second and a fresh list every 2 seconds. Real systems often wait 10 to 30 seconds for each.
        • There are at most two callers, and they share the same requests a second between them. A real system has many, and each holds a list of a different age.
        • Choosing health checks turns heartbeats off. Real systems often use both. The registry also keeps asking a copy it has dropped, and lists it again when it answers well.
        • The registry is one box. A real one runs as several copies that agree with each other, so that one of them can stop.
        • A request to a stopped copy fails after 80 ms. A real dead machine often gives no answer at all, and the caller must wait for its own time limit.
        • A broken copy fails every request and every health check. A real one may fail only some of them, and a health check is only as good as the work it asks for.
        • When the registry comes back, it starts its count again for every copy on its list. Real registries have their own, more careful, rules for this.
        • With "Skip a copy that fails" on, a caller tries a failed request once more, and forgets what it skipped at its next fresh list.
        • The callers pick a copy themselves. A load balancer in front of the copies, which is the other common design, is left out.

        Also try: Load balancer · Orchestrator: copies, machines and autoscaling

        Mechanics

        How it works

        The pictures follow this page's example: an orders service that calls a payments service, which runs as 3 copies. The first shows how orders finds them. The second shows what happens when one of them dies.

        1. A copy registers when it starts

          Orders service, the callerOrders servicethe callerRegistry, the list of copies, NOWRegistrythe list of copiespayments: 3 copiesNOWPayments 1, 10.0.0.1Payments 110.0.0.1Payments 2, 10.0.0.2Payments 210.0.0.2Payments 3, 10.0.0.3Payments 310.0.0.3
          • Orders service — the caller — healthy
          • Registry — the list of copies — healthy — NOW — payments: 3 copies
          • Payments 1 — 10.0.0.1 — healthy
          • Payments 2 — 10.0.0.2 — healthy
          • Payments 3 — 10.0.0.3 — healthy
          • Heartbeat
          • Call
          • Answer
          Step 1 of 4

          Step 1 of 4 · Registry

          Done: Each payments copy registers when it starts

          What happens
          Each copy tells the registry its name and address: "payments, at 10.0.0.2".
          Why
          Copies come and go, so only the copies themselves know where they are right now.
          What changes
          The registry lists 3 copies of payments.
          Next
          Every 5 seconds, each copy sends a heartbeat
          Copies on the list
          3
          .1 .2 .3
          Heartbeat
          every 5 s
          off the list after 2 missed

          Step 1 of 4: Registry. Each payments copy registers when it starts. Each copy tells the registry its name and address: "payments, at 10.0.0.2".

          Payments 1 starts and tells the registry: "payments, at 10.0.0.1:8080". On many platforms the platform does this for the copy. , a platform that runs copies for you, adds each new copy to the list. In the picture, three copies start, and the orders service finds them by name.

        2. Heartbeats and health checks keep the list honest

          A heartbeat is sent by the copy: "I am alive", every 5 seconds. A goes the other way: the registry or a load balancer asks the copy "are you well?" and waits for an answer. Both keep the list fresh. A health check can also test that the copy can do real work, not only that it is running. In the pictures, a copy that misses 2 heartbeats in a row is taken off the list.

        3. The caller looks the service up by name

          The orders service asks for "payments", never for an address. It gets back the addresses that are on the list now.

        4. Two places to keep the list

          In , the caller keeps the list and picks a copy itself. That saves one step on the way, but every caller needs the lookup code. In , the caller sends to one fixed name, and a load balancer behind that name holds the list. A is a third way: a small helper program beside every copy does the lookup for it.

        5. The caller keeps a copy of the list

          Pick a case to follow below

          With this case: Payments 3 got calls for 20 seconds after it died: 10 for the registry to drop it, 10 more for the old list. 2 users saw an error.

          Orders service, the callerOrders servicethe callerRegistry, the list of copiesRegistrythe list of copiesPayments 1, 10.0.0.1Payments 110.0.0.1Payments 2, 10.0.0.2Payments 210.0.0.2Payments 3, 10.0.0.3, FAILED, NOWPayments 310.0.0.3crashedFAILED
          • Orders service — the caller — healthy
          • Registry — the list of copies — healthy
          • Payments 1 — 10.0.0.1 — healthy
          • Payments 2 — 10.0.0.2 — healthy
          • Payments 3 — 10.0.0.3 — FAILED — NOW — crashed
          • Call with no answer
          • Heartbeat
          • Call
          Step 1 of 6

          Step 1 of 6 · Payments 3

          Failed: At second 0, payments 3 crashes

          What happens
          Payments 3 stops answering and stops sending heartbeats. Nobody is told.
          Why
          A crashed program cannot say that it crashed.
          What changes
          Both the registry and orders still list payments 3.
          Next
          At second 3, orders calls payments 3
          Clock
          0s
          Registry lists
          3 copies
          payments 3 missed 0 of 2
          Orders' saved list
          3 copies
          still has payments 3
          Errors users saw
          0
          no retry

          Step 1 of 6: Payments 3. At second 0, payments 3 crashes. Payments 3 stops answering and stops sending heartbeats. Nobody is told.

          What these pictures simplify. These pictures use short, stated times: a heartbeat every 5 seconds, a refresh every 10, and two calls to the dead copy. Real systems often wait 10 to 30 seconds between heartbeats, so the gap can be a minute or more. The service discovery lab above runs many calls at once.

          Asking the registry before every request would be slow, so callers keep the list for a while. In the picture, the caller fetches it every 10 seconds, and payments 3 crashes. A dead copy can then get requests for up to 20 seconds: 10 for the registry to drop it, and 10 for the old list. The picture uses short times so it is easy to follow. Real systems often wait 10 to 30 seconds between heartbeats or checks, so the gap can be a minute or more.

          Step through the first case. Payments 3 still gets its turn twice after it died, because orders' saved list still has it. Each of those calls fails, and a user sees an error.

          Then pick the second case, the fix from the next step. Each failed call is tried again on another copy, so users see a short delay instead of an error. That retry is safe here because the call carries an id, so a payment cannot be charged twice.

        6. Callers must cope with a wrong list

          The list will be wrong sometimes. So the caller sets a short limit on how long it waits. When a call fails and repeating it is safe, it another copy. A failed call is a signal too: many callers stop using a copy as soon as it fails, without waiting for the registry.

        Use it when

        • Any system where copies come and go by themselves, because a platform starts, stops and replaces them for you.
        • Many services that call each other, where addresses typed into settings files would never keep up.

        Skip it when

        • Two or three fixed servers that change once a year: a settings file or a name is enough.
        • Traffic from browsers and phones: they find you through DNS and a load balancer, not through your registry.
        • A second registry beside the one your platform already has: use the platform's.

        Check yourself

        A new copy of a service starts. How do callers learn about it?

        Judgement

        Trade-offs and how it fails

        What you give up

        Speed
        A heartbeat every second finds a dead copy in a second or two. But every copy sends more messages, and a short pause can look like a death.
        Safety
        A heartbeat every 30 seconds is calm and cheap. But requests can go to a dead copy for up to a minute, and longer with an old list, unless callers try another copy.

        Failure modes

        • An old list: the registry has dropped a copy, but callers send to it until their next fetch. In the explainer that is 1 more failed request, after 8 that failed while the registry waited.
        • Alive but not ready: a copy registers before it has loaded its data, and its first requests fail.
        • Flapping: a slow copy misses two heartbeats, is dropped, comes back and is dropped again, so traffic jumps about.
        • The registry is down: new copies cannot register. Callers that kept their last list keep working, and callers that did not are stuck.
        • A health check that is too strict: one slow database makes every copy fail its check together, and the list is empty.

        In the real world

        • Netflix built Eureka for this: each copy sends heartbeats to it, and each caller keeps its own copy of the list.
        • Kubernetes gives a service one fixed name. It asks each copy whether it is ready, and sends traffic only to the ones that say yes.
        • Consul keeps the list and runs the health checks, and callers can look a service up through DNS.
        • A cloud load balancer does server-side discovery: callers use one name, and the balancer checks which servers behind it are healthy.

        Trade-offs to be ready to discuss: Client side vs server side discovery · Heartbeat gap vs load

        Check yourself

        Why do callers keep a cached copy of the registry's list?

        Interview

        What interviewers look for

        • Say where the list lives: "services register with a registry, and callers look each other up by name."
        • Say how a dead copy is found, and give the delay: the heartbeat gap times the misses needed, plus the age of the caller's list.
        • Say what covers that gap: a short wait limit, and a second try on another copy.

        Say it out loud: A copy crashes. Why do some requests still go to it for several seconds?

        Copies send a heartbeat every 5 seconds, and the registry drops a copy after 2 misses. A caller fetches the list every 10 seconds. For how long, at most, can the caller send to a copy that has stopped?

        Expected depth by interview level

        Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

        JuniorUseful
        Why a fixed list of addresses breaks, and what a registry is.
        MidUseful
        Heartbeats and health checks, and how long a dead copy stays on the list.
        SeniorExpected
        Who keeps the list, how old it may be, and what happens when the registry is down.
        StaffExpected
        One way of finding services across many teams: its limits, its cost, and how far a bad list can spread.

        Practice

        Apply it

        Three harder questions

        The first two use the idea on a new case. The last asks you to judge a design.

        Apply it

        A payments copy adds itself to the registry the moment its process starts. It then takes 20 seconds to load its data. What happens?

        Apply it

        Callers are written in four languages, and the team does not want lookup code in each one. Which kind of discovery fits?

        Judge a design

        A team sets heartbeats to every 1 second and drops a copy after 1 missed heartbeat. What is the likely problem?

        Next: reverse proxies and API gateways, the front door that sends each outside request to the right service.

        Go deeper

        Sources and further reading

        First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

        • Official docsServiceKubernetes documentationA stable name and address in front of a changing set of pods.
        • Official docsDNS for Services and PodsKubernetes documentationHow one service finds another by a DNS name inside the cluster.