Delivery Semantics and Transactions: At-Least-Once, At-Most-Once and Exactly-Once

Apache Kafka 19 min min read Updated: Nov 17, 2026 Advanced
Delivery Semantics and Transactions: At-Least-Once, At-Most-Once and Exactly-Once
Advanced Topic 2 of 4

The Three Guarantees

GuaranteeMeaningHow you get itRisk
At-most-onceEach message delivered zero or one timeProducer acks=0, or consumer commits before processingLoss
At-least-onceEach message delivered one or more timesacks=all + retries, consumer commits after processingDuplicates
Exactly-onceEach message's effect happens onceIdempotent producer + transactions + read_committed (inside Kafka), idempotent or transactional sinks outsideMore complexity and some latency

Step 1: Idempotent Producer (Per-Partition Exactly Once)

The broker tracks each producer's id and per-partition sequence numbers, so retries do not create duplicates inside one producer session and partition. See the producer reliability lesson.

Step 2: Transactions (Atomic Writes to Many Partitions and Topics)

A transaction lets a producer write to several partitions and topics (and commit consumer offsets) so that either all become visible or none.

java Properties p = new Properties(); p.put("bootstrap.servers", "localhost:9092"); p.put("key.serializer", StringSerializer.class.getName()); p.put("value.serializer", StringSerializer.class.getName()); p.put("transactional.id", "order-processor-1"); // unique and STABLE per producer instance (enables fencing) // idempotence and acks=all are enabled automatically with a transactional.id KafkaProducer<String, String> producer = new KafkaProducer<>(p); producer.initTransactions(); // registers the id, fences older instances try { producer.beginTransaction(); producer.send(new ProducerRecord<>("payments", "c1", "{\"amount\":250}")); producer.send(new ProducerRecord<>("ledger", "c1", "{\"debit\":250}")); producer.commitTransaction(); // both records become visible together } catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) { producer.close(); // fatal: another instance took over, stop } catch (KafkaException e) { producer.abortTransaction(); // none of the records become visible }

Step 3: Consumers Must Read Committed Data

java props.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed"); // hide records of open or aborted transactions
isolation.levelBehaviour
read_uncommitted (default)Sees everything, including records of aborted transactions
read_committedSees only committed transactional records and non-transactional records. May wait for open transactions to finish (higher end-to-end latency)

Consume-Transform-Produce: Exactly-Once Pipeline

The classic use: read from topic A, compute, write to topic B, and record the input offset in the same transaction. After a crash, either both the output and the offset commit happened, or neither.

java consumer.subscribe(List.of("orders")); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500)); if (records.isEmpty()) continue; producer.beginTransaction(); try { Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>(); for (ConsumerRecord<String, String> r : records) { String enriched = enrich(r.value()); producer.send(new ProducerRecord<>("orders-enriched", r.key(), enriched)); offsets.put(new TopicPartition(r.topic(), r.partition()), new OffsetAndMetadata(r.offset() + 1)); } producer.sendOffsetsToTransaction(offsets, consumer.groupMetadata()); // offsets commit atomically with the output producer.commitTransaction(); } catch (Exception e) { producer.abortTransaction(); // then seek the consumer back to the last committed offsets and retry resetConsumerToCommitted(consumer); } }
Easier route: Kafka Streams does all this for you: set processing.guarantee=exactly_once_v2 and it handles transactions, offsets and fencing.

Zombie Fencing

If an application instance is paused (long GC or network partition) and a replacement starts with the same transactional.id, the old instance is a zombie. When the new one calls initTransactions(), the broker bumps the producer epoch, and the zombie's later writes are rejected with ProducerFencedException. This is why the transactional.id must be stable and unique per logical worker (for example derived from the input partition or the pod name).

Transaction Settings

SettingMeaning
transactional.idIdentity of the producer across restarts
transaction.timeout.ms (producer, default 60 s)A transaction not completed in this time is aborted by the broker
transaction.max.timeout.ms (broker)Upper limit for the producer timeout
transaction.state.log.replication.factor, transaction.state.log.min.isrReplication of the internal transaction topic. Use 3 and 2 in production

What Exactly-Once Does Not Cover

Side effects outside Kafka: Kafka transactions only cover reads and writes inside Kafka. If your handler also sends an email, calls an API or writes to a database, those effects can still repeat after a retry. For external systems use idempotent writes (upsert by key, unique event id), a transactional outbox, or a Kafka Connect sink that supports exactly-once.
PipelineRealistic guarantee
Kafka to Kafka with transactions or Kafka Streams EOSExactly-once
Kafka to database with an idempotent upsertEffectively exactly-once
Kafka to HTTP call or emailAt-least-once. Send an idempotency key
Database to Kafka (outbox, CDC)At-least-once into Kafka, deduplicate downstream by event id
Do you need it? Many systems are fine with at-least-once plus idempotent consumers, which is simpler and faster. Use transactions where duplicates are costly and the pipeline is Kafka to Kafka.

Conclusion

At-least-once with idempotent handlers is the practical default. Exactly-once inside Kafka uses idempotent producers, transactions with sendOffsetsToTransaction, stable transactional.ids and read_committed consumers. It does not make external side effects exactly-once.

Quick Summary: idempotence < transactions; sendOffsetsToTransaction; read_committed; stable transactional.id; external effects need idempotent writes.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators