Back to blog

Nine services and counting: who talks to whom?

Thắng Đoàn
Thắng Đoàn

FotoFlow's feed service calls Photos using a list of IP addresses in its config file. At 3 a.m. the autoscaler replaces one instance, and the old address now has nothing listening on it. Half of Feed's calls go into the void while the new instance sits idle, and nobody edited a config file, which is the point. Any address written into config goes stale. In an autoscaled system that happens many times a day.

The registry: a live phone book

Every instance registers itself with a service registry when it starts, then keeps sending heartbeats every few seconds. Feed asks the registry where Photos is and gets the current list, which it caches for a few seconds so the registry never becomes a bottleneck. An instance that hangs misses three heartbeats and is dropped; a new one registers and gets traffic within seconds. Consul, etcd, ZooKeeper, Eureka, and Kubernetes, which does all of this for every service automatically.

Discovery can sit in the client, which picks an instance itself, or in a proxy: Kubernetes gives every service a stable name and virtual IP and looks up instances for you. DNS can answer too, photos.internal, but some runtimes cache DNS answers forever by default, so keep TTLs short and check yours before relying on it.

Alive is not ready

A new instance registers the moment its process starts, before it has opened database connections or warmed its cache. The registry hands its address out, it receives a full share of traffic, and for 30 seconds a third of calls time out. Being alive is not the same as being ready.

So two checks are separated: liveness, the process is running, and readiness, it can serve requests now. An instance is listed only once it passes readiness, and balancers ramp its share up gradually, a slow start. Shutdown is the mirror image: deregister first, let in-flight requests finish, then stop. Kubernetes ships these as readinessProbe, livenessProbe, and a preStop hook.

What this does not solve

Everything now depends on the registry, so it cannot be a single point of failure. A registry must never be a single point of failure, so it runs as a small consensus cluster of 3 or 5 nodes agreeing by majority vote, the Raft consensus from an earlier part; odd sizes matter, 3 nodes survive 1 failure, 5 survive 2. Callers also keep their last known list cached, so traffic flows even while the registry is unreachable.

The other trap is rebuild: every service-to-service call also needs timeouts, retries, circuit breakers, encryption and metrics, and nine services in four languages means nine hand-rolled versions with nine sets of bugs. A service mesh (Istio, Linkerd) puts a sidecar proxy next to every service that does it all once: finding instances, retrying, timing out, mutual TLS, reporting. It costs two local hops and real operational complexity, so it pays off with many services and is overkill for a handful. At the edge, an API gateway remains the one front door for outside clients: authenticate, rate-limit, terminate TLS, route each path to the right service, and keep your internal layout private so you can split and merge services freely.

The next instance to be replaced will be replaced tonight. Does your calling code learn about it in seconds or at the next deploy?
A new instance gets full traffic the second it boots. What breaks in the first 30 seconds?

Services can find each other. Next in the series: A step fails halfway through five services: how do you roll back?

Sources

Martin Fowler: Microservices

System Design Primer

The Raft Consensus Algorithm

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.