Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A five-server ZooKeeper ensemble serves 60 services for configuration, discovery and leader election, with 1.2 million watches. After a 6-second GC pause on its leader, thousands of clients logged 'Session expired' and re-registered at once, and the ensemble stayed overloaded for 25 minutes.

Explain the cascade, how you would stabilise the ensemble, and how you would change the way services use it.

The 6-second GC pause stopped the ZooKeeper leader, so clients with short session timeouts expired; their ephemeral nodes vanished, watches fired everywhere, and every client reconnected, recreated nodes and re-read data at the same moment, which overloaded the ensemble and caused even more timeouts. We stabilise it by tuning the servers' JVMs and transaction log disk and bringing clients back gradually with jitter, then change how services use ZooKeeper: longer session timeouts with backoff, discovery and configuration moved elsewhere, and separate ensembles by criticality.

Why this question is asked

A shared ZooKeeper ensemble is often the quietest piece of infrastructure in a company until the day it takes everything down. Interviewers ask about this cascade because it shows whether we understand sessions, ephemeral nodes and watches, and whether we can see a feedback loop rather than a single failure. In this answer, we will recap how ZooKeeper sessions work, follow the cascade step by step, stabilise the ensemble during the incident, and then change both the client settings and the role ZooKeeper plays.

1A Quick Recap of Sessions, Ephemeral Nodes and Watches

A ZooKeeper client opens a session with the ensemble and keeps it alive with heartbeats. The session has a timeout, negotiated within the servers' bounds (minSessionTimeout and maxSessionTimeout, by default 2 and 20 times tickTime, so 4 to 40 seconds with a 2-second tick). If the ensemble hears nothing from a client for longer than the timeout, it expires the session. Session expiry is decided by the leader, the one server that orders all writes.

An ephemeral node is a znode tied to a session. Services use them to say "I am alive": a service instance creates /services/payments/instance-17 as ephemeral, and when its session expires ZooKeeper deletes the node. A watch is a one-time notification: a client reading the children of /services/payments can ask to be told when they change. With 1.2 million watches, a single change to a popular path can notify thousands of clients at once.

2How a 6-Second Pause Became a 25-Minute Outage

The leader paused for 6 seconds. During the pause no writes were ordered and session bookkeeping stopped. Clients with session timeouts of 4 or 5 seconds could not have their heartbeats processed in time, so when the leader resumed, their sessions were expired. That is the first step, and on its own it would be a small blip.

The trouble is what each expiry triggers. Every expired session deletes its ephemeral nodes, which are writes. Every deleted node fires the watches on its parent, so thousands of other clients wake up and re-read the children list. Meanwhile every expired client creates a new session, recreates its ephemeral nodes and re-registers its watches, all within the same second. Every write must be logged to disk with an fsync before it is acknowledged, so write latency climbs. When latency climbs past more clients' timeouts, more sessions expire, and the loop feeds itself.

Step throughThe expiry cascade
  1. Leader GC pause of 6 sno writes or session processing
  2. Short-timeout sessions expire4 to 5 s timeouts lose first
  3. Ephemeral nodes deletedeach delete is a logged write
  4. Watches fire on parentsthousands of clients re-read children
  5. Clients reconnect at oncenew sessions, new nodes, new watches
  6. Latency above other timeouts?
    YesMore sessions expire: loop againNoEnsemble recovers

6 steps in order. Play them, or step through them one at a time.

Each turn of the loop creates the load that causes the next turn.

3Stabilising the Ensemble During the Incident

While the loop is running, the goal is to remove load faster than it arrives. We pause or scale down the heaviest clients first, the ones with most watches and most churn, and then let services back in gradually, a few at a time, so that their reconnects are spread out. If clients reconnect immediately on failure, we restart them with a randomised delay rather than all together. A per-IP connection limit (maxClientCnxns) stops a single host from opening hundreds of sessions.

On the servers, the pause is the root cause. ZooKeeper keeps its whole data tree in memory, so we give it a fixed heap sized for the data with headroom (equal -Xms and -Xmx) and a low-pause collector such as G1 with a pause target, or ZGC, and make sure the host never swaps. The transaction log (dataLogDir) gets its own low-latency disk, because every write waits for its fsync; sharing that disk with snapshots or application logs makes write latency jump exactly when load is highest.

4Changing How Clients Behave

The client settings decide whether a pause turns into a storm. A session timeout of 4 seconds cannot survive a 6-second pause, and most services gain nothing from such fast detection. A timeout of 30 seconds, within the server's maxSessionTimeout, rides out realistic pauses. The connection timeout and the retry policy then decide how clients come back after a failure. With Apache Curator, the standard Java client library, we set both explicitly.

