Introduction
When a partition leader fails and no in-sync replica is available, the cluster has two options. It can leave the partition offline until an in-sync replica returns, or it can elect an out-of-sync replica and lose any messages that replica never received. unclean.leader.election.enable makes that choice, and the default differs between open source Kafka and some managed services.
This article explains the trade-off between availability and durability, the configuration at broker and topic level and how defaults vary by provider, and how to decide which behaviour your system needs.
Availability vs Durability
Consider the following Kafka Broker state, with three Broker nodes. The diagram shows that node 1 is the lead replica for a particular topic partition, and the three messages written to the partition, namely ‘foo’, ‘bar’ and ‘xyz’, are being replicated to nodes 2 and 3.

When unclean leader election is enabled the following scenario results in message loss

In this failure scenario, if unclean leader election was not enabled, then clients would have to wait for a consistent insync replica to come back online before it can perform further reads and writes to that topic partition. This configuration therefore favours durability over availability.
Configuration
The configuration parameter is unclean.leader.election.enable. It can be configured on the broker, and can be overridden on a per topic basis by applying the configuration to the topic itself.
The default configuration for the core Apache Kafka Broker and topics is false, meaning durability is favoured over availability. Note that this default was changed, as prior to version 0.11.0.0 it defaulted to true. 0.11.0.0 was released in June 2017 however, so it is unlikely to still be in use.
If using a managed Kafka service provider this default may again vary, so it is vital to check the documentation to determine whether the configuration is as required.
At the time of writing the Confluent offering defaults to false, while the Amazon’s AWS Managed Streaming for Apache Kafka (MSK) offering defaults to true.
Confluent defaults: https://docs.confluent.io/platform/current/installation/configuration/broker-configs.html
MSK defaults: https://docs.aws.amazon.com/msk/latest/developerguide/msk-default-configuration.html
| Kafka Provider | Unclean Leader Election | Favours |
|---|---|---|
| Non-managed (\< 0.11.0.0) | true | Availability over durability |
| Non-managed (0.11.0.0+) | false | Durability over availability |
| Confluent | false | Durability over availability |
| AWS MSK | true | Availability over durability |
Conclusion
For systems that deem message loss as unacceptable, it is important to ensure that unclean leader election is not enabled. Where message loss is however acceptable, and high availability is considered more important, then unclean leader election can be enabled. In some cases it may be prudent to enable unclean leader election on some topics, and disable on others, and Kafka provides the flexibility to achieve this.
