Zookeeper Service Availability

Zookeeper is a service that can be replicated so that one can have multiple servers running on different machines, essentially having a distributed Zookeeper service. This blog post explains, using a Ruby example, when Zookeeper service is available and when not.

This is the 2nd story on our encounter with Zookeeper. If you have not read the first one, you can read it here.

Client Switches To Available Server

Having a cluster of multiple Zookeeper servers, a Zookeeper client can connect to anyone of them and start requesting information and issuing commands. But, what does it happen if the server, that the client is connected to, goes down? The client is designed in such a way so that it can pick up the next available server in order to continue with its interaction with the Zookeeper cluster.

Let’s see how this is done.

Note: Please, read the first part of these Zookeeper stories in which we explain how you can set up 3 Zookeeper servers to run on your local machine.

Start 3 Zookeeper Servers

Let’s start 3 Zookeeper servers on different terminals. We will use the start-foreground option, so that we can watch their logs:

Start 1st Server

In Zookeper folder:

Start 2nd Server

Using another terminal window start 2nd server:

In Zookeeper folder:

Start 3rd Server

Using another terminal window start 3rd server:

In Zookeeper folder:

Write Ruby Client

Now, let’s write the Ruby client program. Save the following as main.rb:

In your working folder:

This is a very simple client that will be fetching data for the znode /test, repeatedly, sleeping for 5 seconds between consecutive fetches. On line 3, we tell this client that we have a cluster of 3 Zookeeper services. The idea here is that the client will pick up one and set up a connection with it, in order to do the fetches. It will be continuously connected to this server, as long as this server is alive. However, if this server fails for some reason, it will automatically switch to the next available server.

Note: We assume that you have a znode created with the name /test. If you don’t have that, read the first part of these Zookeeper stories, in order to learn how you can do it.

Start Ruby Program

Now start the Ruby program and watch the logs of the 3 servers.

Note: I am assuming that you have a Gemfile created in your project folder with the following content:

In your working folder:

Watching Logs

On one of your servers you will see something like this:

This will be the server that would have accepted the connection from your client. Note that this is not necessarily the first server.

Watch the id of the established session (look at line 4 in the log above). In my case this was 0x25e562c9e7a0000. This is the unique session id and you will notice that it will persist even if we switch to another server.

sb-tech-site-technology

Kill Server that Serves Client

Now, go ahead and use Ctrl + C to stop the server that received the client connection. When you do that, you will see another server picking up, and your Ruby client will continue fetching data.

This is what you will see on another server:

That is a long output. But the last 4 lines show that the Client attempting to renew session 0x25e562c9e7a0000. This is very clear. Our client does not give up when the original server fails. It automatically tries to connect to another server and renew the session. Look at the session id displayed now: 0x25e562c9e7a0000. It is the same session id that was printed by the server the client was originally connected to.

The above experiment proves that:

  1. Our client will try to reestablish a failed connection, by connecting to another server in the Zookeeper cluster.
  2. It will connect to the new server and will keep on being in the same Zookeeper session.

Kill Zookeeper Again

Now, go ahead and kill the next Zookeeper server, the one that our client is talking to at the moment. What will happen? Out of the 3 initial Zookeeper servers, we are going to have only 1. How will client behave?

Unfortunately, this is a situation in which Zookeeper cluster will not work and client will get a session time out exception, and, stop sending requests to fetch data. It will terminate.

What has happened here is that Zookeeper cluster is not working because there are not enough servers to make sure that we have proper replication and Leader election. Number of live servers is less than the majority/quorum needed in order for the cluster to work.

Stop the Last Server

Now, go ahead and stop the last server too.

Repeat with 5 servers

You can repeat the previous experiment but with 5 Zookeeper servers in the cluster. You will see that Zookeeper fails to serve client requests when you are left with 2 servers.

Although you may have 2 Zookeeper servers running (out of 5 initially in the cluster), the communication between the client and the Zookeeper cluster fails to be reestablished. This is, again, because the number of Zookeeper servers running are less than the majority of the servers being alive. The number of the servers being alive and running, it needs to be greater than or equal to the majority (quorum) of the servers in the cluster. Otherwise, Zookeeper cluster does not serve requests.

In more formal terms, the quorum is calculated by the formula ceil(N/2), where N is a number of servers in a cluster (or ensemble in Zookeeper nomenclature). For a 3 server ensemble, that means 2 servers must be up at any time, for a 5 server ensemble, 3 servers need to be up at any time.

Note: The quorum is necessary in order for the Leader to be selected. We will talk about the Leader in a following story.

Closing Note

In this blog post:

  1. We have demonstrated how the Zookeeper client is clever enough to switch to another server when the originally connected server goes down.
  2. Also, we have demonstrated how Zookeeper client keeps on using the same Zookeeper session (identified by the same id) even if it switches to another server.
  3. We have also seen that the Zookeeper cluster needs the majority of the servers alive in order to work. Otherwise, it is not possible to serve requests, being unable to select a Leader.

Thank you for reading this blog post. Your comments below are more than welcome. I am willing to answer any questions that you may have and give you feedback on any comments that you may post. Don’t forget that, I learn from you as much as you learn from me.

About the Author

Panayotis Matsinopoulos works as Development Lead at Simply Business and, on his free time, enjoys giving and taking classes about Web development at Tech Career Booster.

Ready to start your career at Simply Business?

Want to know more about what it’s like to work in tech at Simply Business? Read about our approach to tech, then check out our current vacancies.

Panos G. Matsinopoulos

This block is configured using JavaScript. A preview is not available in the editor.