Zookeeper
Overview
Zookeeper is a highly-available, high-performance coordination service:
- Key features: scalable, distributed, configuration, consensus, group membership, leader election, naming
It basically keeps track of information that must be synchronized across your cluster
- Which node is the master?
- What tasks are assigned to which workers?
- Which workers are currently available?
It’s a tool that applications can use to recover from partial failures in your cluster
Zookeeper Guarantees
- Clients will never detect old data (incorrect / inconsistant data — it may not be the latest one but it must be updated some times in the future)
- Clients will get notified of a change to data they are watching within a bounded period of time
- All requests from a client will be processed in order
- All results received by a client will be consistent with results received by all other clients
Primitive Operations
- ZooKeeper uses a protocol called Zab that runs in two phases
- Leader election
- Select a member called the leader (master), and holds a “lock” on that data
- Other machines be followers (cannot become leader until that lock is released)
This phase is finished once a majority (or quorum) of followers have synchronized their state with the leader
- Atomic broadcast
- All write requests are forwarded to the leader, which broadcasts the update to the followers. The leader commits the update if a majority agree (either succeeds or fails)
- Leader election

- All servers store a copy of the data (in memory), at least 50% above servers survices then it works
- A leader is elected at startup
- Followers service clients, all updates go through leader
- Update responses are sent when a majority of servers have persisted the change
no matter the client connects to which servers, it guarantees that the client can get the same result.
Data Model
A client can create a znode, read and write data to znode inside Zookeeper
- Znodes are organized in a directory-like structure
- Call the intermediate nodes groups
- Call the leaf nodes group members

Znodes are not designed for general data storage. Instead, znodes map to abstractions of the client application (e.g., metadata)
Zookeeper doesn’t use handles to access znodes
- Use absolute paths (e.g.,
/zoo/cow) directly - Saves overhead of calling
open()orclose()
- Use absolute paths (e.g.,
Data access is atomic
A client reading the data stored at a znode will never receive only some of the data; either the data will be delivered in its entirety, or the read will fail
Types of znodes
Persistent (or regular) znodes
- They remain stored until explicitly deleted
- Usually represent groups, or intermediate nodes (Client — Zookeeper)
i.e., assignment of tasks to workers must persist even if master crashes
Ephemeral znodes
- They go away if the client that created it crashes or loses its connection to ZooKeeper
- Either delete them explicitly, or let the system remove them automatically when the session that creates them terminates
- Sessions have an associated timeout. Zookeeper considers a client faulty if it does not receive anything from its session for more than that timeouti.e., if the master crashes, it should release its lock on the znode that indicates which node is the master!
- Usually represent the existence of an client, i.e., leaf node
Notifications/Watches
A client can register for notifications on a znode, clients will receive notifications when a znode changes
Avoids continuous polling
e.g., A client may wait on the creation of a znode. If the znode is created, the watch is triggered
Watchers are triggered only once
- Clients need to reregister the watch to receive multiple notifications
Znode creation
PERSISTENT & EPHEMERAL- The naming of such znodes has to be specific and unique, if a client with a duplicated pathname is joining a group, it will be rejected
PERSISTENT_SEQUENTIAL & EPHEMERAL_SEQUENTIAL- The naming of such znodes contains a client-defined prefix and the monotonic increasing, numerical postfix: generated by the Zookeeper service.
- E.g., Creating a sequential znode using the name
/zoo/fubar, the resulting name will be:/zoo/fubar001 - Another client using the same name
/zoo/fubarto create a sequential znode will not be rejected, and the resulting name will be/zoo/fubar002
Programming
- Zookeeper object
- The main object controlling every aspect of Zookeeper
- Instantiate this object when connecting to Zookeeper.
- Using the object methods, you can: create or delete znodes, get and set znode data, query a znode’s child list
- Watcher interface
- It is the Zookeeper-specific event. It calls back when:
- the concerned node is created/deleted
- the concerned node has its list of children changed
- It is the Zookeeper-specific event. It calls back when:
Client APIs
create(path, data, flags):- Creates a znode with pathname; flag specifies the type of znode
delete(path, version):- Deletes the znode path if that znode is at the expected version
exists(path, watch):- Returns true if the znode with pathname exists
getData(path, watch)/setData(path, data, version):- Reads/writes data in the znode
getChildren(path, watch):- Returns the set of names of the children of a znode
sync():- Waits for all updates pending at the start of the operation to propagate to the server that the client is connected to
Watcher Interface
Notification Mechanism

Watch creation (install watches)
zk.exists()zk.getChildren()zk.getData()

- All functions are non-blocking calls, meaning that they return the answers immediately
- A separate thread will listen to the watcher and be triggered to execute
process()
Watch Triggers

Distributed Lock Service
Lock and critical section:
- When I’m the first one who takes the lock, others should wait until I leave.
- Others will then race in order to get the lock.
Mapping to Zookeeper:
- When a client is the first one who creates an EPHEMERAL znode, that means “client takes the lock”
- Other clients would fail to create such a znode. They attach a Watch to the created znode
- When the first client dies, all waiting clients will be triggered (EventType.NodeDeleted)
- They can race for the lock by creating the same EPHEMERAL znode

Herd effect
what if there are hundreds of clients waiting for the lock?
- Many notifications are sent
- Many clients awake and rush for the lock together
- Only one of them gets the lock
- The situation happens again and again…
Using a set of EPHEMERAL_SEQUENTIAL clients!
- give up the property that a released lock will be randomly held by another client
- New property: the client with the (numerically) smallest pathname will hold the lock
- Using
exists(), a client will only watch the deletion of the client that is just smaller than its pathname
lock without herd effect




