When does Kafka Leader Election happen?

Marut Singh picture Marut Singh · Mar 31, 2017 · Viewed 8.9k times · Source

When and how often does Kafka High Level Producer elect a leader? Does it do before sending each message or only once at the time of creating connection?

Answer

Vijet Badigannavar picture Vijet Badigannavar · Apr 16, 2017

Every broker has a information about the list of topics(and partitions) and their leaders which will be kept up to date by the zoo keeper whenever the new leader is elected or when the number of partition changes.

Thus, when the producer makes a call to one of the brokers, it responds with this information list. Once the producer receives this information, it caches this and uses it to connect to the leader. So next time when it wants to send the message to that particular topic (and partition) it will use this cached information.

Lets assume there was only one leader and there are no replicas for that topic/ partition duo and it got crushed. In this case it will try to connect to that leader and it fails. It will try to fetch the leader from the other brokers list which it has cached to check if there is any leader for this topic! As it does not find any, it will try to hit to the same leader(that is dead) and after reaching a maximum no of retries it will throw an exception !!