How does Kafka guarantee consumers doesn't read a single message twice?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a high-throughput distributed messaging system, is well-regarded for its durability and scalability. One of the key features of Kafka is its ability to prevent consumers from reading the same message more than once, even in the event of failures or rebalancing. Understanding how Kafka achieves exactly-once semantics (EOS) involves examining several of its core concepts and functionalities.
1. Consumer Offsets
In Kafka, every message within a partition has a specific and immutable offset. Kafka keeps track of the messages that each consumer group has processed by storing a unique offset for each group that denotes up to which message the group has processed. These offset values are committed to a Kafka topic named __consumer_offsets. Once a consumer from a group reads a message from a particular partition, it increments the offset. If the consumer successfully processes the message, it commits the new offset back to Kafka. Here, the coordination between reading, processing, and committing the offset ensures that a consumer does not process the same message multiple times.
2. Consumer Idempotence and Transactions
Kafka 0.11 introduced capabilities for idempotence and transactional writes, enhancing Kafka’s robustness concerning duplicate processing:
- Idempotent Producers: These producers prevent duplicates from being introduced when producing messages. An idempotent producer assigns a sequence number to each message tuple of
<producer_id, partition, seq_no>. Kafka brokers ensure that messages with the same tuple are considered as duplicates and thus not appended to the log again. - Transactional Producers: These producers can write to multiple partitions atomically. Using transactional producers, consumers can read committed transaction data only, ensuring that partial data sets from producers do not cause inconsistent states in consumer outputs.
3. Retries and Duplicates
Typically, network failures or consumer failures might lead to scenarios where messages might be read more than once. Proper acknowledgment and offset commitments ensure that even if a consumer reads a message again due to a retry, it does not reprocess it unless necessary. For example, if a consumer crashes after processing a message but before committing its offset, it might reprocess the last few messages upon restart. Logic within the consumer application is often required to handle such potential duplicates gracefully.
4. Consumer Configurations
Several Kafka consumer configurations are crucial in ensuring minimal duplicates:
- auto.offset.reset: This property dictates the consumer behavior when no initial offset is found or if the current offset does not exist anymore on the server.
- enable.auto.commit: If set to
true, the consumer's offset is periodically committed in the background. However, to guarantee that a message is processed exactly once, consumer applications often manage offset commits manually, committing offsets only after the message processing completes.
Summary Table
| Key Feature | Description | Impact on Duplicate Prevention |
| Consumer Offsets | Track the point to which each consumer group has consumed. | Prevents re-reading by managing up-to-date offsets. |
| Idempotent Producers | Producers so that messages are only written once to the log. | Reduces duplicates from producer side retries. |
| Transactional Writes | Allows atomic writing across multiple partitions. | Ensures complete sets of data are consumed. |
| Offset Management | Consumers can manually commit offsets after processing. | Avoids duplicates during consumer failover or rebalance. |
Conclusion
Kafka's design, with its robust handling of offsets, combined with the abilities for idempotence and transactional operations, substantially mitigates the risk of consuming a message multiple times. Understanding and leveraging these capabilities make Kafka a powerful tool for reliable data streaming and processing, satisfying stringent delivery semantics in complex distributed systems.
Related reading
- How does Kafka guarantee sequential disk access?
- How does Kafka handle a consumer which is running slower than other consumers?
- How does kafka handle network partitions?
- How does Kafka specify key alias for Client Authentication?
- How does one implement graph algorithms that require efficient contraction and expansion of connected components?
- How does one join string-type array-items, each with a comma character, except for the last item which has to be joined by and?
- How does Kafka store offsets for each topic?
- How does kafka streams compute watermarks?

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.