how to re-register zookeeper watches
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
ZooKeeper watches are not continuous subscriptions by default. A standard watch fires once, and if you still care about future changes, your client must set the watch again as part of handling the event or performing the next read.
Understand the One-Time Nature of Watches
This is the most important rule: a watch is consumed when the matching event occurs. If you call getData, exists, or getChildren with watch enabled, ZooKeeper sends a notification when something changes, but that watch does not stay active forever.
That means the normal pattern is:
- read node state while setting a watch
- receive a watch event
- read again and set a new watch
If you skip step three, future updates on that path will not trigger another notification.
Re-Register Inside the Watch Handling Flow
In Java, the most common approach is to perform another read inside the watcher callback and set the watcher again.
The key point is that getData(..., this, ...) both reads the latest state and installs the next watch in one step.
Choose the Right Read Method
Different read calls watch different kinds of changes:
- '
exists(path, watcher)watches node creation, deletion, and data changes depending on the current state' - '
getData(path, watcher, stat)watches data changes and deletion' - '
getChildren(path, watcher)watches child-list changes'
You should re-register with the same semantic read that matches what you care about. Replacing getChildren with getData, for example, changes the meaning of the watch.
Handle Disconnects and Session Events Separately
ZooKeeper also sends connection-related events such as SyncConnected and Expired. Those are not normal znode events, and they need different handling.
If the session expires, ephemeral nodes disappear and previously registered standard watches are gone with the old session. The correct recovery path is usually:
- create a new
ZooKeepersession - rebuild any ephemeral state
- issue fresh reads that install fresh watches
Re-registering only inside the old callback is not enough after session expiration.
Prefer Persistent Watches When Available
Newer ZooKeeper versions added persistent watches. Those remain active until you remove them, which avoids the repeated re-registration cycle for many use cases.
Persistent watches simplify client logic, but you still need to handle reconnection, session state, and application-level consistency carefully.
Keep the Callback Lightweight
Watcher callbacks should be quick. If processing is expensive, hand the work off to another thread or executor. Blocking heavily inside the callback can make event handling harder to reason about and can delay follow-up reads that re-establish watches.
A common production pattern is:
- callback logs the event
- callback submits work to another component
- worker reloads current state and re-establishes the next watch
Common Pitfalls
- Assuming a standard watch stays active after the first event.
- Re-registering the wrong type of watch by calling a different read method.
- Ignoring session expiration and expecting old watches to survive it.
- Doing heavy business logic directly inside the watcher callback.
- Treating persistent watches as a reason to stop thinking about reconnection and state recovery.
Summary
- Standard ZooKeeper watches are one-time triggers and must usually be re-registered.
- Re-registration normally happens by reading the node again with watch enabled.
- Use
exists,getData, orgetChildrenbased on the kind of change you need to observe. - Session expiration requires full recovery, not just another callback action.
- Persistent watches reduce re-registration overhead but do not remove the need for sound client design.
Related reading
- How to read all the records in a Kafka topic
- How to read data using key in Kafka Consumer API?
- How to read json data using scala from kafka topic in apache spark
- How to read messages from kafka consumer group without consuming?
- How to re-sync the Mysql DB if Master and slave have different database incase of Mysql replication?
- How to reconnect to RabbitMQ?
- How to read records in JSON format from Kafka using Structured Streaming?
- How to reconnect kafka producer once closed?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.