Offset Commits and Delivery Semantics

Offset Commits and Delivery Semantics in Apache Kafka: A committed offset is a promise: "everything before this is done." Every duplicate-processing and…

A committed offset is a promise: "everything before this is done." Every duplicate-processing and lost-message bug on the consumer side comes from breaking that promise.

Two different positions

PositionMeaning
Current positionWhere the consumer will read next in this session
Committed offsetWhere a new consumer of this group would start

They diverge between commits. That gap is the replay window after a crash.

Auto-commit and how it fails

With enable.auto.commit=true, the consumer commits the offsets of the last poll() periodically, from inside poll(). The commit therefore covers records that were delivered to your code, not records your code finished.

 poll() → 500 records
 auto-commit fires (offsets for all 500 committed)
 you have processed 120 of them
 process crashes

 restart: group resumes AFTER record 500
 → records 121-500 are never processed. Silent data loss.

And the mirror case:

 poll() → 500 records
 you process all 500 (side effects done)
 rebalance occurs before the next auto-commit
 → new owner replays all 500. Duplicate side effects.

Auto-commit is acceptable when reprocessing and skipping are both harmless — dashboards, sampled metrics. For anything with side effects, turn it off.

Manual commit shapes

// At-least-once: process, then commit
for (ConsumerRecord<String, String> r : records) handle(r);
consumer.commitSync();

// At-most-once: commit, then process (loses on crash)
consumer.commitSync();
for (ConsumerRecord<String, String> r : records) handle(r);

commitSync blocks and retries; commitAsync is faster but you must handle failures in the callback. A common production shape is commitAsync in the loop for throughput plus a final commitSync in the shutdown/revoked path for correctness.

The three semantics, honestly

SemanticsRecipeYou must tolerate
At most onceCommit before processingLost records on crash
At least onceCommit after processingDuplicate processing
Effectively onceAt-least-once delivery + idempotent side effects, or Kafka transactionsExtra design work

At-least-once is the correct default. Most systems cannot lose data but can be made to tolerate duplicates.

Why "exactly once" is not a flag

Kafka transactions give atomicity for the read-process-write pattern within Kafka: consume from topic A, produce to topic B, and commit the consumer offsets and the produced records as one atomic unit, read only by consumers with isolation.level=read_committed.

 producer.initTransactions()
 producer.beginTransaction()
   produce results to topic B
   sendOffsetsToTransaction(offsets of consumed records)
 producer.commitTransaction()     ← results and offsets commit together

What transactions cannot do is make an external side effect exactly-once. If processing charges a card, sends an email, or writes to a separate database, Kafka has no say in whether that happened twice. There is no protocol that makes an arbitrary remote effect atomic with a log commit.

So the real recipe for effectively-once end to end is:

  1. At-least-once delivery (acks=all, idempotent producer, commit after processing).
  2. Idempotent consumers — the part you own.

Making a consumer idempotent

TechniqueHow it worksFits
Natural idempotenceOperation is a set, not an increment: status = SHIPPEDState updates
Deduplication storePersist processed event IDs, skip repeatsSide effects like emails
Upsert by keyInsert-or-update on a unique business keyDatabase sinks
Version / sequence checkIgnore events with a version at or below what you storedOrdered entity updates
Transactional outboxWrite business row and outbox row in one DB transactionProducing from a service

Note the anti-pattern: balance = balance + amount is not idempotent, and replay silently corrupts data. balance = amount computed from an authoritative source is.

A deduplication store needs a bounded retention window, or it becomes an unbounded table and a new operational problem.

Where to start reading

auto.offset.reset applies only when the group has no committed offset (new group, or offsets expired):

  • earliest — read the whole retained history. Safe for correctness, potentially a large first run.
  • latest — start from now. Convenient, and silently skips everything currently in the topic.
  • none — throw. Useful when starting position must be an explicit decision.

Choosing latest for a new billing consumer is a classic quiet data-loss incident.

Consumer lag: the health signal

Lag = log end offset − committed offset, per partition.

 partition   end offset   committed   lag
 p0            1,204,553   1,204,401    152
 p1            1,198,220   1,198,101    119
 p2            1,201,880     903,442  298438   ← problem

Treat lag as the primary consumer SLI, and monitor it per partition, not just as a group total. A group total hides exactly the failures you care about: one hot or stuck partition, or one consumer that is alive but not progressing.

Read the derivative, not the value. Steady non-zero lag is fine. Monotonically increasing lag means consumption is slower than production and will not recover on its own. A cliff to zero usually means an offset reset, not a fix.

Check it from the CLI:

kafka-consumer-groups.sh --bootstrap-server localhost:9092 \
  --describe --group billing-service

Exercise: A consumer group processes payment events with enable.auto.commit=true and a 30-second processing step per record. Finance reports both duplicate charges and occasional missing charges from the same time window. Explain how one setting produces both symptoms, then design a fix covering offset handling and consumer idempotence, and state which duplicates your fix still cannot prevent.