Backend Development 10 min read

Understanding Kafka Partition Failover When a Broker Goes Offline

This article analyzes a real‑world Kafka outage caused by killing a broker process, explains why partitions with a replication factor of one lose their leader, and walks through the internal Zookeeper‑based failover mechanism and leader‑election logic that Kafka uses to recover from such failures.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding Kafka Partition Failover When a Broker Goes Offline

The author, a senior architect and author of "RocketMQ Technical Insights," describes a production incident where expanding a Kafka log cluster introduced high latency, and after killing the process of a broker with kill pid , both Java and Go clients reported errors indicating that some partitions had no online leader.

Investigation of the affected partition (dw_test_kafka_0816000, partition 101) using Zookeeper revealed a leader state of -1 and an ISR list containing only a single replica on the now‑offline broker, confirming that the partition’s replication factor was set to 1. Because no other broker held a copy, automatic leader election could not occur.

The root cause was the intentional configuration of a single‑replica topic to reduce inter‑broker traffic under heavy load, which made the system vulnerable to broker outages. The recommended mitigation is to relocate the topic’s partitions away from brokers scheduled for maintenance before shutting them down.

Kafka stores live broker information in Zookeeper under /{namespace}/brokers/brokers/ids as ephemeral nodes. When a broker disappears, its node is removed, triggering the Controller’s onBrokerFailure callback, which subsequently invokes KafkaController.onReplicasBecomeOffline to handle partitions without a leader.

The onReplicasBecomeOffline method groups partitions needing removal, selects those without a leader ( partitionsWithoutLeader ), marks them as OfflinePartition , and drives the state machine to transition them toward OnlinePartition via doHandleStateChanges and sendRequestsToBrokers .

Transitioning from OfflinePartition to OnlinePartition involves assembling the relevant Seq[TopicPartition] , defining the target state, and applying the OfflinePartitionLeaderElectionStrategy . The electLeaderForPartitions method returns sets of successful and failed elections, retrying on recoverable exceptions.

Leader election proceeds by fetching partition metadata from Zookeeper, building a map of TopicPartition → LeaderIsrAndControllerEpoch , comparing controller epochs, and applying the configured election strategy. If unclean.leader.election.enable is false, the first live ISR replica becomes the leader; otherwise, any live replica may be chosen.

Finally, the election results are written back to Zookeeper and propagated to all brokers, completing the failover. The article concludes that moving partitions before broker shutdown prevents write failures and encourages readers to explore the source code for deeper understanding of Kafka’s partition election mechanisms.

backendZooKeeperKafkaMessagingFailoverLeader ElectionPartition
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.