Junior — Middle
How is the possibility of delivering messages at least once in Kafka ensured?
sobes.tech AI
Answer from AI
In Apache Kafka, at-least-once message delivery is ensured through acknowledgments (acks) and offset management.
The process works as follows:
- The producer sends a message to Kafka and waits for acknowledgment from the broker (e.g., ack=all — acknowledgment from all replicas).
- The message is stored in the broker's log.
- The consumer reads the message and, after successful processing, sends an offset commit to Kafka.
If the consumer crashes before acknowledgment, it will read the message again upon restart, ensuring at-least-once delivery.
Example in Go using the sarama library:
consumer, err := sarama.NewConsumerGroup(brokers, groupID, config)
// ...
// In the message handler:
func (h *handler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for message := range claim.Messages() {
process(message) // process the message
sess.MarkMessage(message, "") // acknowledge processing
}
return nil
}
Thus, if acknowledgment is not sent, the message will be delivered again.