Message duplication in Kafka typically comes from producer retries creating duplicate writes or consumers reprocessing messages after a rebalance, and both are addressed with idempotence and careful offset handling.
A payment service enabling idempotent producers and switching to manual offset commits after each transaction is durably written avoids charging a customer twice even if a producer retry or a mid-batch consumer rebalance occurs.
// Idempotent consumer: remember processed event ids so a re-delivered
// message (after a rebalance or retry) is skipped instead of charged twice
@KafkaListener(topics = "payments", groupId = "payment-service")
@Transactional
public void onPayment(PaymentEvent event, Acknowledgment ack) {
if (processedEventRepository.existsById(event.eventId())) {
ack.acknowledge(); // duplicate: already handled
return;
}
paymentService.charge(event);
processedEventRepository.save(new ProcessedEvent(event.eventId()));
ack.acknowledge(); // commit offset after the DB commit
}
# application.properties
spring.kafka.consumer.enable-auto-commit=false
spring.kafka.listener.ack-mode=manual
spring.kafka.producer.properties.enable.idempotence=trueA concise interview answer is
I'd enable idempotent producers to eliminate retry-caused duplicates, use transactional or exactly-once semantics where the processing needs it, and switch consumers to manual offset commits after processing completes so a rebalance can't cause messages to be reprocessed.
Last updated: 1 November 2026
Previous question: Your application requires high availability and fault tolerance for the Kafka cluster. How do you configure Kafka to meet these requirements?
Next question: What strategies would you use if your Kafka messages are larger than the default size limit (1 MB) and causing performance issues?