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.
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:
Or:
Typical output includes a line like:
Or:
That is the direct answer.
Check Every Node In The Ensemble
Because only one node will report leader, loop over all known hosts:
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:
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 2181orecho stat | nc host 2181to inspect a node's role. - Look for the
Modeline to identifyleader,follower, orobserver. - Query every node in the ensemble because only one should report
leader. - '
zkServer.sh statusis 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
- How to choose Kafka transactional.id in a Kubernetes (Producer side only transaction) set up
- How to choose the no of partitions for a kafka topic?
- How to close kafka consumer once all messages are consumed?
- how to compress data in producers when using spring kafka
- How to classify a failure detector?
- How to clear CrashLoopBackOff
- How to configure Celery to run as systemd service with a Django application served by Gunicorn?
- How to configure Confluent Platform Kafka connect logs?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.