pythonapache-kafkakafka-producer-apikafka-pythonbroker

Why do I get Intermittent "NoBrokersAvailable" errors, with python application (kafka-python Producer)?


I’m trying to make a connection to Kafka on AWS. Communication is done over TLS.<br> Kafka-python==2.0.2 is used, and the KafkaProducer configuration is as follows:

From Kafka import KafkaProducer 

producer = KafkaProducer(
    bootstrap_servers=boostrap_servers, # I have three brokers in my Kafka cluster.
    security_protocol='SSL',
    ssl_check_hostname=False,
    ssl_cafile=ssl_cafile,
    value_serializer=lambda x: json.dumps(x, ensure_ascii=False).encode("utf-8"),
)

I run into NoBrokersAvailble Error intermittently without a pattern. The connection continues to fail for a moment once NoBrokersAvailable occurs.

I already checked if there's any problem connecting with the Kafka cluster on AWS:

So these are my questions:

  1. What is the default number of retries when the connection is found to fail, in Kafka-python?
  2. I found out that NoBrokersAvailable Error is raised while checking the version of a Kafka broker; I won't get NoBrokersAvailable Error if I specify my API version. But why is this error raised from time to time, not always? Why does my python application checking version sometimes get proper return value?
  3. It would be helpful to know some other parameters to set, server settings to check, or any other expected error points.

Solution

  • In my case, I specified the kafka version while creating the Producer instance and I don't see the NoBrokersAvailable Errors anymore.

    producer = KafkaProducer(bootstrap_servers='localhost:9092',
                                 value_serializer=lambda v: json.dumps(v).encode('utf-8'),
                                 max_request_size=20971520,api_version = (2, 8, 0))
    

    You can get the kafka version by running kafka-topic.sh:

    kafka/bin/kafka-topics.sh --version
    

    Output:

    2.8.0 (Commit:ebb1d6e21cc92130)