Kafka
Duplicate Messages
Message Detection
Data Streaming
Kafka Topic

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.

Practice system design

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:

  1. 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.
  2. 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.

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
5props.put("enable.idempotence", "true");
6Producer<String, String> producer = new KafkaProducer<>(props);

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:

java
1Set<String> messageIds = new HashSet<>();
2ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
3for (ConsumerRecord<String, String> record : records) {
4    if (!messageIds.contains(record.key())) {
5        process(record);
6        messageIds.add(record.key());
7    }
8}

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.

java
props.put("isolation.level", "read_committed");

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

MethodUse CaseConsistency Level
Idempotent ProducersSimple deduplication across retriesHigh
Unique KeyState-oriented deduplication, log compactionMedium to High
Custom DeduplicationComplex custom business logicDepends on implementation
Exactly Once SemanticsEnd-to-end message processing integrityHighest
Log CompactionRetaining only the latest state per keyHigh

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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.