Sobes.tech
Senior

What are the message delivery strategies in Kafka and how are they implemented?

sobes.tech AI

Answer from AI

In Kafka, there are three main message delivery strategies:

  1. At most once

    • A message can be delivered 0 or 1 times.
    • Used when message loss is acceptable.
    • Implemented when the producer does not wait for acknowledgment from the broker (fire-and-forget).
  2. At least once

    • A message is guaranteed to be delivered, but may be delivered multiple times (duplicates possible).
    • This is the standard mode of Kafka operation.
    • The producer waits for message write acknowledgment (acks=all), and the consumer commits offsets after processing.
  3. Exactly once

    • A message is delivered and processed exactly once.
    • Achieved using Kafka transactions and an idempotent producer.
    • Prevents duplicates even in case of failures.

Example of configuring a producer for "exactly once":

Properties props = new Properties();
props.put("enable.idempotence", "true");
props.put("transactional.id", "my-transactional-id");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);

producer.initTransactions();
try {
    producer.beginTransaction();
    producer.send(new ProducerRecord<>("topic", "key", "value"));
    producer.commitTransaction();
} catch (Exception e) {
    producer.abortTransaction();
}

Thus, the choice of strategy depends on the reliability requirements and the acceptability of duplicates in the system.

What are the message delivery strategies in Kafka and… - sobes.tech