system-design · intermediate

Service Discovery — Finding Instances That Change

Start here

Service discovery solves:

“I need to call the payments service—what IPs/ports are healthy right now?”

In modern systems, instances are cattle: containers appear and disappear. Hard-coded addresses break. Discovery provides a name → set of endpoints mapping that updates with membership and health.

You should care because microservices multiply dependencies; without discovery (or an equivalent platform network), deploys and autoscaling become manual pain.

What you will learn

  1. Define service discovery in plain English.
  2. Contrast client-side vs server-side discovery.
  3. See DNS, registries (Eureka/Consul), and platform discovery (K8s).
  4. Connect health checks to removing bad instances.
  5. Work a complete checkout→payments example.
  6. Avoid stale caches and thundering reconnections.

What you should know first

TopicWhy
DNSClassic name lookup
Load balancingOften paired with discovery
Client–serverWho looks up whom

Words you need before we begin

TermPlain English
Service nameLogical name like payments.
Instance / endpointConcrete address of one process.
RegistryStore of service→instances.
Client-side discoveryCaller queries registry and picks an instance.
Server-side discoveryCaller hits a load balancer that knows instances.
Health checkProbe deciding if instance should receive traffic.
Sidecar / meshHelper proxy beside the app for discovery/routing.

Simple story: company directory

Employees change desks. Instead of memorizing seat numbers, you look up “Alice” in the directory (registry) or ask reception (load balancer) to route you. Out-of-date directories send you to empty desks—stale discovery.

The problem without discovery

Config file lists 10.0.0.5:8080 for payments. Autoscaler adds new tasks; old task dies. Callers break until someone edits configs and restarts everything.

Step-by-step explanation

Step 1 — Register on start

Instance announces payments @ 10.0.1.23:8080 with metadata (version, zone).

Step 2 — Heartbeat / lease

Registry removes instances that miss heartbeats (or platform API reflects pod deletion).

Step 3 — Discover

Caller resolves payments to a list or LB VIP.

Step 4 — Select

Round-robin, random, least-conn, or zone-aware choice.

Step 5 — Health gate

Failing readiness checks → removed from pool.

Step 6 — Cache carefully

Clients cache lookups with short TTL; stale caches cause errors after deploys.

Step 7 — Observe

Track discover failures, empty pools, and call error rates after deploys.

Visual mental model

flowchart LR
  SvcA[Service A] -->|resolve payments| Reg[Registry / DNS / platform]
  Reg --> LB[Load balancer or client picker]
  LB --> P1[Payments-1]
  LB --> P2[Payments-2]

Learning question: Who removes an instance that is still registered but returns 500s?

Caption: Health checks and outlier detection—not only registration TTL.

Complete worked example: Kubernetes-style

Starting situation

Checkout pods must call payments pods. Both autoscale.

Decisions

ItemChoice
MechanismKubernetes Service + kube-proxy/IPVS or mesh
Namepayments.namespace.svc.cluster.local
Readiness/health/ready checks DB connectivity
TimeoutsClient 200–500ms with retries limited

Flow

Checkout uses cluster DNS to service VIP; platform maps to ready pods. Pod dies → endpoint removal → traffic shifts.

Failures

How it works in production

Failure modes

ModeImpactMitigation
Stale client cacheCall dead tasksShort TTL; watch APIs
Missing health checksBlackhole trafficReadiness probes
Split registriesWrong environment callsStrict config, mTLS
Thundering discoverRegistry overloadCaching, platform VIP
Empty poolTotal dependency failureAlerts; degrade

Trade-offs

ChoiceBenefitCost
Client-side discoveryFlexible LB logicClient complexity
Server-side discoverySimple clientsLB critical path
Platform-nativeLess custom codePlatform lock-in
MeshRich policyOperational weight

Compare with related concepts

ConceptDifference
DNS aloneMay lack active health without extras
Load balancingUses discovered targets
Service meshContinuous discovery + policy
API gatewayNorth-south edge; discovery is often east-west

Common misunderstandings

  1. “DNS is enough everywhere.” Health and multi-port realities vary.
  2. “Discovery replaces timeouts.” Still need client resilience.
  3. “Register healthy forever.” Must deregister and probe.
  4. “Only microservices need this.” Any dynamic fleet does.
  5. “Client libraries always agree.” Standardize platform patterns.

Check your understanding

  1. What question does discovery answer?
  2. Client-side vs server-side discovery?
  3. Role of readiness probes?
  4. Risk of long client-side DNS TTL?
  5. Name one platform discovery mechanism.

Practice

  1. Draw discovery for three services in a cluster.
  2. Design metadata: version, zone, weight.
  3. Write a runbook for empty endpoint sets.
  4. Compare Consul vs Kubernetes services conceptually.
  5. List metrics for discovery health.

