Monedula

Scenario 03.2.3 · Unclean leader

Kafka unclean.leader.election.enable: offline or data loss

Replication · Advanced · about 3 min · Apache Kafka 4.3

A stale survivor is promoted with data loss when unclean.leader.election.enable is flipped on.

Run this scenario in the playground

broker-2 killed - partition OFFLINE. t+21.000s

ISR is empty. With unclean election disabled, Kafka refuses to promote the stale broker-3: durable, but unavailable.

Open at this step: step 5, broker-2 killed - partition OFFLINE

What you start with

With unclean.leader.election.enable=false, a partition whose entire ISR is dead stays offline rather than promote a stale replica. Here both in-sync brokers are killed after a follower has been throttled out of date, then the unclean flag is flipped on.

Step by step

  1. Cluster ready - RF 3, min.ISR 2 t+0.000s orders has 3 in-sync replicas. unclean.leader.election.enable is OFF: Kafka prefers durability over availability. Open at this step: step 1, Cluster ready - RF 3, min.ISR 2
  2. 2 records committed on all three t+4.100s High-watermark is 2 - broker-3 holds both records. Open at this step: step 2, 2 records committed on all three
  3. broker-3 throttled out of ISR - stale survivor t+12.100s broker-3 is stuck at LEO 2 while the leader committed up to 5; it has fallen out of ISR but is still alive. Open at this step: step 3, broker-3 throttled out of ISR - stale survivor
  4. broker-1 killed - broker-2 leads (clean election) t+16.600s broker-2 is the only in-sync replica left, so it is elected cleanly with no data loss. Open at this step: step 4, broker-1 killed - broker-2 leads (clean election)
  5. broker-2 killed - partition OFFLINE t+21.000s ISR is empty. With unclean election disabled, Kafka refuses to promote the stale broker-3: durable, but unavailable. Open at this step: step 5, broker-2 killed - partition OFFLINE
  6. Unclean election enabled - broker-3 leads, records lost t+25.000s broker-3 (LEO 2) is promoted unclean. The high-watermark drops from 5 to 2 - 3 committed records are gone. Availability restored at the cost of durability. (The simulator elects at once; in KRaft a dynamically enabled flag waits for the periodic check, 5 min by default, unless you run kafka-leader-election.sh with the unclean option.) Open at this step: step 6, Unclean election enabled - broker-3 leads, records lost

The takeaway

Flipping unclean.leader.election.enable on promotes the stale survivor and brings the partition back online, but at the cost of losing every record it never received - availability bought with data loss. (In real KRaft the election is not instant: a dynamically enabled flag waits for the controller's periodic check, 5 min by default, unless kafka-leader-election.sh runs an unclean election.)

Configs in this scenario

Embed this scenario

Paste this into a blog post, a course page or your team's docs. The scenario plays inside your page, and the link under it points readers to this walkthrough.

<iframe src="https://monedula.dev/kafka-simulator/embed/?scenario=03.2.3" title="Kafka Simulator: Unclean leader" width="100%" height="520" style="border:1px solid #d0d0d0;border-radius:8px" loading="lazy"></iframe>
<p><a href="https://monedula.dev/kafka-simulator/scenarios/unclean-leader/">Kafka unclean.leader.election.enable: offline or data loss</a></p>

Further reading

Apache Kafka documentation

Troubleshooting

More in Replication

All scenarios