How to detect duplicate messages in a kafka topic?
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 trillions of events a day. However, one of the challenges when dealing with large-scale message processing is the handling of duplicate messages. Duplicate messages can occur due to a variety of reasons including network issues, consumer failures, or producer retries. Detecting and managing duplicates is crucial for ensuring the accuracy of data processing systems.
Understanding Duplicate Messages in Kafka
Duplicate messages in Kafka can occur in two main contexts:
- Producer-side duplicates: When a producer sends a message more than once. This can happen if the producer doesn't receive an acknowledgment from the broker and retries sending the message.
- Consumer-side duplicates: When a consumer processes the same message multiple times. This can happen in cases of consumer rebalancing or if the consumer fails to commit its offset.
Techniques to Detect and Handle Duplicates
1. Idempotent Producers
Since Kafka 0.11, producers can be configured to be idempotent. This means that Kafka will ensure that exactly one copy of each message is written to the log. If a producer attempts to produce the same message again, Kafka will recognize it and avoid duplicating the message.
Configuration:
Set the enable.idempotence property to true in the producer configuration.
2. Using a Unique Key
Kafka messages can be key-value pairs. By ensuring that each message has a unique key, you can leverage Kafka's log compaction feature, which retains only the last message for each key. This approach can be effective for scenarios where only the latest state is relevant.
3. Custom Deduplication Logic
Implement custom deduplication in your consumer application by maintaining a cache or database of message identifiers that have been processed. When a new message is received, check the identifier against the stored identifiers.
Example using a HashSet:
4. Exactly Once Semantics (EOS)
Kafka transactions provide exactly once processing capabilities between producers and consumers. By using transactions, messages processed during a transaction are either all committed or all aborted, thus preventing duplicates across consumer groups.
Configuration:
Set the isolation.level to read_committed in the consumer configuration.
5. Log Compaction
Log compaction is a feature in Kafka where the Kafka broker retains only the last known value for each key within a partition. It’s particularly useful for key-value type messages where only the most recent value is interesting.
Best Practices and Recommendations
- Use a combination of techniques: Often, a single method may not suffice, especially in systems with complex business logic or higher reliability requirements.
- Monitor and audit: Implement monitoring to track duplicates and audit logs to ensure messages are processed as expected.
- Scale sensibly: Ensure that your deduplication storage solution scales with your Kafka usage.
Summary Table
| Method | Use Case | Consistency Level |
| Idempotent Producers | Simple deduplication across retries | High |
| Unique Key | State-oriented deduplication, log compaction | Medium to High |
| Custom Deduplication | Complex custom business logic | Depends on implementation |
| Exactly Once Semantics | End-to-end message processing integrity | Highest |
| Log Compaction | Retaining only the latest state per key | High |
Conclusion
Detecting and handling duplicate messages in Kafka is vital for data integrity in large-scale systems. Depending on the specific needs and characteristics of your application, you might choose one or a combination of several strategies outlined above to effectively manage duplicates.
Related reading
- How to determine a Kafka consumer's offset
- How to determine API version of Kafka?
- How to disable all Kafka related auto configuration from yaml/properties file in spring-boot-2 without removing dependencies?
- How to disable JSON schema in Kafka Source Connector (e.g. Debezium)
- How to disable RabbitMQ default tcp listening port - 5672
- How to do content filtering with Apache Kafka?
- How to do error handling with EasyNetQ / RabbitMQ
- How to dynamically add consumers in consumer group kafka

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.