liveBook · Manning

Preferred leader election

As explained in Apache Kafka in Action

Preferred leader election is the selection of a partition's designated preferred replica as its leader when that replica is available and synchronized.

Preferred leader election restores a partition’s designated leader after recovery. The preferred replica is the first broker in the partition’s replica list and must be synchronized before taking leadership. section 5.3

Recovering a partition leader

If Broker 3 leads Partition 1 and fails, Kafka promotes an in-sync replica such as Broker 1. After Broker 3 rejoins and catches up, leadership remains with Broker 1 until automatic rebalancing occurs or an operator triggers a preferred election.

$ kafka-topics.sh --create \
    --topic products.prices.changelog.replication-3 \
    --replication-factor 3 \
    --partitions 3 \
    --config min.insync.replicas=2 \
    --bootstrap-server localhost:9092
Created topic products.prices.changelog.replication-3.

Use kafka-leader-election.sh with --election-type=preferred to rebalance all topic partitions or target a specific topic. Avoid unclean elections except in extreme emergencies because out-of-sync replicas can lose data. section 5.3

How Apache Kafka in Action covers it

How deep it goes
Explains replica ordering, synchronization, automatic rebalancing, manual triggering, and the data-loss trade-off of unclean election.
Examples
Demonstrates Broker 3’s failure and recovery for Partition 1 using kafka-topics.sh and kafka-leader-election.sh.
What it assumes
Assumes familiarity with partition replication, replica ordering, leaders, followers, and in-sync replicas.
What you can do after
Operate Kafka leadership by triggering preferred elections and selecting targeted or cluster-wide scopes.
How it connects
Connects partition leaders with replicas, in-sync replicas, replication, leader-follower behavior, and unclean leader election.

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.