Configuration and Coordination with Zookeeper

It took me a while to understand the concept of Zookeeper and it took me another some to understand how to use it for the task that I had begun with. This post is intended to help others cross the bridge faster.
Dynamic Configuration Management for today’s system comes with all the nitty-gritties that are involved with a distributed environment. A distributed environment is relatively unreliable, with problems like network failures, clock synchronization, and it becomes the responsibility of each server to keep track of correctness of current configuration. These problems are the motivation behind configuration and coordination systems.This particular post looks at Zookeeper as a configuration management tool. A short and good course on ZookeeperSetup: A zookeeper cluster with 5 servers. A java wrapper that uses zookeeper to keep track of configuration changes. For our purposes we used the Curator framework.Read zookeeper multi-server setup on a single host for setting up zookeeper cluster.In your java project include the dependency for the curator framework.   Initializing connection to Zookeeper ensemble

public static CuratorFramework createConnection(String zookeeperConnectionString) {
//First retry will wait for 1 second,
//the second will wait up to 2 seconds,
//the third will wait upto 4 seconds.
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.builder()
.zookeeperFactory(new DefaultZookeeperFactory())
return client;
view raw hosted with ❤ by GitHub

Creating a new znode

public static void create(CuratorFramework client, String path) throws Exception {
view raw hosted with ❤ by GitHub

Setting data of a znode

public static void setData(CuratorFramework client, String path, String data) throws Exception {
byte[] payload = data.getBytes();
.forPath(path, payload);
view raw hosted with ❤ by GitHub

Deleting a znode

public static void delete(CuratorFramework client, String path) throws Exception {
view raw hosted with ❤ by GitHub

Get children and set the given watcher on the node

public static List watchedGetChildren(CuratorFramework client, String path) throws Exception {
return client.getChildren()
.usingWatcher(new WatcherImpl(client,path))

Get Data of the node and set a watcher on the node

public static String getData(CuratorFramework client, String path) throws Exception{
String str = new String(client.getData()
.usingWatcher(new WatcherImpl(client,path))
return str;
view raw hosted with ❤ by GitHub

Zookeeper works on the idea of client setting watches on znodes. Whenever the znode changes, the watch is triggered, that is, the client is notified.We should have a class that implements the Watcher interface. This class should implement the process method. This is called if and when the corresponding changes occur.

public class WatcherImpl implements Watcher{
public void process(WatchedEvent event) {
if(event.getType() == Event.EventType.NodeDataChanged) {
System.out.println("The Data has changed");
else if(event.getType() == Event.EventType.NodeChildrenChanged){
System.out.println("Children have changed");
view raw hosted with ❤ by GitHub

Its important to note that watches are one time event and if its required to continue monitoring the changes the watch needs to be set again. All the read operations like getData(), getChildren() and exists() have the option of setting a watch. These watches are triggered on the corresponding changes.That’s all for getting the configuration management with zookeeper up and running.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s