Lesson 4 of 9 · 28m
Delivery Semantics Without the Marketing Fog
Separate producer, broker, and consumer responsibilities for at-most-once, at-least-once, and exactly-once processing — and implement idempotency deliberately.
"Exactly once" is the phrase most responsible for bad Kafka architecture. Kafka can provide exactly-once semantics across specific producer and stream-processing boundaries, but it cannot make your email provider, payment gateway, or downstream HTTP service magically transactional. The useful question is narrower: where can duplicates or loss appear, and what does correctness require at each boundary?
At-most-once: fast, lossy, sometimes correct
At-most-once processing means a record may be skipped but should not be processed twice. Consumers usually implement it by committing offsets before doing work:
poll record -> commit offset -> process recordIf the process crashes after the commit, the record is never handled. That is acceptable for metrics samples or ephemeral telemetry where loss is preferable to duplication. It is rarely acceptable for money, inventory, security events, or workflow state.
At-least-once: the default serious choice
Commit after the work:
poll record -> process record -> commit offsetIf the consumer crashes after processing but before committing, the record is redelivered after restart or rebalance. Duplicates are not a bug in this design; they are the price of not losing data. Your handler must therefore tolerate repetition.
A sane Java consumer configuration begins with manual control:
enable.auto.commit=false isolation.level=read_committed max.poll.interval.ms=300000Then commit only after the side effect succeeds; in the Java client that is commonly an explicit consumer.commitSync() at a controlled batch boundary.
If the side effect is a database write, prefer making the event application and offset progression part of one transaction where the architecture permits it.
Producer idempotence is narrower than application idempotence
Kafka's idempotent producer prevents duplicates introduced by producer-to-broker retries for a partition and producer session:
enable.idempotence=true acks=allIt does not understand business identity. If your application retries charge card because a timeout occurred upstream, Kafka cannot know those two records represent the same business operation. Put a stable event ID or idempotency key in the payload and enforce uniqueness downstream.
{"event_id":"01J9R7Y7K3ZXG5M0P6Q2W8E4TC","type":"payment.authorized","payment_id":"pay_38291","occurred_at":"2026-11-30T18:20:41Z"}Kafka transactions connect specific write sets
Transactional producers can atomically write across partitions and commit consumer offsets as part of a consume-transform-produce loop:
transactional.id=stream-worker-17 isolation.level=read_committedThis is powerful inside Kafka-centric systems, especially stream processing. It still does not transactionally encompass arbitrary external systems. You cannot roll back an email after a Kafka transaction aborts.
Practical correctness pattern
For external side effects, use the outbox/inbox shape: write intent atomically in your own database, publish asynchronously, and make consumers idempotent by event ID. For projections, make replay safe. For commands, make retries converge rather than multiply effects.
Field notes
- Auto-commit can commit positions for records still being processed if your threading or async handling is careless.
commitSyncblocks and retries the current position;commitAsyncis faster but introduces failure ambiguity. Know which failure you prefer.- Batch side effects carefully: committing the batch high-water mark means every prior record's effect must already be durable.
- Exactly-once claims must name the boundary. "Exactly once into Kafka" and "exactly one customer email" are different universes.
Was this lesson helpful?