Kafka - why new topic partition leader is not elected?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed event store and stream-processing platform designed to handle data pipelines and provide high-throughput, low-latency messaging. Within Kafka, topics are the categories or feeds where records are published. These topics are split into partitions for better scalability and fault tolerance. Each partition has one server acting as a leader and the rest as replicas, ensuring high availability and durability.
Understanding Leaders and Replicas in Kafka
In Kafka, every partition of a topic has multiple replicas distributed across different brokers. One of these replicas is designated as the leader, whereas the others serve as followers. The leader handles all read and write requests for the partition, while the followers passively replicate the leader. If the leader fails, one of the followers will need to become the new leader, assuming it has all the committed messages replicated up to the point of the leader’s failure. This process is managed by the Kafka controller, which is responsible for leadership election.
Why Might a New Topic Partition Leader Not Be Elected?
There are several situations in which a new leader for a partition may not be elected effectively:
- Insufficient Replicas: If all replicas of a partition except the leader are down, there is no eligible replica to take over as leader. This scenario can occur due to network issues, hardware failures, or a massive crash affecting several brokers.
- Under-Replicated Partitions: Even if other replicas are technically "up," they might not have all the latest messages that were present in the leader before it failed. These replicas cannot be elected as leaders until they finish replicating the current state of the leader.
- ZooKeeper Latency or Failures: Kafka relies on ZooKeeper for managing cluster metadata, including the list of topics, partitions, and replicas. Any operational latency or failure in ZooKeeper can delay or prevent leader election.
- Configuration Issues: Misconfigurations in Kafka’s setup, such as incorrect broker and replication settings, can lead to issues where no leader election takes place.
- Unclean Leader Election Disabled: Kafka has an "unclean leader election" feature that allows a follower that is not fully caught up with the leader to become the leader in the absence of fully synced replicas. However, if this feature is disabled (which is recommended for avoiding data loss), then no new leader will be elected if no in-sync replicas are available.
Technical Considerations and Examples
Imagine a Kafka cluster with three brokers - Broker A, Broker B, and Broker C. A topic Topic1 with one partition (Partition 1) is replicated across all three brokers, with Broker A as the current leader. If Broker A and Broker C experience failures and Broker B does not have the latest set of writes, Partition 1 would not be able to elect a new leader if unclean leader election is disabled. This would effectively leave Partition 1 in a read-only state until either Broker A or Broker C is restored or until Broker B synchronizes fully.
Table Summary: Factors Affecting Leader Election
| Factor | Impact on Leader Election | Example Scenario |
| Insufficient Replicas | No available replicas for election | All replicas except the leader are down |
| Under-Replicated Partitions | Delay in election until replicas are fully synced | Replicas have not synced all committed messages |
| ZooKeeper Issues | Delays or prevents leader election | ZooKeeper downtime or high latency |
| Configuration Errors | Prevents leader election | Incorrect broker or replication settings |
| Unclean Leader Election Disabled | Prevents leader election without fully synced replicas | No fully in-sync replicas available |
Conclusion
The election of a new leader partition in Kafka is crucial for the continued availability and performance of Kafka-based systems. Understanding the factors that can prevent a leader from being elected helps in effectively managing and troubleshooting Kafka clusters. Proper planning, configuration, and monitoring are essential to ensure that leader election is successful, preserving data integrity and system reliability.
Related reading
- Kafka 0.10 Java Client TimeoutException Batch containing 1 record(s) expired
- kafka 0.11 reset offset for consumer group by --to-datetime
- Kafka 0.8, is it possible to create topic with partition and replication using java code?
- kafka 0.9.0.1 fails to start with fatal exception
- kafka ack=all and min-isr
- Kafka Acknowledgment vs Kafka commit
- Kafka 0.9 How to re-consume message when manually committing offset with a KafkaConsumer
- Kafka 10 - Python Client with Authentication and Authorization

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.