Reliable coordination
for distributed services.
Apache ZooKeeper is a centralized coordination service: it keeps configuration, state, and location data in a tree replicated across several servers, which stays available as long as a majority of them are up. Follow a write through the ensemble and see how ephemeral znodes elect a new leader when a process fails.
ZooKeeper keeps a tree of znodes, replicated across an ensemble of servers, which distributed services use to coordinate with each other.
In TDP Datacenter, HDFS, YARN, HBase, Kafka, and other services in the stack depend on it.
An in-memory tree
Znodes, like files and directories
Each znode can have data and children at the same time, is read and written as a whole, and holds small amounts of coordination data. The entire tree is kept in memory on each server.
One order for everything
Every write goes through the leader
Reads are served by the server the client is connected to. Writes go to the leader, which numbers them and only confirms them once a majority of the ensemble has recorded them.
Presence and notifications
Sessions and watches
An ephemeral znode exists as long as the session that created it is active, and a watch notifies the client when a znode changes. With the two, clients elect leaders and implement locks.
On this page
- See a write in the ensemble
- Architecture
- The znode tree
- See a leader election
- When something fails
- Why ZooKeeper
- In the TDP editions
See a write in the ensemble
Press Play or step through. One client writes a znode. Another, which watches it, receives the notification. Underlined names link to the Glossary.
1 Each client opens a session
Each client receives the list of servers in the ensemble, picks one, and connects, and ZooKeeper creates a session for it. Requests, responses, watch notifications, and heartbeats all travel over the same TCP connection.
- Each client opens a session. Each client receives the list of servers in the ensemble, picks one, and connects, and ZooKeeper creates a session for it. Requests, responses, watch notifications, and heartbeats all travel over the same TCP connection.
- A read is local. Client B reads the znode. Its own server responds, from its copy of the in-memory tree. In the same read, client B leaves a watch on the znode, kept on that server.
- The write goes to the leader. Client A asks to write the same znode. Writes change the state of the service, so its server does not apply them on its own: every write is forwarded to the leader.
- The leader proposes. The leader calculates the state the write will produce and turns it into a transaction, stamped with the next zxid. It sends the proposal to all followers, in the order it received the requests.
- The followers record and acknowledge. Each follower writes the proposal to disk, in its transaction log, and acknowledges it to the leader. The acknowledgements arrive in the same order as the proposals.
- A majority is enough. When a quorum, a majority of the servers, has acknowledged the proposal, the leader sends the COMMIT to all. The leader also counts toward this quorum. Each server applies the transaction to its copy of the in-memory tree, in the same order.
- The response and the notification. Client A's server responds that the write succeeded. Client B's server triggers the watch: client B receives the notification that the znode changed, and the watch is removed. To learn about the next change, client B reads again and leaves another watch.
Architecture
Each client connected to one server. In the ensemble, a leader, the followers, and, if any, observers.
What the figure shows:
- The clients each keep a session with a single server of the ensemble, over which requests, responses, watch notifications and heartbeats travel. If the connection drops, the client moves to another server in the list.
- The ensemble gathers the ZooKeeper servers, which know each other.
- The followers vote on the leader's proposals, agreeing on delivery, and serve clients.
- The leader receives all writes and orders them (zxid).
- The observers serve clients like followers do, but only receive the result of the votes, without voting.
- The agreement protocol links the leader to the followers: writes go through it, with proposals, acknowledgements and COMMIT.
- The znode tree and the transaction log live on each server: each one keeps a copy of the entire tree in memory, with the transaction log and the snapshots on disk.
The znode tree
A path in the tree, part by part. Click each part of the name.
Click each part of the path.
Chroot
A client can append a chroot to the connection string. Its paths then become relative to that root: what the client calls /election is, on the servers, at /app/a/election. This way, each application uses the same ZooKeeper as if it were at the root.
See in the Glossary/app/a— Chroot- A client can append a chroot to the connection string. Its paths then become relative to that root: what the client calls /election is, on the servers, at /app/a/election. This way, each application uses the same ZooKeeper as if it were at the root.
election— Parent znode- The znode the application chooses for a leader election. It has children, and it is the one that keeps the counter used to number the sequential children.
guid-n_0000000003— Ephemeral sequential znode- Created with the EPHEMERAL and SEQUENCE flags. ZooKeeper appends to the name an increasing counter, unique under the parent and zero-padded to ease sorting, and deletes the znode when the session that created it ends. For this reason, an ephemeral znode has no children.
Paths are always absolute and slash-separated, with no relative references, and the name zookeeper is reserved.
Besides its data, each znode holds a state structure, the stat, and an access control list.
Data
Read and written as a whole
A read returns all of the znode's data, and a write replaces all of it. This is small coordination data. For large volumes, the practice is to keep the data in a storage system such as HDFS and leave only the pointer to it in ZooKeeper.
Stat
Versions and zxids
The stat holds the zxids of creation and of the last change, the timestamps, and the versions of the data, the children, and the ACL. An update that supplies a version different from the current one fails: this is the basis of conditional updates.
Access
One ACL per znode
A znode's ACL applies only to it, not to its children. It grants the CREATE, READ, WRITE, DELETE, and ADMIN permissions to identities from schemes such as world, auth, digest, ip, and x509.
See a leader election
Three processes of an application compete for leadership using ephemeral sequential znodes. Press Play or step through.
1 Each process becomes a candidate
Each process creates, under /election, a znode with the SEQUENCE and EPHEMERAL flags. ZooKeeper appends to each name a sequence number greater than all previous ones, in the order the creations arrived.
In the Glossary: Process A · Process B · Process C · guid-n_…01 · guid-n_…02 · guid-n_…03 · Sequential znode
- Each process becomes a candidate. Each process creates, under /election, a znode with the SEQUENCE and EPHEMERAL flags. ZooKeeper appends to each name a sequence number greater than all previous ones, in the order the creations arrived.
- The smallest number leads. Each process lists the children of /election. The one that created the znode with the smallest sequence number is the application's leader: here, process A.
- Each one watches the one before. Instead of all watching the leader's znode, each process leaves a watch only on the znode immediately before its own: B watches A's, and C watches B's. This way, a failure wakes a single process, not all of them: this is what avoids the herd effect.
- The leader fails. Process A stops responding. While active, it kept the session alive with requests and heartbeats. Now the ensemble stops hearing from it and, after the session timeout, the session expires.
- The ephemeral znode disappears. With the session expired, ZooKeeper deletes all of its ephemeral znodes and notifies whoever was watching them. Only process B had a watch on A's znode, and only it is notified.
- The next one takes over. Process B lists the children of /election again. Its znode is now the smallest, and B starts running the leader role. Process C keeps watching B's znode.
The same pieces, sessions, ephemeral znodes, and watches, underpin the locks and high availability of other services.
The HDFS ZKFC keeps a session in ZooKeeper and, on the active NameNode, a lock on an ephemeral znode, automatically deleted if the session expires. The YARN ResourceManagers use ZooKeeper to decide which one is active.
When something fails
What happens to the clients and to the ensemble when a piece goes down.
The client library tries the next server in the list until it reconnects. If it returns within the session timeout, the session continues, and the registered watches are re-sent to the new server. While disconnected, the client does not receive notifications.
The ensemble decides the expiration, when it stops hearing from the client for the duration of the session timeout. The session's ephemeral znodes are deleted and whoever was watching them is notified. The client itself only finds out when it manages to reconnect.
Its clients move to other servers, and the service stays available as long as a majority of the servers are up. When the follower returns, it synchronizes with the leader.
The servers elect a new leader, which must have seen the highest zxid among them. The new leader starts a new epoch, sends each follower the proposals it is missing, or a full snapshot, and only accepts new writes after a quorum confirms it is following it.
The service needs a majority of the voting servers. This is why the ensemble usually has an odd number of servers: with an even number, it tolerates the same number of failures as the ensemble with one server fewer. See Quorum.
Because it does not vote, an observer can fail or disconnect without affecting the availability of the service.
Why ZooKeeper
Simple
A small API
Creating and deleting znodes, testing whether they exist, reading and writing data, listing children, and synchronizing: these are the operations of the API.
Fast reads
Data in memory
The tree is kept in memory and reads are served by the client's server. Performance is best where reads outnumber writes, the typical case for a coordination service.
Replicated
No single point of failure
The service runs on an ensemble of servers and stays available as long as a majority of them are up.
Ordered
One order for all changes
Each change receives a zxid, and a client's updates are applied in the order it sent them.
Guarantees
The same view on any server
Atomic updates, with no partial results. A client never sees an older view of the service, even when it switches servers within the same session, and what has been applied persists until overwritten.
Recipes
Primitives built on the API
Locks, barriers, queues, leader election, and two-phase commit are recipes that clients build on znodes, watches, and the ordering of updates.
Coordination services are hard to get right and prone to errors such as race conditions and deadlocks. ZooKeeper was created so that distributed applications do not need to implement them from scratch. At Yahoo!, it is the coordination and failure recovery service for the Yahoo! Message Broker.
In the TDP editions
| Edition | Version | Documentation |
|---|---|---|
| TDP Datacenter | 3.8.4 | TDP Datacenter components |
In TDP Datacenter
In TDP Datacenter, ZooKeeper is a service of the stack, installed and managed by Ambari, and is among the basic services in cluster creation.
ZOOKEEPER_CLIENT (client, one or more) and ZOOKEEPER_SERVER (master, one or more). Component category.
ZooKeeper Server runs on the machines that form the ensemble. Ambari provides a service check, and ZooKeeper Server supports rolling restart.
In the documented installation, ZooKeeper Server runs on an odd number of machines, greater than one, with its files, if possible, on dedicated disks, volumes, and directories. See Service installation and Quorum and service placement.
In the zoo.cfg configuration, the ensemble's time unit is set in tickTime. The time, in ticks, for followers to connect and synchronize with the leader is set in initLimit, and to stay synchronized in syncLimit. The client port is set in clientPort, and the data directory in dataDir.
Removal of old snapshots and transaction logs can be enabled in autopurge.purgeInterval, with the number of snapshots retained set in autopurge.snapRetainCount.
The maximum memory of each ZooKeeper Server is set in zk_server_heapsize, and the log and PID directories in zk_log_dir and zk_pid_dir, in the zookeeper-env configuration. Log rotation can be configured in zookeeper-log4j.
The permitted Four Letter Words commands are set in 4lw.commands.whitelist, and the embedded AdminServer can be enabled in admin.enableServer, with its port in admin.serverPort.
Configurable in zookeeper-ssl: the connection implementations in zookeeper.serverCnxnFactory and zookeeper.clientCnxnSocket, the use of secure connections by clients in zookeeper.client.secure, and the keystore and truststore in zookeeper.ssl.keyStore.location and zookeeper.ssl.trustStore.location.
ZooKeeper's Kerberos can be enabled together with the cluster's Kerberos, through Ambari, with the principal in zookeeper_principal_name and the keytab in zookeeper_keytab_path. See Security in TDP Datacenter.
These services require this one in the cluster: Infra Solr, Ambari Metrics, Druid, HBase, HDFS, Hive, Kafka, NiFi, Solr and YARN.
Each service points to the ensemble in its own configuration: yarn.resourcemanager.zk-address in YARN, hbase.zookeeper.quorum in HBase, hive.zookeeper.quorum in Hive, zookeeper.connect in Kafka, nifi.zookeeper.connect.string in NiFi, druid.zk.service.host in Druid, infra_solr_zookeeper_quorum in Infra Solr, and cluster.zookeeper.quorum in Ambari Metrics.
Services keep their data under their own znodes, configurable in Ambari: zookeeper.znode.parent in HBase, hive.zookeeper.namespace in Hive, nifi.zookeeper.root.node in NiFi, druid.zk.paths.base in Druid, and yarn.resourcemanager.zk-state-store.parent-path for the ResourceManager state. In Kafka, zookeeper.connect accepts a chroot.
The Ranger plugins for HDFS, YARN, HBase, Hive, Kafka, NiFi, and Solr reach the audit Solr through ZooKeeper, in xasecure.audit.destination.solr.zookeepers.
Next steps
TDP Datacenter
Quorum and service placement
The ensemble and the machines of the services in high availability.
TDP Datacenter
Service installation
The cluster creation wizard, with ZooKeeper among the basic services.
Architecture
High availability
The core services in pairs, with the switchover coordinated by ZooKeeper.
Sources
Apache ZooKeeper - Programmer's Guide
Apache ZooKeeper - Administrator's Guide
Apache ZooKeeper - Observers Guide
