← Building blocks

Find services and config at runtime

Observability & operationssenior

Hard-coded hostnames and config files that need a redeploy to change.

Say it like this

Instances come and go and limits change daily, so services find each other through a registry that health-checks its members and read config from a watched store, with the last good copy cached locally so neither is a hard dependency at request time.

Basic solution

Register instances in a discovery service and read config from a central, watched store.

ConsuletcdKubernetes DNSEureka

Registration, health, watches and the local cache

Instances register and are health-checked; clients watch the healthy list and the config keys. Every client keeps a local copy of the last good state so the registry is never on the request path.

How it works

  1. When an instance starts, it registers itself in the discovery service with its name, address, port and metadata such as version and zone. From then on it either sends heartbeats on a schedule or answers health checks the registry sends it.
  2. A client that needs a service asks the registry for the healthy instances of that name, caches the list in memory, and either watches for changes or re-polls every few seconds. It balances its own calls across the cached list, directly or through a sidecar proxy next to it.
  3. When an instance stops heartbeating or fails its check, the registry removes it from the healthy list within a few seconds, and every watcher drops it from its cached copy.
  4. Configuration (rate limits, feature settings, provider endpoints) lives in a central key-value store that versions each key. A service reads its keys once at start and subscribes to changes, so a new value reaches it without a restart.
  5. Each service keeps the last successfully loaded configuration both in memory and on local disk. If the config store becomes unreachable, the service keeps running on that copy and raises an alert; it never blocks a request or a restart waiting for the store.
  6. Changes go through the same safety as code: a schema validates the value before it is written, a changelog records who changed what, and the change rolls out to a percentage of watchers first rather than to the whole fleet at once.

Discovery versus DNS

  • DNS maps a name to a set of addresses with a cache lifetime of seconds to minutes and knows nothing about health. A registry gives a health-filtered membership list that changes within seconds and carries metadata. Kubernetes blurs the two by rewriting its DNS from its own registry.

Worked example

Rate limiter rules and the Redis shard map

  • 60 API gateway instances need the current rule set (800 rules, 120 KB as JSON) and the address list of 12 Redis shards.
  • Config store: etcd or Consul KV holding rules under /ratelimit/rules with a version. Gateways load it at start (one 120 KB read) and watch the key; a change fans out to 60 watchers in under a second. Operators change a limit with a PUT; no deploy.
  • Validation: a schema check runs before the write; a rule with window_seconds = 0 is rejected at the store, not discovered at 3 a.m. by every gateway dividing by zero.
  • Discovery: each Redis shard registers with a health check every 2 seconds. When shard 7's primary fails and the replica is promoted, the registry entry updates and the gateways re-resolve within about 5 seconds; before that, their circuit breaker on shard 7 is already failing open.
  • Local cache: each gateway writes the last good rules and shard map to /var/cache. During a 10-minute etcd outage the gateways keep enforcing the last rules and alert on config staleness; nothing on the request path waits on etcd.
  • Scale check: 60 watchers plus 12 registrations is tiny for etcd, which handles thousands of watchers; the point is correctness and the fast fan-out, not throughput.

Variants and when each wins

Client-side discovery (Consul, Eureka, etcd)

  • Each client queries the registry and balances across the instances itself. It wins on the number of network hops; every client needs a registry-aware library.

Server-side discovery (load balancer, Kubernetes Service)

  • Clients call one stable virtual address and the platform routes each call to a healthy backend. It wins for client simplicity; it adds one hop and the platform has to do the health filtering.

Service mesh (Envoy sidecars with a control plane)

  • Discovery, retries, mutual TLS and metrics move into a sidecar proxy next to every instance. It wins once the fleet is large and polyglot because behaviour is uniform across languages; it is a sizeable control plane to run.

DNS with short TTLs

  • Names resolve to addresses and the only health filtering is removing records. It wins when instances rarely change; it reacts slowly to failures.

Recommendation: the platform's built-in discovery (Kubernetes Services or a cloud balancer) for routing, a versioned KV store with watches for config, and a mesh only once the fleet is large enough to need uniform policy.

Failure modes

Config store becomes a request-path dependency

  • An outage of the config store takes every service down with it. It happens because services read configuration on each request, or block at start-up when the store is unreachable. Load once, cache locally, start from the cached copy, and alert on staleness.

Registry lag routes traffic to dead instances

  • A burst of connection errors lasts 30 seconds after a node dies. It happens because the heartbeat interval and the entry's TTL are long, so the dead node stays in the healthy list. Check every 2 to 5 seconds, and let clients drop an instance on their own after a few consecutive failures without waiting for the registry.

A bad config change hits the whole fleet at once

  • One typo in a limit rejects all traffic within seconds. It happens because a change fans out instantly with no validation or staging. Validate against a schema at write time, roll out to a percentage of watchers first, and keep a one-click revert to the previous version.

Split-brain registry

  • Half the clients see one membership list and half see another. It happens because the registry has no consensus between its nodes, or clients are pinned to a partitioned node. Use a consensus-backed store and have clients re-resolve through a quorum endpoint.

Secrets in the config store

  • Database passwords are readable by anyone with config access. It happens because the config store was the convenient place to put them. Keep secrets in a secrets manager with per-service identities; the config store holds references to them, not the values.

Interviewer follow-ups

Answer out loud before you open each one.

Why not just use DNS for service discovery?

DNS caches resolutions for a TTL and has no notion of health, so clients keep sending to a dead instance until the TTL expires and the record is updated. A registry removes a failed instance within seconds and can carry metadata such as version and zone for routing.

What happens to a service when the config store goes down?

Nothing visible if it was built right: it keeps running on the last config it loaded, which it cached in memory and on disk, and it raises a staleness alert. It must never block a request or a restart on the store being reachable.

How do you change a production config safely?

Validate against a schema at write time, record who changed what, roll the change out to a percentage of watchers first while watching the SLO metrics, and keep the previous version one click away. Treat it like a deploy, because to the system it is one.

How does this relate to consensus?

The registry and config store need one agreed view of membership and values even when nodes fail, which is exactly what etcd, ZooKeeper and Consul provide through Raft or ZAB. That is why they are small, consistent stores for metadata, never for high-volume data.

Connections

Pairs with
Agree on one leader or one value · Pull broken instances out of rotation · Ship dark, release gradually · Spread traffic across servers · Add nodes without remapping everything
Pulls against
Keep servers stateless
Used in
Distributed Key-Value Cache (Redis / Memcached-like) · Distributed Rate Limiter

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon ElastiCacheRedis shard 7MemorystoreAzure Managed RedisRedis, Valkey, Memcached
Amazon API GatewayAPI gateway ×60API Gateway, ApigeeAPI ManagementKong, Tyk