kafka binding to ipv6 port even though ipv4 address specified in config
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 capable of handling trillions of events a day. Initially conceived by LinkedIn and subsequently open-sourced, Kafka is widely used for building real-time streaming data pipelines and applications. Kafka also provides robust support for both the TCP/IP stack versions - IPv4 and IPv6.
Binding Kafka to an IPv6 Port with IPv4 Configuration
Even if you specify an IPv4 address in Kafka’s server configuration, you might notice Kafka binding to an IPv6 port. This situation can occur due to default settings and the underlying Java networking behavior, which prefers IPv6 when available. Understanding this behavior necessitates a closer look at Kafka’s configuration and the environment it operates in.
Underlying Java Networking
Kafka is written in Java, which by default prefers to use IPv6 addresses if the system supports it. This preference can lead to situations where services like Kafka listen on IPv6, even though the configuration explicitly specifies an IPv4 address. This is governed by the Java system property java.net.preferIPv4Stack. When this property is set to true, Java will prefer using an IPv4 stack, otherwise, it defaults to using both stacks with a preference for IPv6 if available.
Configuration in Kafka
In your server.properties file used for Kafka’s configuration, you specify the listeners’ property to control which IP addresses and ports Kafka binds to. Consider this setting:
Here are some possible scenarios:
- Default Operation: Kafka will parse this and attempt to bind to the IPv4 address
192.168.99.100on port9092. However, depending on the JVM settings and the operating system’s configuration, it may end up listening on[::]:9092— the IPv6 equivalent of0.0.0.0:9092(all available addresses). - IPv6 Enabled: If IPv6 is enabled and not specifically restrained, Kafka might bind to the IPv6 address derived from the given IPv4, often seen as a dual stack bind where it listens on both IPv4 and IPv6 interfaces.
Forcing Kafka to Use IPv4
To ensure Kafka only binds to IPv4, you need to set the following in your kafka-server-start.sh or ensure it’s in your Kafka startup script:
Alternatively, you can set this property in server.properties:
Kafka Binding Behavior Summary
The table below summarizes the key points regarding Kafka's IP binding behavior based on the configuration and system properties:
| Configuration Setting | Java System Property | Resulting Binding |
|------------------------------------|--------------------------------------|-----------------------------|
| listeners=PLAINTEXT://192.168.99.100:9092 | java.net.preferIPv6Addresses=true | Binds to IPv6 and IPv4 |
| listeners=PLAINTEXT://192.168.99.100:9092 | java.net.preferIPv4Stack=true | Binds only to IPv4 |
| listeners=PLAINTEXT://[::1]:9092 | N/A | Binds only to IPv6 |
Conclusion
Understanding Kafka’s network binding behavior is crucial for deploying in environments with complex networking requirements, including those that support both IPv4 and IPv6. Configuring Kafka to correctly bind to the desired IP stack ensures that Kafka services are accessible and perform as expected in specific network setups. Ensure that your JVM settings align with your networking configuration goals, particularly in mixed IPv4 and IPv6 environments.

