Video in progress
What is unclean leader election, and what's the risk of turning it on?
Unclean election means making an out-of-sync follower the leader when no live replica is left in the ISR. The partition recovers faster, but the messages that follower hadn't written are lost for good, and consumer offsets can get confused.
📖 Answer
unclean.leader.election.enable is false by default (since Kafka 0.11). That means: if all replicas in the ISR are down, the partition goes offline and waits until one of them comes back.
If it's set to true, a lagging follower becomes the leader. Its log is shorter: say it ends at 940 instead of 1000. 60 messages are gone. When the old leader comes back, it truncates its log to match the new leader, so those messages now exist nowhere.
On top of that, consumers may have committed up to 1000 while the new log ends at 940. The next messages written get offsets 940, 941... and the consumer skips some of them or hits an offset out of range.
When is turning it on justified: metrics, click streams, logs, topics where losing some data is cheaper than losing availability. You can set this at the topic level, so leave the payment topics alone and enable it only where needed.
The meaning is the same in Kafka 3.x/4.x KRaft; work is going on in newer versions on improvements such as ELR (eligible leader replicas), but the core trade-off is the same.
Sign up to mark studied questions and save your progress.