Kafka Log Compacted Topic Duplication Values against same key not deleted
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 popular distributed streaming platform used primarily for building real-time data pipelines and streaming applications. One of Kafka's features includes topics where messages are published. These messages are stored as key-value pairs within partitions. Kafka supports various topic configurations, including the log-compacted topics, which play a crucial role in maintaining data consistency and handling stateful operations.
Understanding Log Compaction
Log compaction ensures that Kafka retains at least the last known value for each key. This feature is essential for scenarios where retaining a complete history of record changes isn't necessary or feasible due to storage constraints. Instead, Kafka keeps the updated state of each key, removing older records with the same key to save space and ensure faster state recovery.
In a log-compacted topic, when a new message with a key comes in, Kafka will mark the older record(s) with the same key for deletion. However, these records won't be immediately removed. They will only be replaced during the next log compaction cycle.
Why Duplications Occur in Log Compacted Topics
Despite the log compaction mechanism, scenarios exist where duplicated values against the same key might not be deleted immediately. Here are a few reasons behind this behavior:
1. Compaction Lag
Log compaction is not an instantaneous process but occurs periodically. This lag can lead to temporary duplication of values for the same key. Records that are marked as obsolete (due to being superseded by newer records with the same key) remain in the log until the compaction runs.
2. Active Segment is Not Compacted
Kafka compacts log segments, which are groups of messages. However, the active segment where new messages are being written is not compacted immediately. Duplication can occur if many messages with the same key are written to the active segment before it is closed and a new segment is opened, after which compaction can occur.
3. Multiple Partitions and Consumer Configurations
When multiple partitions are used, a key's records can appear in different partitions. Log compaction works within a partition and not across partitions, causing potential duplicates if key-value data isn't partitioned thoughtfully.
4. Cleanup Policies
Kafka also allows specifying a cleanup policy. When set to compact, log compaction is enabled. Sometimes, a topic may have a cleanup policy set to delete, which means messages are deleted based on age or size, without considering the keys. This policy setting might interfere with how duplicates are handled.
Examples of Compaction
Consider a Kafka topic configured for log compaction and receiving the following sequence of messages:
Under ideal conditions, after compaction, only the last entries for each key should remain:
However, due to reasons such as compaction lag or active segment behaviors, the log might temporarily hold duplicates for User1.
Key Takeaways for Handling Duplications
| Factor | Behavior and Impact on Duplication |
| Log Compaction Frequency | Less frequent compactions can lead to prolonged data duplication. |
| Active Segment Behavior | New writes are not immediately compacted, allowing duplicates in the active log. |
| Partition Strategy | Poor key partitioning can lead to discrepancies in duplication behavior across partitions. |
| Cleanup Policy | Inconsistent policies (compact vs. delete) can affect how data is managed and duplicated. |
To minimize issues with duplication while using log-compacted topics in Kafka, consider designing a key strategy that matches your application's needs, setting appropriate compaction settings, and monitoring topic and partition configurations. Ensuring these factors can help in leveraging the full potential of Kafka's log compaction feature for efficient data handling and storage optimization.
Related reading
- Kafka Log Compaction not starting
- Kafka Login module not specified in JAAS config
- kafka log.retention.hours inconsistency in multiple brokers
- kafka logs + how to limit the logs size
- kafka loses all topics on reboot
- Kafka message corrupted in master but replica looks good
- Kafka log.segment.bytes vs log.retention.hours
- Kafka make consumer group Inactive

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.