Does kafka broker always check if its the leader while responding to read/write request

1k views Asked by At

I am seeing org.apache.kafka.common.errors.NotLeaderForPartitionException on my producer which I understand happens when producer tries to produce messages to a broker which is not a leader for the partition. Does that mean each time a leader fulfills a write request it first checks if its the leader or not? If yes does that translates to a zookeeper request for every write request to know if the node is the leader?

1

There are 1 answers

7
spike 王建 On

How Producer Get MetaData About Brokers
The producer sends a meta request with a list of topics to one of the brokers you supplied when configuring the producer.

The response from the broker contains a list of partitions in those topics and the leader for each partition. The producer caches this information and therefore, it knows where to redirect the messages.

When Producer Will Refresh MetaData
I think this depends what kafka client you used.There are some small differents between ruby, java or other kafka client.for example, in java:

  1. producer will fetch metadata when client initialize,then period update it depends on expiration time.
  2. producer also will force update metadata when request error occured,such as InvalidMetadataException.

But in ruby-kafka client, it usually refresh metadata when error occured or initialize.