esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Service Discovery: How Services Find Each Other's Current Addresses

Learn how a registry, health checks, and two lookup styles let services find each other without hardcoded addresses.

In short

Service discovery is how one service finds the current network location of another service, using a registry instead of fixed addresses.

11 min readFree

What you will learn

Service discovery is the way one service finds the current network location of another service. A service is a small program that does one job, such as taking payments or listing products. Its location is an IP address plus a port. The IP address names a machine on a network, and the port names the door on that machine where the program listens.

By the end of this lesson you will know why these locations cannot be typed into a settings file, how a registry keeps a live list of them, and the two ways a caller can use that list.

Key words

Service
A small program that does one job and talks to other programs over a network.
Instance
One running copy of a service. A busy service can have many instances at once.
IP address
A number that names a machine on a network.
Port
A number that names one door on a machine, where one program listens for requests.
Hardcode
To type a value straight into code or settings so it never changes on its own.
Service registry
A database that keeps a live list of service instances and where each one can be reached.
Health check
A test that asks whether an instance can do its job right now.
Heartbeat
A short I am still here message that an instance sends on a regular schedule.

Why do fixed addresses break?

In a small app, one program often runs on one server for years. You can write its address into a settings file and forget about it. Microservice systems work differently. Each service may run as five instances or fifty, and the team changes those counts often.

Addresses change because instances keep coming and going. The cards below show four common reasons.

  • Starts and stops

    A new instance gets its own address when it boots. A stopped instance leaves an address that no longer answers.

  • Scales up and down

    Traffic rises in the evening, so more copies start. It falls late at night, so copies shut down.

  • Fails and gets replaced

    A crashed instance is usually replaced by a new one, which often has a different address.

  • Moves to another machine

    The platform may move an instance to a different machine, and that changes its address.

Kubernetes, a popular system for running containers, makes the same point. The address of a pod, which is the smallest unit it runs, is temporary and changes when the pod restarts. <cite index="26-4">Pod IPs are ephemeral and change on restarts, so service names give a stable identity backed by automatic DNS records.</cite>

What does a stale address do?

Suppose the web service has the address of the recommendation service written in its settings. When that instance stops, the web service keeps knocking on a closed door. Requests fail, and users see slow or broken pages. The fix is to edit the settings and redeploy every caller. That is slow and easy to get wrong. <cite index="5-2">Without discovery, teams have to type in each address by hand.</cite> Service discovery removes that manual step by letting callers ask for the current list instead.

How does a service registry work?

A registry is a database that records where each live instance can be reached. <cite index="3-2">The service registry is a database populated with information on how to dispatch requests to microservice instances.</cite> Four steps keep that database accurate.

  1. 1

    Register on startup

    When an instance boots, it announces itself to the registry with its address and port. Some platforms do this step for the instance. <cite index="13-4">Each service instance registers itself with the discovery server when it starts, providing its IP address, port, and health status.</cite>

  2. 2

    Prove you are alive

    The instance keeps proving that it works. It may send a heartbeat on a timer, or the registry may run checks against it. <cite index="17-3">Instances register with a registry, renew a TTL heartbeat, and drop out when they stop or fail health checks.</cite>

  3. 3

    Answer lookups with healthy instances only

    When a caller asks for a service, the registry returns only the instances that passed their latest checks. <cite index="10-6">Consul returns only healthy instances because it continuously runs health checks against every registered instance.</cite>

  4. 4

    Drop the instance that stops responding

    When checks fail or heartbeats stop, the registry removes that instance from its list. Callers then stop receiving its address.

The registry is only as good as its checks. Next, look at how those checks work.

What are heartbeats and health checks?

Health checks come in several forms. Some tools call an HTTP address and expect a success answer. Others open a plain network connection or run a small script. Many tools also support a time-to-live check, often called a TTL. The instance must report in before the timer runs out. If the report does not arrive in time, the registry marks the instance as failed.

The code below is a tiny registry. It remembers the last time each instance reported in, and it drops any instance that has been quiet for longer than the limit. The times are passed in as plain numbers, so the output is the same every run.

Python
class Registry:
    def __init__(self, limit):
        self.limit = limit
        self.last_seen = {}

    def register(self, address, now):
        self.last_seen[address] = now

    def heartbeat(self, address, now):
        if address in self.last_seen:
            self.last_seen[address] = now

    def healthy(self, now):
        alive = [a for a, t in self.last_seen.items() if now - t <= self.limit]
        return sorted(alive)

reg = Registry(limit=10)
reg.register('10.0.0.5:8080', now=0)
reg.register('10.0.0.6:8080', now=0)
reg.heartbeat('10.0.0.5:8080', now=8)
print(reg.healthy(now=12))
print(reg.healthy(now=25))

Output

['10.0.0.5:8080']
[]

