Getting started with Apache Zookeeper

Apache ZooKeeper is a distributed, open-source coordination service for distributed systems. It helps to manage large distributed systems by providing a centralized service that allows nodes to communicate and exchange information.

In this tutorial, we will learn how to use ZooKeeper to coordinate the actions of distributed nodes.


Before starting this tutorial, you should have the following:

  • A Java development environment, including the JAVA_HOME environment variable set to the location of your Java installation.
  • Apache ZooKeeper installed on your system. You can download the latest release from the ZooKeeper website.

Setting up a ZooKeeper Ensemble

A ZooKeeper ensemble is a group of ZooKeeper servers that work together to provide a distributed coordination service. In order to use ZooKeeper, you need to set up at least one ZooKeeper server in your ensemble.

To set up a ZooKeeper ensemble, follow these steps:

  1. Unpack the ZooKeeper distribution file and navigate to the conf directory.
  2. Create a configuration file named zoo.cfg and add the following configuration parameters:

The tickTime parameter specifies the length of a tick in milliseconds. A tick is the basic time unit used by ZooKeeper for timeouts and other time-based operations. The initLimit and syncLimit parameters control the amount of time that a follower can be behind a leader before it is considered to be out of sync. The dataDir parameter specifies the directory where ZooKeeper will store its data files, and the clientPort parameter specifies the port that clients will use to connect to the ensemble.

  1. Create a file named myid in the dataDir directory and add the number 1 to it. This file is used to identify each server in the ensemble, and the number you specify should be unique for each server in the ensemble. For example, if you are setting up a three-server ensemble, you would use the numbers 1, 2, and 3 for each server, respectively.
  2. Start the ZooKeeper server by running the following command:
bin/ start

To set up additional servers in the ensemble, repeat these steps, using a unique myid number for each server and specifying the same dataDir and clientPort as the first server.

Stopping Zookeeper

You can conversely use the stop command to stop Zookeeper

$ stop

JMX enabled by default

Using config: /home/user1/zookeeper-3.4.6/bin/../conf/zoo.cfg

Stopping zookeeper ... STOPPED

Checking the status of ZooKeeper when it has stopped or is not running will show the following result:

$ status

JMX enabled by default

Using config: /home/user1/zookeeper-3.4.6/bin/../conf/zoo.cfg

Error contacting service. It is probably not running

Connect to the ZooKeeper server using a client

You can use the script to start a command-line client, or you can use one of the many client libraries available for various programming languages.

Create a new node in the ZooKeeper hierarchy. You can do this using the create command in the client, or by using the create method of a client library. For example:

create /app1 "Hello, World!"

This will create a new node called app1 with the value “Hello, World!”. As you can see from the following picture, Zookeeper organizes information using a hierarchical namespace:

Read the data stored in a node. You can use the get command in the client, or the getData method of a client library to read the data stored in a node. For example:

get /app1

This will return the value of the app1 node, which is “Hello, World!”.

Update the data stored in a node. You can use the set command in the client, or the setData method of a client library to update the data stored in a node. For example:

set /app1 "Hello, ZooKeeper!"

This will update the value of the myapp node to “Hello, ZooKeeper!”.

Delete a node. You can use the delete command in the client, or the delete method of a client library to delete a node. For example:

delete /app1

This will delete the app1 node.

That’s a basic tutorial on how to use ZooKeeper. There are many more features and capabilities available, such as the ability to create ephemeral nodes, watch nodes for changes, and manage ACLs (access control lists). You can learn more about these features in the ZooKeeper documentation.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon