concept leader in category kafka

This is an excerpt from Manning's book Kafka in Action.
As you have seen already, our cluster for Kafka includes more than one broker (server). To act as one correct application, these brokers need to not only communicate with each other, they also need to reach agreement. Agreeing on who the leader of a partition is, is one example of the practical application of ZooKeeper within the Kafka ecosystem. For a real world comparison, most of us have seen examples of clocks alone getting out of sync and how it becomes impossible to tell the true time if all of them are showing different times. Agreement can be challenging across separate brokers and something is needed to keep Kafka coordinated and working as one in both success and failure scenarios.
One of the replicas of the partition will have the job of being the leader. The leader is in charge of handling the writes for that partition. Since this leader is the only one with the new written data, it also has the job of propagating writes to the followers. The In-Sync Replicas (ISR) list is maintained by the leader. Thus, it knows which replicas are caught up and see all the current messages. The replicas will act as consumers of the leader partition and will fetch the messages and have offset checkpointing to track their consumption.
Figure 6.3. Leader
![]()
Figure 6.3 shows a 3 node cluster and how leadership is defined for a specific example. Broker 3 is the leader for partition 2. Broker 2 and Broker 1 are followers of this leader. As the leader, Broker 3 will handle all of the reads and writes from external producers and consumers. The leader will also write the messages it receives to the other two brokers. The ISR list would include the leader as the first position (3) and the remaining followers that are staying up-to-date on their copies of the messages from the leader.
Since we are dealing with distributed computing and we know failures are expected, the leader also can notice a slower follower replica and remove it from the ISR list.
In some cases, the broker that fails could be the leader partition. The Controller and ZooKeeper could be used in this case to changes the leader for that partition. In Figure 6.4, our previous example has experienced a failure. Since this broker is not available, a new leader is elected. This new leader in this example was Broker 2. Once a follower, it was moved to a leader to keep Kafka serving and receiving data for that partition.
Figure 6.4. New Leader Elected
![]()
It is interesting to note that there is a Kafka Improvement Proposal KIP-392 (cwiki.apache.org/confluence/display/KAFKA/KIP-392%3A+Allow+consumers+to+fetch+from+closest+replica) which would allow consumers to fetch from the closest replica. Reading from the leader only is something that might take more time if your brokers span physical data centers and Kafka might be updated to help with these situations in the future.