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 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
| Address | Last heartbeat | Check |
|---|---|---|
| 10.0.0.1:8080 | none yet | on the list |
| 10.0.0.2:8080 | none yet | on the list |
| 10.0.0.3:8080 | none yet | on the list |
Payments 1
Payments 2
Payments 3
- Requests sent to the dead copy
- 0
- Each copy registers. On starting, each of the 3 payments copies registers: it gives the registry its own address.
- A heartbeat every 5 s. Every 5 s each copy sends a heartbeat, a short message that says "I am alive".
- The caller asks. The orders service asks the registry where payments is, and gets 3 addresses back.
- It calls one directly. It then sends 3 requests a second straight to the copies, 1 to each in turn.
- One copy stops. At 12 s Payments 2 stops without a word, and the caller's next request to it fails.
- Two heartbeats missed. Its heartbeats at 15 s and 20 s never come, so the registry drops it at 20 s.
- The caller's list is old. The caller still holds the list from 11 s, so 1 more request goes to the dead copy.
- A fresh list. At 21 s the caller fetches the list again, and from then on sends only to live copies.
- 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
- A copy gives the registry its address when it starts, then sends heartbeats.
- The registry drops a copy only after missed heartbeats, so it is always a little late.
- A caller that keeps its own copy of the list adds more delay.
- 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. (How one service finds the addresses of another service's live copies, by name, when those copies start and stop all day.) 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 small "I am alive" message sent again and again at a steady interval, to show that a server or connection still works. The interval depends on the system: milliseconds in some, seconds in others.), 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 (A server that receives every request first and passes each one to one of several servers behind it.) 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
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.
A copy registers when it starts
Orders service, the caller Orders service the caller Registry, the list of copies, NOW Registry the list of copies payments: 3 copies NOW Payments 1, 10.0.0.1 Payments 1 10.0.0.1 Payments 2, 10.0.0.2 Payments 2 10.0.0.2 Payments 3, 10.0.0.3 Payments 3 10.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 4Step 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 list3.1 .2 .3Heartbeatevery 5 soff the list after 2 missedStep 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. (The most used orchestrator. It calls a copy a pod and a machine a node.), 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.
Heartbeats and health checks keep the list honest
A heartbeat is sent by the copy: "I am alive", every 5 seconds. A (A test that a load balancer or a service registry sends a server on a schedule, to find out if it is still working.) 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.
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.
Two places to keep the list
In (The caller keeps the list of copies and picks one itself, with no load balancer in between.), 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 keeps the list of copies.), the caller sends to one fixed name, and a load balancer behind that name holds the list. A (A small helper program beside every copy of every service. It finds other services and sends the calls, so the service's own code does not have to.) is a third way: a small helper program beside every copy does the lookup for it.
The caller keeps a copy of the list
Pick a case to follow belowWith 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 caller Orders service the caller Registry, the list of copies Registry the list of copies Payments 1, 10.0.0.1 Payments 1 10.0.0.1 Payments 2, 10.0.0.2 Payments 2 10.0.0.2 Payments 3, 10.0.0.3, FAILED, NOW Payments 3 10.0.0.3 crashed FAILED - 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 6Step 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
Clock0sRegistry lists3 copiespayments 3 missed 0 of 2Orders' saved list3 copiesstill has payments 3Errors users saw0no retryStep 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.
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 (A tree that stores words one letter per step, so words that start the same share a path.) 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 (The Domain Name System: the way the internet turns a name, such as example.com, into the address of a server.) 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
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
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?
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
Apply it
Judge a design
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.