Kafka unrecoverable if broker dies
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform known for its robustness and high availability, primarily used for building real-time data pipelines and streaming applications. It is designed to be fault-tolerant, with automatic recovery features in place. However, certain situations, such as the complete failure of a broker without adequate replication, can lead to unrecoverable data losses. This article explores the scenarios where Kafka might face unrecoverable errors if a broker dies, technical insights into how Kafka manages data, and strategies to mitigate these risks.
Understanding Kafka's Architecture
Kafka stores streams of records in categories called topics. In a typical setup, topics are split into partitions for better manageability and scalability, with each partition being an ordered, immutable sequence of records. Partitions allow Kafka topics to be parallelized by splitting the data across multiple brokers.
Replication for Fault Tolerance
To ensure data availability and fault tolerance, Kafka replicates partitions across multiple brokers. This means that each partition may have one or more replicas distributed across different brokers. The number of replicas is configurable and is set by the replication.factor property. There is one leader and the rest are followers for each partition. The leader handles all read and write requests for the partition, while the followers passively replicate the leader.
If the leader broker dies, one of the followers can automatically take over as the new leader. However, the effectiveness of this mechanism heavily depends on having the replicas and the replication factor configured correctly.
When Can Data Loss Occur?
Data loss in Kafka can occur if the following situations are met:
- Single Replica: If a partition is configured with a
replication.factorof 1, there are no additional copies of the partition's data. If the broker storing this partition fails, the data is lost irrecoverably. - In-sync Replicas are Out of Sync: Kafka maintains a list of in-sync replicas (ISR), which are replicas that have fully caught up with the leader. If all replicas in the ISR list are lost or out of sync at the time of the leader’s failure, remaining data may not contain the latest writes and hence can lead to data loss.
- Unflushed Data on Leader Broker: Data written to a Kafka broker isn't instantly flushed to disk; it might reside in memory for a period depending on the configuration (
log.flush.interval.messagesorlog.flush.interval.ms). If a broker fails before this data is flushed to disk, any unflushed data is lost.
Example of a Failure Scenario
Consider a Kafka cluster with 3 brokers and a topic configured with replication.factor = 2. If the leader broker of a partition and its only replica are on two of the brokers and both of these brokers fail simultaneously, the data in that partition is unrecoverable.
Mitigating Risks of Unrecoverable Data
Several strategies can prevent data loss in Kafka:
- Increase Replication Factor: Setting a higher
replication.factorfor critical topics can safeguard against multiple broker failures. - Regular Monitoring of ISR: Ensuring that all replicas remain in the ISR by monitoring and alerting if the size of the ISR shrinks below a threshold.
- Concise Disk Flush Policy: Configuring an appropriate disk flush policy to balance performance and data safety.
Key Points Summary
| Aspect | Description |
| Replication Factor | Higher replication factors increase data safety but consume more resources. Ideal factor depends on data criticality. |
| ISR (In-sync Replicas) | Critical for determining data loss risks. Out-of-sync replicas can lead to data loss during broker failures. |
| Flush Policies | Tuning flush intervals can minimize unflushed data losses but may impact performance. |
Conclusion
While Kafka is designed with mechanisms to handle failures and ensure data availability, the configuration and maintenance practices play a pivotal role in preventing data loss. Understanding the intricacies of Kafka's replication model, actively monitoring the health of the Kafka brokers, and intelligently setting the configuration parameters can significantly mitigate the risks associated with unrecoverable errors.
Related reading
- kafka upgrade to .9 with new consumer api
- Kafka Use common consumer group to access multiple topics
- Kafka Utils wrong classpath org.apache.kafka.common.utils.Utils
- kafka vs chronicle queue vs disruptor
- Kafka What is the minimum number of brokers required for high availability?
- Kafka, will different partitions have the same offset number
- Kafka vs. MongoDB for time series data
- Kafka vs SignalR

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.