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
| Position | Meaning |
|---|---|
| Current position | Where the consumer will read next in this session |
| Committed offset | Where 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
| Semantics | Recipe | You must tolerate |
|---|---|---|
| At most once | Commit before processing | Lost records on crash |
| At least once | Commit after processing | Duplicate processing |
| Effectively once | At-least-once delivery + idempotent side effects, or Kafka transactions | Extra 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:
- At-least-once delivery (
acks=all, idempotent producer, commit after processing). - Idempotent consumers — the part you own.
Making a consumer idempotent
| Technique | How it works | Fits |
|---|---|---|
| Natural idempotence | Operation is a set, not an increment: status = SHIPPED | State updates |
| Deduplication store | Persist processed event IDs, skip repeats | Side effects like emails |
| Upsert by key | Insert-or-update on a unique business key | Database sinks |
| Version / sequence check | Ignore events with a version at or below what you stored | Ordered entity updates |
| Transactional outbox | Write business row and outbox row in one DB transaction | Producing 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.