Read the output step by step. At time 12, the first instance last reported at time 8, which is 4 units ago, so it stays listed. The second instance has been silent since time 0, which is 12 units ago, so it is dropped. At time 25, both instances are silent for longer than the limit, so the list is empty.

Tip

Real registries are more careful than this sketch. They add retries and extra checks, so one lost message does not remove a healthy instance.

What are the two ways to look up a service?

Once the registry holds a live list, a caller still needs a way to read it. There are two common styles. The main difference is who does the querying: the caller itself, or a router placed in front of the service.

Client-side discovery

In client-side discovery, the calling service asks the registry for the list of instances. It then picks one itself and sends the request straight to that instance. <cite index="4-5">In client-side discovery, the client queries a registry and chooses an instance, often with a load-balancing library.</cite>

The cost is extra code in every caller. Teams that write services in several languages must write this lookup logic once for each language and framework they use.

Server-side discovery

In server-side discovery, the caller sends its request to one fixed address, such as a router or a load balancer. That middle box asks the registry, picks a healthy instance, and forwards the request. The caller never needs to know the registry exists. <cite index="8-9">In server-side discovery, the client simply calls a fixed endpoint, and that intermediary queries the registry on the client's behalf and forwards the request to a service instance.</cite>

The trade-off is one more part to run and one more network step on every request. Teams choose the style that puts the work in the place they can best maintain.

Comparing the two styles

  • Who reads the registry

    Client-side: the caller reads it. Server-side: the router or load balancer reads it.

  • Code in each caller

    Client-side: more code, and it must match each language. Server-side: less code, because callers use one fixed address.

  • Extra network step

    Client-side: one extra registry query before each request. Server-side: one more hop through the router.

Tip

Many real systems mix both styles. Do not memorize the names. Ask one question instead: which part of my system should carry the lookup work?

Worked example: checkout finds payments in Kubernetes

Imagine a shopping app that runs on Kubernetes. Its checkout service must take card payments from a payments service. Each payments copy runs as a pod. Checkout does not store any payment address. It only uses the name payments. Here is what happens behind that name.

  1. 1

    Checkout asks for the name

    Checkout sends its request to the name payments. Pods in the same namespace, which is a group of related resources, can use the short name. Pods in other namespaces add the namespace to the name.

  2. 2

    DNS turns the name into addresses

    DNS is the system that turns names into addresses. Kubernetes runs a DNS server inside the cluster. <cite index="22-9">A cluster-aware DNS server, such as CoreDNS, watches the Kubernetes API for new Services and creates a set of DNS records for each one.</cite>

  3. 3

    Only ready pods are listed

    A pod that is not ready should not receive traffic. Kubernetes tracks which pods are ready, and the name leads only to those. Depending on the Service type, the answer may be one stable virtual address. That address then routes to the ready pods.

  4. 4

    Kubernetes keeps the list current

    Kubernetes keeps watching for pods that appear or disappear. The list of endpoints, which are the addresses of the pods behind a service, is therefore always up to date in one shared place.

  5. 5

    A deploy swaps pods without a config change

    During a deploy, old pods stop and new pods start. Old addresses leave the list, and new addresses join once their pods are ready. <cite index="18-17,18-18">When pods die, their endpoints are removed. When new pods start, their endpoints are added.</cite> Checkout keeps using the same name, so no one edits its settings.

The result is that checkout keeps working through the deploy. Only the registry changed. Checkout did not.

Tip

Add a readiness check to every pod behind a service.

Check your understanding

Quick check

A video app's recommendation service scales from 3 instances to 10 during the evening peak. Later, the web service keeps sending requests to an old recommendation instance that has already shut down. Which part is missing or failing, and what fixes it?

Quick check: choosing a lookup style

Quick check

A shop has an API gateway at its edge. Outside users call only the gateway. Inside the system, the orders, stock, and payments services are written in three languages. The team does not want registry code in any of them. Which lookup style fits best?

Common mistakes

  • Skipping health checks

    A registry with no checks keeps dead instances in its list. Callers then keep failing, and the errors look random.

  • Keeping addresses for too long

    A caller that saves an address for a long time can keep using it after the registry removed it. Keep cache times short.

  • Adding a fixed fallback

    A fixed backup address looks safe. Once the real machine moves, the fallback sends traffic to a machine that no longer serves the service.

  • Forgetting to leave the list

    An instance that stops without removing itself leaves a ghost entry. Heartbeats or health checks catch this case.

Key takeaways

Service discovery lets services find each other's current addresses at call time, instead of relying on hardcoded values. A registry keeps the live list. Health checks and heartbeats keep that list accurate. The caller either queries the registry itself, or it asks a router to do the lookup. Kubernetes applies the same idea through service names and DNS. Other lessons cover how routers choose an instance and how public DNS works on the internet.

Sources

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.