Kafka Get broker host from ZooKeeper
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform that's predominantly used for building real-time streaming data pipelines and applications. Kafka's scalability and fault tolerance are largely due to its deep integration with Apache ZooKeeper, which manages the state of Kafka brokers in a cluster. Understanding how to retrieve broker details from ZooKeeper can be crucial for setup and monitoring of Kafka clusters.
Understanding the Relationship Between Kafka and ZooKeeper
Before diving into specifics, it's important to recognize that Kafka uses ZooKeeper for multiple purposes:
- Broker Registration: Each broker in a Kafka cluster registers itself in ZooKeeper when it starts. The broker registration includes metadata such as the broker ID, host, and port.
- Cluster Management: ZooKeeper keeps track of all the brokers that are functioning at any given time and helps in leader election for partitions.
- Configuration Management: Configuration details of topics, including partition count, replication factor, and other relevant configuration settings, are stored in ZooKeeper.
How to Access Broker Information from ZooKeeper
To get broker information from ZooKeeper, you typically interact with the ZooKeeper ensemble that Kafka uses. Here’s how you can do it:
- Connect to ZooKeeper: Use a ZooKeeper client, like the command-line client shipped with Kafka, to connect to ZooKeeper. The connection string is typically in the format
hostname:port[,hostname:port]*. - Navigate to the Broker Node: Broker details are stored under the
/brokers/idspath in ZooKeeper. Each child node under this path represents a broker, and the node name is the broker’s unique ID. - Retrieve Broker Details: The data stored in each broker’s node is in JSON format, which includes the broker host, port, and other metadata.
Example: Fetching Broker Information
Here is a step-by-step example using the ZooKeeper command-line interface:
- Connect to ZooKeeper Shell:
- List Broker IDs:
- Get Broker Details:
Replace [BROKER_ID] with the actual broker ID.
Python Example Using Kazoo
If you prefer working with Python, the Kazoo library facilitates interaction with ZooKeeper. Here’s how you can use it:
Important Considerations
- Version Compatibility: Ensure that the method and path in ZooKeeper for broker details are compatible with your Apache Kafka version.
- Security: Interaction with ZooKeeper should be secured, especially for production environments. Consider enabling ACLs and SSL/TLS for connections.
Summary Table
| Attribute | Detail |
| Usage | Retrieves broker details for monitoring and management |
| Required Tools | ZooKeeper CLI, Kazoo (Python), or other ZK clients |
| Key ZooKeeper Path | /brokers/ids |
| Data Format | JSON |
| Security Considerations | Secure the ZK connection, manage ACLs |
This detailed guide should help in understanding and implementing the retrieval of Kafka broker details from ZooKeeper, streamlining cluster management and monitoring efforts.
Related reading
- Kafka get to know when related messages are consumed
- Kafka Getting error No resolvable bootstrap urls given in bootstrap servers
- Kafka GlobalKTable Latency Issue
- Kafka having duplicate messages
- Kafka how to consume one topic parallel
- Kafka how to set producer retries to Infinity
- Kafka High-level Consumer error_code=15
- Kafka High Level Consumer Fetch All Messages From Topic Using Java API (Equivalent to --from-beginning)

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.