Junior — Middle
Kuidas tagatakse võimalus edastada sõnumeid vähemalt korra Kafka's?
sobes.tech AI
Vastus AI-lt
In Apache Kafka, the delivery of messages with the guarantee of "at least once" is ensured through acknowledgments and offset management mechanisms.
The process is 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 acknowledgment (commit) of the offset to Kafka.
If the consumer crashes before acknowledgment, upon restart it will read the message again, 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, "") // confirm processing
}
return nil
}
Thus, if acknowledgment is not sent, the message will be delivered again.