Course map

Topic 042 / Advanced

Service discovery

Chapter 3.8 · Edge Infrastructure

What and why

An API gateway (→ 41), or any service calling another service, needs the current network address of a healthy instance — but in a dynamic environment instances are added, removed, rescheduled, and fail continuously, so a hardcoded address breaks the moment anything changes. Service discovery is how services find each other's current, healthy instances: client-side discovery (the caller queries a registry and picks an instance itself) versus server-side discovery (the caller always talks to a fixed address — a load balancer or gateway — which queries the registry on its behalf), backed by a health-checked registry — as simple as DNS, or a dedicated system such as Consul or etcd as examples (full coordination-service depth → 136). Builds on: → 41.

Diagram

Client-side:   client ──► registry (lookup) ──► client picks an instance ──► service B

Server-side:   client ──► fixed address (load balancer / gateway)
                              │ (queries the registry itself)
                              ▼
                         service B instance

How it works

  1. Every service instance registers itself with the registry on startup, or is registered on its behalf by an orchestrator.
  2. The registry health-checks each instance and removes ones that stop responding.
  3. In client-side discovery, the calling service queries the registry directly, gets a list of healthy instances, and chooses one itself — needing its own load-balancing logic.
  4. In server-side discovery, the caller always addresses one fixed endpoint, and that layer does the registry lookup and forwarding, so the caller needs no discovery logic at all.
  5. DNS-based discovery (→ 26) is the simplest version of this — a name resolves to the current healthy set — trading DNS's own caching and TTL behavior against the cost of running a dedicated registry.

Practical example

(illustrative). Service A needs to call service B. With server-side discovery, A simply calls a fixed internal address, and whatever sits behind it has already removed B's unhealthy instances. With client-side discovery, A would query the registry itself, get back a list marking one instance unhealthy, and call one of the remaining healthy ones directly.

When to use it

  • Use when: instances are added, removed, or rescheduled dynamically — autoscaling or container orchestration make static configuration unable to keep up.
  • Prefer server-side discovery when: keeping discovery logic out of every service matters more than per-caller control over instance selection.
  • Avoid when: the fleet is small and fixed — DNS or static configuration is enough; a dedicated registry earns its cost only once the fleet is dynamic enough that static configuration visibly breaks.
  • Failure scenarios: a stale registry entry sends traffic to an instance that's already gone; a registry outage takes down the ability to find anything, so the registry itself needs its own availability story; relying on DNS TTL for discovery inherits DNS's caching lag (→ 26) and reacts slowly to a sudden failure.
  • Simpler alternative: static configuration or plain DNS for a small, stable set of instances.

Practice

(this topic contributes to the chapter's shared exercise below — mechanism kind; 20 min, no lab services). For the discovery hop in that diagram, state explicitly whether you modeled client-side or server-side discovery, and what happens to an in-flight request if the registry briefly can't answer. Done when the discovery mode is named and the registry-outage behavior is written down.

Key takeaway

Service discovery replaces a hardcoded address with a health-checked lookup — server-side discovery keeps that lookup out of every caller, at the cost of depending on the registry's own availability. Done when you can: explain what breaks, and what doesn't, when the registry is briefly unreachable. Used next in → 136 (coordination services, full depth); → 147 (inter-service communication, Part 9).

References

None — this topic makes no external claims.

Exercise: Diagram a request path — client → CDN → API gateway → service A → (discovery) → service B → database — labeling what each hop adds and what happens when each hop fails.

Source directory

Search the guide