Sobes.tech
Junior — Middle

What methods can be used to implement the chosen message delivery type in Kafka?

sobes.tech AI

Answer from AI

In Apache Kafka, the message delivery type depends on the producer and consumer settings, as well as the application's architecture. The main methods of implementing delivery types:

  1. At most once

    • The producer sends messages without waiting for acknowledgment (fire-and-forget).
    • Messages may be lost in case of failures.
    • Settings: acks=0.
  2. At least once

    • The producer waits for acknowledgment from the broker (acks=1 or acks=all).
    • Messages may be delivered multiple times (duplicates).
    • The consumer must be prepared to handle duplicates.
  3. Exactly once

    • Uses transactional message sending.
    • Includes enable.idempotence=true and transactions (initTransactions(), beginTransaction(), commitTransaction()).
    • Ensures that each message is processed exactly once.

Example of producer configuration for Exactly once:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
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 delivery type is determined by the settings and message handling logic in Kafka.

What methods can be used to implement the chosen… - sobes.tech