kafka Brokers Leader Skewed
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 streaming platform that is extensively used for building real-time streaming data pipelines and applications. Kafka operates on a cluster of one or more servers (known as brokers), and managing these clusters effectively is crucial for ensuring high availability and performance. One of the common issues encountered in managing Kafka clusters is the problem of leader skew.
Understanding Kafka Brokers and Leader Election
In a Kafka cluster, each broker can handle reads and writes for several partitions of different topics. Kafka partitions are replicated across multiple brokers for fault tolerance. Among these replicas, one is designated as the leader and the others as followers. The leader handles all read and write requests for the partition, while the followers passively replicate the leader. If a leader fails, one of the followers will automatically take over as the new leader.
What is Leader Skew?
Leader Skew in Kafka refers to the uneven distribution of leader partitions across the brokers in a cluster. Ideally, each broker in a Kafka cluster should have approximately an equal number of leader partitions to balance the load equitably. However, due to various reasons such as cluster expansions, failures, and uneven traffic patterns, some brokers may end up with a higher number of leader partitions than others.
Causes of Leader Skew
Here are some common causes of leader skew in Kafka:
- Imbalanced Topic Configuration: If topics are not uniformly configured with partitions, some brokers end up with more leader partitions.
- Broker Failures: When a broker fails, its partitions are reassigned, potentially causing an imbalance when the broker rejoins.
- Manual Reassignments: Improperly executed manual partition reassignment can lead to leader skew.
- Cluster Scaling: Adding new brokers to a Kafka cluster without proper rebalancing can result in leader skew.
Impacts of Leader Skew
The uneven distribution of leader partitions can lead to several issues:
- Performance Degradation: Brokers with more leaders will have higher disk I/O, network I/O, and CPU usage, leading to slower performance.
- Increased Latency: Overloaded brokers may lead to increased latency for producers and consumers.
- Hotspots in The Cluster: Creates hotspots which can lead to failures and bottlenecks.
Example Scenario
Suppose a Kafka cluster with 3 brokers and a topic with 9 partitions, replicated with a factor of 3. Ideally, each broker should be the leader for 3 partitions. Due to an imbalance, imagine broker 1 is the leader for 5 partitions, broker 2 for 2 partitions, and broker 3 for 2 partitions. This scenario indicates a leader skew.
Resolving Leader Skew
To address leader skew:
- Use Topic Partition Reassignment Tool: Reassign partition leaders across the brokers more equitably.
- Monitor Regularly: Use Kafka's built-in tools like
kafka-topics.shto monitor distribution of leader partitions. - Automate Rebalancing: Implement automation scripts or use Kafka operator frameworks that periodically check and correct skew.
Conclusion
Maintaining a balanced distribution of leader partitions is crucial for Kafka's performance and reliability. Regular monitoring and mindful operational practices can prevent or correct leader skew, thus ensuring efficient streaming data operations.
Table: Summary of Key Points on Kafka Broker Leader Skew
| Aspect | Description |
| What is it? | Imbalance in the distribution of leader partitions across Kafka brokers |
| Causes | Imbalanced configurations, broker failures, manual reassignments, cluster scaling |
| Impacts | Performance degradation, increased latency, cluster hotspots |
| Resolution Methods | Topic partition reassignment, regular monitoring, automation |
Understanding and managing leader skew is crucial for maintaining the operational efficiency of Kafka clusters. Through careful planning and regular maintenance, it is possible to minimize or avoid the negative impacts associated with leader skew.
Related reading
- Kafka Buffer Size And Time Interval
- Kafka cached zkVersion not equal to that in zookeeper broker not recovering
- Kafka can not delete old log segments on Windows
- kafka cant connect to zookeeper- FATAL Fatal error during KafkaServerStable startup
- Kafka Client Connection Pooling
- kafka cluster configuration
- Kafka Cant Create Multiple Stream Consumers
- Kafka Capacity Planning

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.