Messages lost when Kafka nodes are restarted
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 capable of handling large volumes of data. It is designed with fault tolerance and durability in mind. However, under certain circumstances, such as when nodes are restarted, messages might appear to be lost. This article explores why these situations occur and how to mitigate them, providing technical explanations and examples.
Understanding Kafka Architecture and Message Storage
At its core, Kafka maintains streams of records in categories known as topics. Within a topic, records are split into partitions, and each partition is replicated across a set of brokers (server nodes) for fault tolerance. The main components involved in the storage and retrieval of messages include:
- Broker: A single Kafka server which is part of a Kafka cluster.
- Topic: A category or feed name to which records are published.
- Partition: A division of a topic. Each partition can be hosted on a different broker.
- Replica: A copy of a partition for fault tolerance. There can be one or more replicas of a partition.
Each partition has one leader and zero or more followers. The leader handles all read and write requests for the partition, while the followers passively replicate the leader's log.
Factors Leading to Message Loss
1. Unclean Leader Election
When a broker goes down, it may trigger an unclean leader election if no in-sync replicas are available to replace it. Unclean leader election occurs when a follower that was not fully synced is forced to become the leader. Subsequently, any messages that were not replicated to this new leader before the old leader went down could be lost.
2. Acknowledgment and Replication Policies
Kafka uses acknowledgments to ensure message durability. Producers can choose from three acknowledgment policies:
acks=0: The producer does not wait for any acknowledgment from the server.acks=1: The producer receives an acknowledgment after the leader has written the data.acks=all: The producer gets an acknowledgment after all in-sync replicas have received the data.
Lower ack settings (e.g., acks=0, acks=1) are susceptible to data loss if a broker fails before data is sufficiently replicated.
3. Incomplete Replication
If a Kafka node goes down during a write operation, and the replicas have not completed syncing, the messages might be lost depending upon the broker's durability settings and the nature of the failure.
4. Log Retention Policy
Kafka's log retention policy might also inadvertently cause perceived message loss. The broker could be set to delete old messages after a certain period, or once a size limit is reached.
Mitigations Strategies
To mitigate the risk of message loss, consider the following strategies:
- Increase Replica Factor: Ensure that partitions have enough replicas across different brokers. More replicas mean a higher chance of recovery without data loss.
- Careful Configuration of acks: Use
acks=allto ensure that messages are replicated to all in-sync replicas before acknowledgment. - Proper Monitoring: Monitor Kafka brokers and partitions to ensure that replicas remain in sync, and react quickly to desynchronization.
- Replica Synchronization: Ensure follower replicas are fully caught up with the leader before a leader election.
Example Scenario
Imagine a Kafka cluster with 3 brokers, where each partition has one leader and two followers (replication factor of 3). If a broker goes down during a high write load and acks=1 is configured, some messages may not be replicated to the followers before the failure, resulting in potential data loss.
Summary
| Aspect | Description | Mitigation Strategy |
| Unclean Leader Election | Occurs if no in-sync replica is available, leading to potential data loss. | Ensure sufficient in-sync replicas; use replica synchronization. |
| Acknowledgments | Determines when a producer receives confirmation of message storage. | Use acks=all for high durability. |
| Incomplete Replication | If a broker fails during a write operation, messages may be lost if not replicated. | Increase replication factor; monitor replica lag. |
| Log Retention Policy | Old messages can be deleted based on retention configuration. | Adjust log retention policies in accordance with data importance. |
By understanding the underlying causes and deploying strategic mitigations, Kafka administrators can substantially reduce the risk of message loss during node restarts or other disruptions.
Related reading
- Messages with expiration are not removed from RabbitMQ
- Messaging platform with QoS / Kafka partition overloading
- Metadata information from kafka
- Metadata requests in Kafka producer
- Metadata file '.dll' could not be found
- MetadataException Unable to load the specified metadata resource
- Micronaut Kafka Health check fails with Cluster authorization failed
- Microservices Why Use RabbitMQ?

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.