Java
CuratorFramework zk = CuratorFrameworkFactory.builder()
.connectString("zk1:2181,zk2:2181,zk3:2181,zk4:2181,zk5:2181")
.sessionTimeoutMs(30_000) // BEFORE: 4_000, shorter than a realistic pause
.connectionTimeoutMs(10_000)
.retryPolicy(new ExponentialBackoffRetry(1_000, 6)) // randomised, growing sleeps
.build();
zk.start();

ExponentialBackoffRetry sleeps a random multiple of the base time that grows with each retry, so thousands of clients spread their reconnects instead of arriving in the same second. We should also make sure our code does not re-read entire subtrees on every watch event; reading only what changed, or caching with Curator's cache recipes, cuts the read storm.

5Giving ZooKeeper Less to Do

The deeper problem is that one ensemble does three very different jobs for 60 services. Service discovery generates most of the churn: every pod start, stop and restart is an ephemeral node change that fires watches across the company. That job is better served by mechanisms built for it, such as Kubernetes Services and DNS, or a registry with client-side caching. Configuration can be read at startup and cached locally with a periodic refresh, so a ZooKeeper problem does not stop services from knowing their settings. What remains in ZooKeeper is what truly needs a strongly consistent store: leader election and a few small coordination records.

We then split the remaining usage by criticality, so that a noisy batch platform cannot expire the sessions of the payment services. Each ensemble gets monitoring on watch count, outstanding requests, average and maximum latency, and number of connections, exposed through ZooKeeper's metrics endpoint, with alerts long before the levels seen in this incident.

Where each use should live
Use todayBetter homeWhy
Service discovery (ephemeral nodes)Kubernetes Services, DNS or a caching registrymost churn and most watches
Configuration readslocal cache with refreshservices keep working if ZooKeeper is slow
Leader electionZooKeeper (or Kubernetes Lease)needs strong consistency
Small coordination recordsZooKeeper, separate ensemble per tiercritical, low volume

6Follow-up questions

Why not simply add more ZooKeeper servers?

More voting servers make writes slower, not faster, because every write needs acknowledgement from a majority, and a larger majority means more round trips. Extra observers, which do not vote, can absorb read and watch load, but they do not fix the leader pause or the synchronised reconnect storm. Reducing churn and fixing client behaviour does far more.

What metrics would have warned us before the incident?

A steadily rising watch count, outstanding requests above a few dozen, average latency creeping up, and GC pause times on the leader approaching the shortest client session timeout. The last one is the most direct warning: if the longest pause is close to the shortest timeout, a cascade is one bad pause away.

Is a long session timeout bad for leader election?

It delays failover, because a crashed leader's ephemeral node stays until its session expires. Thirty seconds is usually acceptable for leadership, and where faster failover matters we combine it with fencing so that the speed of detection is not what keeps the data safe.

7Conclusion

In this answer, we saw how a single 6-second GC pause expired short sessions, deleted ephemeral nodes, fired a storm of watches and triggered synchronised reconnects that kept the ensemble overloaded for 25 minutes. We stabilised it by shedding load, readmitting clients gradually and fixing the servers' heap, collector and log disk. Then we changed the clients, with session timeouts that survive pauses and randomised backoff, and reduced ZooKeeper's role to election and small coordination, split by criticality and properly monitored.

What to say in the interviewAbout 80 seconds aloud

Open with

This is a feedback loop. The leader's six-second GC pause stopped request and session processing, so clients with four or five second session timeouts were expired. Each expiry deletes ephemeral nodes, which fires watches on their parents, so thousands of clients re-read data, and all the expired clients reconnect, recreate their nodes and re-register watches at the same moment.

Then, if they want more

Every write needs an fsync on the transaction log, latency climbs past other clients' timeouts, and more sessions expire. To stabilise it I would shed load first: pause the noisiest clients, bring services back a few at a time with randomised delays, and limit connections per host.

On the servers I would fix the root cause with a fixed heap, a low-pause collector like G1 with a pause target or ZGC, no swapping, and a dedicated low-latency disk for the transaction log.

Then I would change client behaviour: session timeouts around thirty seconds within the server's bounds, and Curator's exponential backoff, which is randomised, so reconnects spread out. Longer term, I would shrink ZooKeeper's job.

Discovery moves to Kubernetes Services or DNS, configuration is cached locally with refresh, and ZooKeeper keeps only election and small critical coordination, split into ensembles by criticality, with alerts on watch count, outstanding requests, latency and GC pauses.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the cascade, how you would stabilise the ensemble, and how you would change the...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/b63d43ccb3/