SD System Design In Depth
0%
lesson system-designcoordination

Metadata service and node discovery

Module 3.2: Distributed Coordination, Consensus & Leases Updated Sep 17, 2026
📄 Cheat Sheet
Lecture 8: Zookeeper
MIT 6.824: Distributed Systems Lecture 8: Zookeeper Watch on YouTube ↗

MIT 6.824's ZooKeeper lecture (Robert Morris) explains the paper's API, ephemeral and sequential znodes, watches, and its consistency guarantees in depth. It is the canonical treatment of a coordination and metadata service.

0:00 / --:--

The Role of Distributed Coordination Systems

Modern microservices and distributed storage systems (Kubernetes, HBase, Hadoop, and Kafka before KRaft) require a centralized, strongly consistent brain to manage cluster topology, service IP discovery, active leader registration, and feature flags. This is the domain of Distributed Consensus Key-Value Stores: Apache ZooKeeper, CoreOS Etcd, and HashiCorp Consul.

Hierarchical Node Trees & Ephemeral Sessions

flowchart TD Root["/ (Root Namespace)"] --> Services["/services"] Root --> Locks["/locks"] Services --> Payment["/services/payment-service"] Payment --> P1["/services/payment/node_01 (Ephemeral: 10.0.1.5:8080)"] Payment --> P2["/services/payment/node_02 (Ephemeral: 10.0.1.6:8080)"] Watcher["Client API Gateway: Long-Polling / Watcher on /services/payment"] -.->|"Push Event: Node_01 Disconnected"| P1

Under the Hood: Key Architectural Primitives

1. Ephemeral Nodes & Heartbeat Leases

When a payment service instance boots, it opens a session with ZooKeeper/Etcd and registers an Ephemeral Node at /services/payment-service/node_01 with its IP and port. Ephemeral nodes exist only as long as the client's heartbeat session remains active. If the instance crashes, the session times out, and the cluster manager automatically deletes the ephemeral node.

2. Event Watchers (Push Notifications)

Clients avoid polling the metadata service. Instead, they register Watchers on parent nodes (e.g., watch('/services/payment-service')). Etcd uses HTTP/2 multiplexed streams, while ZooKeeper uses persistent TCP connections. When a child node is added or deleted, the server pushes an event to all subscribed gateways, which then update their routing tables. Classic ZooKeeper watches are one-shot triggers: the client must re-read the node and re-register the watch, so changes in between are seen only through that re-read. Persistent and recursive watches arrived in ZooKeeper 3.6. etcd watches are streams that can resume from a revision.

3. Distributed Leader Election via Sequential Ephemeral Nodes

Candidates create a sequential ephemeral node: /election/leader_000000001, /election/leader_000000002. Whichever candidate holds the lowest numerical suffix is the elected leader. Non-leaders watch the node immediately preceding them in sequence, eliminating the 'herd effect' where all nodes wake up on leader crash.

Worked example: service registration with an etcd lease

  1. An instance of payments starts up and grants a lease with a 10-second TTL (lease ID 0x6b).
  2. It writes /services/payments/10.0.1.5:8080 attached to lease 0x6b.
  3. It sends a KeepAlive every 3 seconds.
  4. The gateway watches the prefix /services/payments/ starting from revision 5,120, and receives a PUT event.
  5. The instance is killed by the OOM killer. KeepAlives stop, and within about 10 seconds etcd revokes the lease and deletes the key.
  6. The gateway receives the DELETE event at revision 5,187 and removes the instance from its pool.

The worst-case detection time is the TTL. A shorter TTL detects failures faster but risks false expiry during GC pauses or network blips. Most deployments land between 5 and 30 seconds and also use active health checks at the load balancer.

Discovery approaches

ApproachConsistencyFailure detectionExamples
Consensus KV with sessions or leasesLinearizable writesLease or session expiryZooKeeper, etcd, Consul catalogue
Gossip membershipEventualSWIM or phi-accrualCassandra, Serf, memberlist
DNS-basedEventual, and cached by TTLExternal health checksKubernetes Services, AWS Cloud Map
Self-preservation registryAP by designHeartbeats, but eviction pauses during mass lossNetflix Eureka
Control plane pushes to proxiesEventual (xDS)Health checks plus outlier detectionEnvoy with Istio, Consul Connect

How real systems do it

  • Kubernetes stores all cluster state in etcd. Controllers use list-then-watch from a resourceVersion, and the API server caches state so clients do not hammer etcd.
  • Kafka kept broker registrations and controller election in ZooKeeper for a decade, then replaced it with KRaft (a Raft metadata quorum inside Kafka). ZooKeeper mode was removed in Kafka 4.0.
  • HBase finds the meta table and the active master through ZooKeeper, and RegionServers register ephemeral znodes.

Failure modes

  • Herd effect when thousands of clients watch one key. Use the sequential "watch your predecessor" recipe, or a caching layer.
  • Session expiry storms after a coordinator pause: many ephemeral nodes vanish at once and trigger mass rebalancing.
  • An etcd database that fills its quota (2 GB by default) raises a NOSPACE alarm and the cluster becomes read-only until you compact and defragment it.
Check yourself Question 1 of 3
One-shot watches recall
From: Under the Hood: Key Architectural Primitives

A gateway uses a classic (pre-3.6) ZooKeeper watch on /services/payment and receives a notification that a child changed. What must it do so it does not miss later changes?

Key Takeaways

Distributed metadata stores (Etcd, ZooKeeper) provide strongly consistent consensus-backed coordination.
Ephemeral nodes automatically unregister crashed instances when client heartbeat sessions expire.
Event watchers use HTTP/2 streaming or persistent TCP to push topology updates instantly to API gateways.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue