TL;DR
- When the last broker in a partition's in-sync replica set (ISR) is lost, Apache Kafka® keeps the partition offline by default, preferring consistency over availability.
- Unclean leader election flips that choice: any broker can become the leader, so the partition comes back sooner, with possible data loss.
- In a real disaster, preferring availability usually wins, so decide per topic and write a disaster recovery plan before you need it.
- On Kafka 4.0 or newer, Eligible Leader Replicas (ELR) can prevent a partition from going offline in certain situations, but not always. You should enable ELR.
Apache Kafka® maintains a set of in-sync replicas (ISR) for each topic partition. During normal operations there is at least one broker in the set, the leader. Typically, in order to tolerate losing a single broker, there are more than one, and one of those is the leader. In such a setup, when the leader is lost, another broker becomes the leader. However, if the last broker in the ISR is lost, the partition goes offline. Once a partition is offline, producers and consumers fail to use it.
Kafka with out-of-the-box configuration makes a decision to prefer consistency over availability. It keeps the partition offline until any broker in the ISR returns, ensuring consistency. However, it could take minutes, hours or even days for the partition to become online again. Or it might never become online. The other option is to let any broker become the leader preferring availability with a possible data loss. In Kafka, this is called unclean leader election. The Kafka operator must choose exactly one of these configurations. This can be configured per topic.
This is a classic trolley problem: do nothing, lose availability, do something, lose consistency 😓
However, this is just theory and in real life things are more nuanced.
First, theoretically no data is lost in the default case. A producer buffers messages locally and sends the buffered messages to Kafka once the partition is back online. No data loss, then? Well, not always. There are many reasons, e.g. the volume of data a producer can pile up can be so large that the producer has to start dropping messages in order to not run out of disk space. Data loss. Also, the concept of data loss can be looked at more holistically. A consumer of the offline partition is affected, since it doesn't receive the usual flow of messages. There can be some time-sensitive processing downstream and if messages don’t flow through Kafka, there’s a data loss.
Second, there are cases in which unclean leader election doesn’t sacrifice consistency. If ISR has two brokers and first the non-leader and then the leader drops out, the topic becomes offline. If only the non-leader returns, Kafka won’t promote it to leader automatically, because it was not in the ISR and might not have all the data. In practice, in this case it would be safe to promote the non-leader to the leader using unclean leader election, because it does have all the data.
Let’s have a look at a toy example to illustrate what could happen in practice when topics go offline. Let’s say we have a Kafka cluster powering a web store business with topics:
- transactions: when a customer buys something, a message is produced here
- clicks: when a customer clicks something on the web site, a message is produced here
Let’s now assume that the web store runs for some time and orders keep on appearing on the transactions-topic and tracking events in the clicks-topic. But one night something goes wrong in the cluster and both topics go offline. In the morning when everybody’s back in the office again, the issue is noticed. The business is down.
We now know customers would be making purchases on the web store, but they can’t, since the transactions-topic is down. Also, we don’t have any more visibility on what the customers are doing on the web store, as the clicks-topic is down. Are these issues equally bad?
Quickly, we’ll figure out that by performing an unclean leader election for the clicks-topic we get the visibility back. There’s just a few hours' hole in the data. It doesn’t matter too much. The decision is clear - why not perform an unclean leader election.
Then everybody in the company is summoned to the meeting room to discuss how to recover the business, i.e. make the sales in the web store work again. Soon we’ll notice there’s only one possible solution. Trigger unclean leader election in the transactions-topic. Maybe lose some transactions that have piled up in the producers. Once that has been done, the web store works normally again. We might have lost some sales because of lost messages and might need to deal with the lost transactions. But at least the business is up again!
What do we learn here? Personally, I think preferring availability over durability always ends up winning when recovering from a disaster. There simply is no other option. In this case the option would have been to close down the business and go home.
So. Since we anyways resort to performing an unclean leader election, why not configure Kafka to do that automatically for us? If done, the business would quickly recover. For the clicks-topic, this could make sense. Generally, for this kind of metrics topic, it’s far more important to keep the message flowing through Kafka than potentially dropping some messages. For the transactions-topic, not so simple. Losing a single transaction is always an issue. This could happen with always enabled unclean leader election. A customer thinks he has made an order, but we’ve lost it. Not good. Probably not a good idea to enable automatic unclean leader election here.
It’s important to notice we’re talking about disaster recovery here, not the smooth operations day. This case of either of these topics being offline shouldn’t happen in the first place. But in the case of a disaster, there typically is damage that has already been done and further damage that can still be prevented. Unclean leader election lets the Kafka user to prevent future damage.
This all is a toy example, but it illustrates a pattern I’ve seen a little too many times in production Kafkas.
So, what should you do as a Kafka user to handle the issue better?
As a Kafka operator, you need to be aware of this issue and know how to perform an unclean election if ever needed. Preferably, you should inform your stakeholders of this edge case. As a business owner depending on Kafka, you should also be aware of this issue. Together the Kafka operator and business and the business stakeholder should create a disaster recovery plan. This plan should tell what to do in the case of an offline topic. It’s much easier to think about this beforehand, not when things are burning.
The Kafka community has recognized this issue and there’s a feature called Eligible Leader Replicas (ELR), available since Kafka 4.0. Since the tail of older Kafka clusters is long in production, this doesn't help those clusters. Without going too much into details of the ELR feature, its purpose is to make Kafka able to automatically recover from recoverable situations without data loss. The idea is that in addition to maintaining ISR, Kafka maintains a disjoint set called ELR containing brokers that are not in ISR anymore but are guaranteed to contain all committed records. Once there’s no live broker left in ISR, a broker from ELR can be added to ISR and promoted to be the leader. Even with ELR there are edge cases in which an offline topic doesn’t become online, and unclean leader election is needed.
In the early toy example ELR would have helped. The broker that returned would have been promoted to leader using the ELR logic. No manual intervention would have been needed. 👍
TL;DR
- Kafka 4.0 or newer: Enable ELR if you have new enough Kafka. No questions asked. You’ll sleep better.
- Any Kafka version: Create a disaster recovery plan for offline topics - you always end up pulling the lever.
Table of contents


