Zookeeper
Zookeeper Instance
Zookeeper Leader
Tech Troubleshooting
Ensemble Management

How to check which zookeeper instance is the leader within an ensemble

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In a ZooKeeper ensemble, one server is the leader and the others are followers or observers. The usual way to identify the leader is to query each instance with a four-letter administrative command such as srvr or stat and inspect the reported Mode.

Use srvr Or stat

If the ZooKeeper process listens on the normal client port, usually 2181, you can ask it for server information:

bash
echo srvr | nc zk1.example.com 2181

Or:

bash
echo stat | nc zk1.example.com 2181

Typical output includes a line like:

text
Mode: leader

Or:

text
Mode: follower

That is the direct answer.

Check Every Node In The Ensemble

Because only one node will report leader, loop over all known hosts:

bash
1#!/usr/bin/env bash
2
3hosts=(zk1.example.com zk2.example.com zk3.example.com)
4port=2181
5
6for host in "${hosts[@]}"; do
7  echo "Checking ${host}"
8  echo srvr | nc "${host}" "${port}" | grep Mode
9done

This is the quickest operational script when you already know the ensemble members.

If the cluster is healthy, you should see one leader and the rest as followers or observers.

Another Option: zkServer.sh status

If you have shell access on the ZooKeeper machines and the distribution scripts are installed, this command often works too:

bash
zkServer.sh status

It can report values such as:

  • 'leader'
  • 'follower'
  • 'standalone'

This is convenient for local administration, but the four-letter commands are often easier for remote checks and automation.

Understand What The Role Means

The leader coordinates writes and replication. Followers participate in quorum and can serve reads. Observers receive updates but do not vote in quorum decisions.

That means identifying the leader is useful when:

  • debugging write-path issues
  • confirming failover after a restart
  • checking election behavior
  • diagnosing ensemble instability

If the leader keeps changing unexpectedly, the problem is usually larger than "which node is leader right now."

Watch Out For Command Restrictions

Some deployments restrict four-letter-word commands for security reasons. If srvr or stat fails, check the ZooKeeper configuration for command allow-list settings.

That is an easy detail to miss when a command works in development but not in a locked-down production environment.

Also remember that a disconnected or partitioned node may not respond cleanly even though other ensemble members are healthy.

That is why leader checks are more useful when combined with basic health checks and recent operational context such as restarts, network incidents, or election churn.

Otherwise, a single snapshot can be misleading.

Repeating the check across all nodes after a few seconds is often enough to tell a stable leader from a node that is still in the middle of an election or recovery cycle.

Common Pitfalls

One common mistake is checking only one node and assuming the result represents the whole ensemble state.

Another issue is forgetting that observers are not leaders or voting followers, which can confuse operational scripts.

A third problem is relying on the commands without realizing that some deployments disable or restrict those administrative checks.

Finally, if the cluster is unhealthy or in election, you may see transient states that require checking several nodes more than once before drawing conclusions.

Summary

  • Use echo srvr | nc host 2181 or echo stat | nc host 2181 to inspect a node's role.
  • Look for the Mode line to identify leader, follower, or observer.
  • Query every node in the ensemble because only one should report leader.
  • 'zkServer.sh status is useful when you have shell access to the server.'
  • If the role is unstable, the real issue is likely ensemble health rather than the inspection command itself.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.