Deeper production notes

Warmup and registration races

Instances may accept traffic before warm. Use readiness gates and gradual traffic.

Multi-cluster

Cross-cluster discovery needs explicit design (gateways, service entries). Do not assume single-cluster DNS works globally.

Additional teaching scenarios

Scenario A — peak load day

Traffic multiplies by ten. Mark which failure modes appear first and the first mitigation for each.

Scenario B — mixed versions

Half the fleet runs an old build. Which assumptions break? Prefer one deploy window of compatibility.

Scenario C — five-sentence teach-back

Explain the core idea without acronyms using only the simple story and worked example.

Scenario D — metrics and alerts

List three metrics and one alert that track user impact or a scarce resource.

Scenario E — non-goals

Name two problems this technique should not solve.

Scenario F — ownership

Who owns dashboards, code, and pages? Blank means not ready for broad rollout.

Revision summary

Glossary

TermDefinition
Service discoveryDynamic lookup of service endpoints.
RegistryMembership store for instances.
ReadinessSignal that instance can receive traffic.

Abbreviations and terminology

What to learn next

  1. Load balancing
  2. Microservices architecture
  3. API gateway
  4. DNS

Extra teaching notes for first-time builders

Write the single bottleneck you are protecting before picking tools. Name the signal that tells you the design is working for users, not only that internal counters move. When reviewing a change related to this lesson, ask what happens when the component is slow for ten minutes, down entirely, or running twice. Prefer small explicit failure modes that operators can understand at 3 a.m.

Document ownership for dashboards, code, and pages. Undocumented mechanisms become folklore and then outages. Prefer designs that tolerate mixed versions for at least one deploy window so rollouts do not require perfect global simultaneity.

Napkin math helps: estimate peak rate, multiply by payload size, and ask whether the design still holds when a dependency is at half capacity. If the answer depends on luck, add bounds, backpressure, or shedding before production traffic arrives.

Extra teaching notes for first-time builders

Write the single bottleneck you are protecting before picking tools. Name the signal that tells you the design is working for users, not only that internal counters move. When reviewing a change related to this lesson, ask what happens when the component is slow for ten minutes, down entirely, or running twice. Prefer small explicit failure modes that operators can understand at 3 a.m.

Document ownership for dashboards, code, and pages. Undocumented mechanisms become folklore and then outages. Prefer designs that tolerate mixed versions for at least one deploy window so rollouts do not require perfect global simultaneity.

Napkin math helps: estimate peak rate, multiply by payload size, and ask whether the design still holds when a dependency is at half capacity. If the answer depends on luck, add bounds, backpressure, or shedding before production traffic arrives.

Extra teaching notes for first-time builders

Write the single bottleneck you are protecting before picking tools. Name the signal that tells you the design is working for users, not only that internal counters move. When reviewing a change related to this lesson, ask what happens when the component is slow for ten minutes, down entirely, or running twice. Prefer small explicit failure modes that operators can understand at 3 a.m.

Document ownership for dashboards, code, and pages. Undocumented mechanisms become folklore and then outages. Prefer designs that tolerate mixed versions for at least one deploy window so rollouts do not require perfect global simultaneity.

Napkin math helps: estimate peak rate, multiply by payload size, and ask whether the design still holds when a dependency is at half capacity. If the answer depends on luck, add bounds, backpressure, or shedding before production traffic arrives.

Extra teaching notes for first-time builders

Write the single bottleneck you are protecting before picking tools. Name the signal that tells you the design is working for users, not only that internal counters move. When reviewing a change related to this lesson, ask what happens when the component is slow for ten minutes, down entirely, or running twice. Prefer small explicit failure modes that operators can understand at 3 a.m.

Document ownership for dashboards, code, and pages. Undocumented mechanisms become folklore and then outages. Prefer designs that tolerate mixed versions for at least one deploy window so rollouts do not require perfect global simultaneity.

Napkin math helps: estimate peak rate, multiply by payload size, and ask whether the design still holds when a dependency is at half capacity. If the answer depends on luck, add bounds, backpressure, or shedding before production traffic arrives.

FAQ from first-time learners

Q: Is a load balancer the same as discovery?
A: LB distributes among known targets; discovery maintains the target list.

Q: Do serverless functions need discovery?
A: Platforms often invoke by name/ARN—discovery is abstracted.

Q: How fast must updates be?
A: Fast enough for your deploy/scale events; measure error spikes during rollouts.

Track: Software Design and Architecture

Previous: Serverless Architecture — Managed Compute on Demand

Next: Splitting a Monolith Safely (Strangler Fig)

By Shubham Jain

All articles · Study paths

Shubham Jain · Learning Lab