Kafka Exactly‑Once: Idempotent Producers vs Transactions
Choose between Kafka idempotent producers and the transactional API based on whether you need atomic writes across topics.
15 Jan 2026, 02:51 UTC

Duplicate events after a producer retry
An order service sends an event to Kafka, the broker acknowledges it, but the network drops before the producer receives the acknowledgment. The producer retries, and the consumer processes the same order twice. If that order triggers a charge or a warehouse reservation, the duplicate causes real financial harm. Kafka can prevent this, but the guarantee depends on whether you are writing to one topic or many, and whether your consumer needs to see only fully committed work.
Idempotent producers: exactly-once per partition
For writes to a single topic and partition, enable idempotence. The broker assigns each producer a persistent ID (PID) and tracks a sequence number for every batch. Duplicate batches with the same PID and sequence are discarded. This requires acks=all and keeps max.in.flight.requests.per.connection at 5 or lower.
props.put("enable.idempotence", true);
props.put("acks", "all");
props.put("max.in.flight.requests.per.connection", 5);
This is enough if your service writes to one topic and can tolerate seeing messages in partition order. It does not coordinate across topics or tie consumer offsets to the write.
When to use the transactional API
Use transactions when a single business action must write to multiple topics or partitions atomically, or when you want to commit consumer offsets in the same unit of work as the output records. The transactional API wraps beginTransaction, send, and commitTransaction around the writes. A broker transaction coordinator records the outcome in the internal __transaction_state topic.
props.put("transactional.id", "order-service");
props.put("enable.idempotence", true);
props.put("acks", "all");
KafkaProducer<String, Order> producer = new KafkaProducer<>(props);
producer.initTransactions();
try {
producer.beginTransaction();
producer.send(new ProducerRecord<String, Order>("orders", order.getId(), order));
producer.send(new ProducerRecord<String, Order>("inventory", order.getId(), reservation));
producer.sendOffsetsToTransaction(consumerOffsets, "order-consumer-group");
producer.commitTransaction();
} catch (Exception e) {
producer.abortTransaction();
throw e;
}
The transactional.id must be stable across restarts and unique per logical producer. Reusing it concurrently produces InvalidProducerEpoch errors.
Consumer isolation: read_committed
By default, consumers read uncommitted records immediately. Set isolation.level=read_committed so the consumer waits for the transaction marker before returning records from an open transaction. This prevents reading aborted writes, but adds latency equal to the transaction duration plus replication lag. It also does not prevent duplicate reads if the consumer crashes before committing offsets, which is why sendOffsetsToTransaction binds the offset commit to the same transaction.
props.put("isolation.level", "read_committed");
Trade-offs and operational limits
- Latency: each transaction adds coordinator round-trips, typically a few milliseconds per commit.
- Broker pressure: long-running transactions retain state in the coordinator. Keep
transaction.timeout.ms(default 1 minute) in mind and keep transactions short. - Consumer stalls: a slow or stalled producer holds the commit marker, pausing
read_committedconsumers. - Downstream duplicates: Kafka exactly-once ends at the broker. Databases, HTTP APIs, and other sinks still need their own idempotency keys or deduplication logic.
- Identifier reuse: two producer instances sharing the same
transactional.idwill conflict; use one ID per service instance or logical pipeline.
Verify before you deploy
- Start a local Kafka cluster using Docker.
- Run the transactional producer, then kill the process after
beginTransactionbut beforecommitTransaction. - Start a consumer with
read_committedand confirm it never sees the uncommitted order or inventory records. - Restart the producer with the same
transactional.idand confirm it can recover and continue withoutInvalidProducerEpocherrors. - Monitor broker metrics such as
ExpiredTransactionsPerSec. A sustained non-zero rate suggeststransaction.timeout.msis too low or the producer is hitting garbage-collection pauses.
Closing decision
If your pipeline writes to one topic and the consumer can handle at-least-once with deduplication downstream, an idempotent producer is simpler and cheaper. If you must update inventory, orders, and consumer offsets as one atomic unit, use the transactional API with read_committed consumers, keep transactions brief, and remember that any system outside Kafka still needs its own duplicate protection.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.