Kafka not able to connect with zookeeper with error Timed out waiting for connection while in state CONNECTING
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed streaming platform known for its high throughput and fault tolerance capabilities. It functions effectively for handling real-time data feeds. A key component in the Kafka architecture is ZooKeeper, which is used for managing and coordinating Kafka brokers. ZooKeeper acts as a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
Common Issue: Connection Timeout
One frequent issue encountered when setting up or running Kafka is the inability of Kafka brokers to connect to the ZooKeeper ensemble, typically logging an error: "Timed out waiting for connection while in state: CONNECTING". This error indicates that the Kafka broker started a connection process to ZooKeeper but could not complete the connection within the expected time frame.
Causes and Solutions
1. Network Issues
Cause: The most common cause is network-related issues where packets are delayed, lost, or blocked between Kafka and ZooKeeper.
Solution: Verify network connectivity using tools like ping or telnet. Ensure that no firewall rules are blocking the connections and that both Kafka and ZooKeeper nodes can reach each other.
2. ZooKeeper Server Overload
Cause: ZooKeeper handling too many requests or having too many clients can lead to performance degradation and timeouts.
Solution: Monitor the load on ZooKeeper servers. Increase the number of ZooKeeper nodes or tune ZooKeeper performance parameters such as tickTime and initLimit.
3. Incorrect Configuration
Cause: Misconfiguration in Kafka’s zookeeper.connect string in server.properties file. This might be due to wrong port numbers, hostnames or an incorrect list of ZooKeeper servers.
Solution: Double-check the zookeeper.connect line in the Kafka configuration and make sure it correctly lists all ZooKeeper servers with the correct hostnames and port numbers.
4. Version Mismatches
Cause: Incompatibilities between the versions of Kafka and ZooKeeper can lead to unexpected behaviour including connection timeouts.
Solution: Ensure compatible versions of Kafka and ZooKeeper are used. Upgrade if necessary, following the compatibility guidelines provided in Kafka documentation.
5. Resource Limits
Cause: System resource limits like file descriptors or network buffers being exhausted.
Solution: Check and adjust the operating system's limits for resources. Look at the ulimit settings for file descriptors and increase them if they are too low.
Technical Insight: Connection Process
When Kafka initiates a connection to ZooKeeper, it is managed by the ZooKeeper client library embedded within Kafka. This client attempts to establish a socket connection with each ZooKeeper server in the ensemble sequentially until a connection is established or until all servers have been tried and failed. The CONNECTING state is an intermediate state, and if it persists, suggests that none of the attempted connections were successful within the given timeframe.
Troubleshooting Tools and Methods
- Logging: Check the detailed logs generated by Kafka and ZooKeeper to understand the progression of events leading to the error.
- Network Monitoring Tools: Tools like Wireshark or tcpdump can help capture network packets and diagnose issues such as intermittent network failures.
- ZooKeeper CLI: Use ZooKeeper command line interface to manually connect to the ZooKeeper nodes to verify their availability and responsiveness.
Summary Table
| Issue | Causes | Solutions |
| Connection Timeouts | Network issues | Check connectivity, fix network issues |
| ZooKeeper server overload | Increase node count or performance tuning | |
| Configuration errors | Correct Kafka zookeeper.connect settings | |
| Version mismatches | Ensure compatible versions | |
| Resource limits | Adjust OS resource limitations |
Conclusion
Connection issues between Kafka and ZooKeeper can stem from a variety of factors, from simple misconfigurations to more intricate network or resource availability issues. Identifying and resolving these issues requires a systematic troubleshooting approach involving configuration verification, network testing, and system monitoring. Ensuring that both Kafka and ZooKeeper are correctly set up and maintained will lead to a more robust and reliable data streaming platform.

