Kafka Zookeeper - Java.net.BindException Address already in use
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a popular distributed streaming platform, often works in conjunction with ZooKeeper, which it uses for managing and coordinating Kafka brokers. ZooKeeper is crucial for Kafka as it helps in leader election of Kafka brokers and in keeping track of the status of nodes in a Kafka cluster. However, one common issue encountered during the setup or scaling of such environments is the Java.net.BindException: Address already in use. This error indicates that the port which Kafka or ZooKeeper is trying to bind to is already occupied by another service. This article aims to demystify the reasons behind this error and provide solutions on how to resolve it.
Understanding Java.net.BindException: Address already in use
The exception Java.net.BindException: Address already in use typically occurs when a new process attempts to bind to a port (TCP or UDP) on which a different process is already listening. This could happen due to:
- Configuration mistakes: Mistakingly configuring multiple instances of Kafka or ZooKeeper to use the same port.
- Unclean shutdowns: An instance was not shut down properly, and the operating system hasn’t fully released the port.
- Concurrent use: Two services are configured to use the same port, perhaps due to a wrong setup.
Practical Example
For instance, suppose you have configured your ZooKeeper server to listen on port 2181 (the default port for ZooKeeper). If another instance of ZooKeeper (or another service altogether, like another database) is already using this port, you will encounter this exception during startup:
How to Solve Address already in use
Here are some strategies to resolve the port conflict:
- Check active services: Use commands like
netstat -plnt | grep :<port-number>on Linux orGet-Process -Id (Get-NetTCPConnection -LocalPort <port-number>).OwningProcesson Windows to identify the process using the port. - Change port settings: If possible, change the port number in the ZooKeeper or Kafka configuration file. For ZooKeeper, edit the
clientPortinzoo.cfg:
- Ensure proper shutdown: Make sure that all Kafka and ZooKeeper processes have been properly shut down before restarting them. You can use commands like
kill -SIGTERM <pid>for graceful shutdown. - System reboot: As a last resort or if a port seems stuck, rebooting the system can help release ports that seemed occupied.
Summary Table
| Issue | Solution | Example Command (Linux) | |
| Port already in use | Change configured port | listener=PLAINTEXT://:9093 for Kafka | |
| Unclear port ownership | Identify and potentially kill the process using the port | netstat -plnt | grep :2181 | |
| Reusing ports from previous services | Ensure services do not automatically start or restart | systemctl disable kafka |
Additional Considerations
Firewall settings: Ensure that firewall settings are not blocking or rerouting requests from the expected ports. Sometimes, what appears as a port conflict might actually be traffic being improperly managed by firewall rules.
Cluster management tools: When deploying Kafka in containerized environments or using orchestration tools like Kubernetes, make sure that port mappings are correctly configured in the deployment descriptors.
Conclusion
The Java.net.BindException: Address already in use is a frequent issue faced by many developers and system administrators while setting up servers for distributed systems like Kafka linked with ZooKeeper. Understanding the underlying reasons and knowing how to investigate and solve these conflicts is crucial for maintaining a healthy and responsive system infrastructure. By following structured troubleshooting steps, one can often quickly resolve port conflicts and ensure smooth operations of their Kafka clusters.
Related reading
- Kafka Zookeeper connection issues
- Kafka zookeeper keep on showing info Message 'Accepted socket connection from /10.xxx.xxx.xxx
- KafkaAvroDeserializer does not return SpecificRecord but returns GenericRecord
- KafkaAvroSerializer for serializing Avro without schema.registry.url
- KafkaConsumer Java API subscribe() vs assign()
- kafka.consumer.SimpleConsumer Reconnect due to socket error java.nio.channels.ClosedChannelException
- KafkaException jdk.internal.loader.ClassLoaders can’t find org.apache.kafka.common.security.plain.PlainLoginModule
- KafkaListener in Unit test case does not consume from the container factory

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.