06. Architecture / Lesson 2 of 4

Service Discovery

How one service finds the live addresses of another when machines come and go.

  • 8 min
  • Intermediate
  • 9 steps
  • Failed calls per crash0 (good)
  • Change noticed in—
  • Instances up1 (good)

orders config

PAYMENTS_URL=10.0.1.5:8080

OrdersPayments 10.0.1.5
  • Request
  • Health check
  • Failed request
Step 1/9

Step 1 of 9: The orders service has the payments address in a file

Read every step as text
  1. Step 1: The orders service has the payments address in a file

    To call payments, orders needs an address: an IP and a port. The simplest way is to write it in a config file: PAYMENTS_URL=http://10.0.1.5:8080.

    It works on day one, when payments is one server that never moves.

    A hard-coded address is a promise that the machine at that address will exist forever.

  2. Step 2: Instances come and go, and the addresses change

    Payments now runs 3 instances. Machines crash, deploys replace them and the autoscaler adds new ones, each with a new IP. The config still lists the old addresses.

    When the instance at 10.0.1.7 is replaced by one at 10.0.1.9, one call in 3 still goes to the dead address. At 300 calls a second that is 100 failed calls every second, until someone edits the file and restarts orders.

    In the cloud, addresses are temporary. Callers must look them up, not remember them.

  3. Step 3: Keep a live list in one place: a service registry

    A service registry is a small database of names and addresses: payments → 10.0.1.5, 10.0.1.6, 10.0.1.9. Callers ask it where a service is instead of reading a file.

    The registry is just a list, so it must be correct and highly available. It is usually a small cluster (Consul, etcd or ZooKeeper) that agrees on its content, as in the consensus lesson.

    The service name stays fixed; the addresses behind it change freely.

  4. Step 4: Instances register themselves and send heartbeats

    When an instance starts it tells the registry its address. Then it sends a heartbeat every 5 s. If 3 heartbeats are missed, the registry assumes the instance is dead and removes it: 3 × 5 = 15 s.

    Nobody edits a file. But for those 15 s a crashed instance is still listed: 100 calls/s × 15 s = 1,500 calls could go to it if nothing retries.

    A shorter heartbeat finds failures faster but adds traffic and can drop a healthy but busy instance. Callers should also retry on another instance.

  5. Step 5: Client-side discovery: the caller picks an instance

    With client-side discovery the orders service asks the registry for the list, keeps it for 10 s, and chooses an instance itself (round robin, for example). There is no extra hop.

    The cost: every caller needs this logic in its code, in every language. And a change can take as long as 15 s for the registry to notice plus 10 s for the client's refresh: 25 s, or 2,500 calls to a dead instance without retries.

    Fast and simple on the network, but the discovery logic is copied into every service.

  6. Step 6: Server-side discovery: a load balancer does the lookup

    With server-side discovery orders calls one fixed address, the load balancer. The balancer watches the registry and spreads calls over the live instances. Callers stay simple, and any language works.

    The cost: one more hop on every call, and the balancer is another thing to run and keep alive. It learns about a removed instance as soon as the registry changes, so the window is the registry's 15 s: 1,500 calls without retries.

    This is what Kubernetes Services and cloud load balancers do for you.

  7. Step 7: DNS-based: use the name system you already have

    Give each service a name such as payments.internal and let DNS return the addresses of the live instances. Every language and tool already speaks DNS, so nothing new is needed on the client.

    The catch is caching. A DNS answer is reused for its TTL, say 30 s, and some libraries cache longer. A change takes 15 s to reach DNS and up to 30 s more to reach callers: 45 s, or 4,500 calls to a dead instance without retries.

    A low TTL (5 to 30 s) helps, but cannot be trusted on every client. DNS also gives no health or load information.

  8. Step 8: A service mesh puts the logic in a proxy next to each service

    In a service mesh (Istio, Linkerd) a small proxy runs beside every service. The app calls localhost; the proxy knows the live instances because a control plane pushes the list in about 1 s, so a change is noticed in 15 + 1 = 16 s.

    If a connection to a dead instance fails, the proxy retries another one at once, so the caller sees almost no errors. The proxies also add encryption, timeouts and metrics, without any change to the apps.

    A mesh is powerful but heavy: another layer to run, learn and debug. Use it when many services need the same rules.

  9. Step 9: Service discovery in one picture

    Addresses are temporary, so callers look services up by name. Instances register themselves and send heartbeats; a registry keeps the live list.

    Client-side discovery has no extra hop but needs library code; server-side and DNS keep callers simple; a mesh moves it all into proxies. Each one is a different answer to how quickly a change is noticed: 15 to 45 s here, and retries cover the gap.