Skip to content

What is ZooKeeper?

  • What is ZooKeeper used for?

    ZooKeeper is used to coordinate distributed software applications. It provides a small set of useful primitives for things like mutexs, ownership and producer/consumer queues. Best of all, ZooKeeper runs as a highly available cluster.

A ZooKeeper instance is made up of znodes. Znodes are basically key value pairs that are held in memory. You can perform actions on znodes like create, update, delete and watch. All znode actions are atomic, consistent and VERY fast. This makes ZooKeeper a great system for coordinating applications.

Here’s a Java example of how you might use ZooKeeper to check if the current node is the master.

				
					public class IsMaster impliments Watcher {
    ZooKeeper zoo;
    boolean isMaster;

    public IsMaster(String connectionString, byte[] myId) {
        zoo = new ZooKeeper(connectionString, 3600, this);
        isMaster = false;
        createNode();
    }

    createNode(byte[] myId) {
        try {
            // create znode /master if it doesn't already exist
            // make node ephemeral so it will auto-destruct if this session ends
            zk.create("/master", myId, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            isMaster = true;
        } catch (KeeperException e) {
            if(e.ErrorCode == KeeperException.NodeExists) {
                isMaster = false;
                return;
            }

            rethrow e;
        }
    }

    process(WatchedEvent event) {
        if (event.getType() == Event.EventType.None) {
            switch (event.getState()) {
            case SyncConnected:
                // nothing todo here
            case Expired:
                // our session ended
                isMaster = false;
                break;
            }
        }
    }
}
				
			

If you would like to read more, there is extensive documentation at https://zookeeper.apache.org/doc/current/zookeeperOver.html

Ready to get the whole story on your uptime?
 
Status List delivers uptime checks with technical diagnostics in a one dashboard. A pass/fail isn’t the whole story.
 
Join over 2,000 companies and try it for free today.