Learn Labs
Consumers

Offsets & Delivery Semantics

Committing offsets, at-most-once vs at-least-once, and idempotent consumers

A consumer group remembers its progress as a committed offset per partition, stored in the compacted __consumer_offsets topic. After a crash or a rebalance, the next owner of a partition starts from the committed offset. When you commit, relative to when you process, decides what a crash does.

The experiment

labs/delivery_semantics.py reads 100 messages in batches of 10, crashes halfway through the 5th batch (after processing 45), and lets a replacement consumer resume from the committed offset:

docker compose -f compose_prod.yaml run --rm labs python delivery_semantics.py

at-most-once    95 messages processed: 0 duplicates, 5 lost [45, 46, 47, 48, 49]
at-least-once  105 messages processed: 5 duplicates, 0 lost []

Messages 0-39 arrive in four batches, all processed and committed. The fifth batch is 40-49, and the crash comes after processing 40-44.

At-most-once: commit the batch, then process it. Offset 50 was committed before the crash, but only 40-44 were processed. The replacement started at 50, so 45-49 were never processed.

At-least-once: process the batch, then commit. The fifth batch was never committed, so the committed offset was still 40. The replacement started at 40 and processed 40-44 a second time. Nothing lost, five duplicates.

at-most-once4041424344454647484945-49 lostcommitted 50 (before work)crashat-least-once4041424344454647484940-44 processed twicecommitted 40crash

There's no setting that makes this go away. Either you commit before the side effect (risking loss) or after (risking duplicates). At-least-once plus idempotent processing is what nearly every production consumer does.

Auto-commit

By default (enable.auto.commit=true), the client commits the offsets of records returned by poll() every auto.commit.interval.ms (5 s), in the background. That commits records you may not have finished processing yet. In practice, if you process synchronously inside the poll loop it behaves close to at-least-once, but the guarantee is fuzzy, and a crash can lose or duplicate up to 5 seconds of work. For anything that matters, turn it off and commit explicitly:

enable.auto.commit=falseconsumer.commit(asynchronous=False)commit everything returned so far; blocksconsumer.commit(asynchronous=True)same, without blocking (check the callback)consumer.commit(message=msg)commit up to and including one messageconsumer.commit(offsets=[TopicPartition("t", 0, 1234)])an explicit offset

A common pattern: async commits in the loop (fast), and one sync commit on shutdown and in on_revoke.

Idempotent consumers

Since duplicates are unavoidable under at-least-once, make processing them harmless:

  • Natural idempotence: UPSERT rather than INSERT; SET balance = 70 rather than balance = balance - 30.
  • Dedup by id: store processed message ids (or topic-partition-offset) with a unique constraint, in the same database transaction as the side effect.
  • Store the offset with the data: write the result and the consumed offset in one DB transaction, and on startup seek() to the stored offset instead of using Kafka's. This is exactly-once into that database.

auto.offset.reset

What a group does when it has no committed offset for a partition (new group, or the committed offset was deleted by retention):

ValueStarts at
latest (default)The end: only new records
earliestThe beginning of what's retained
noneThrows an error, so you decide explicitly

Seeking

# replay from a point in time, e.g. after fixing a bug
offsets = consumer.offsets_for_times([TopicPartition("orders", p, ts_ms) for p in range(6)])
for tp in offsets:
  consumer.seek(tp)

# or from the CLI, with the group stopped:
kafka-consumer-groups.sh $B --group billing --topic orders \
--reset-offsets --to-datetime 2026-10-12T09:00:00.000 --execute

Lab

  1. Run delivery_semantics.py and trace exactly which offsets were committed when.
  2. Change the crash point to 47 and predict both outputs before running.
  3. Add dedup to the at-least-once path: keep a set() of processed values in the consumer and skip repeats. Duplicates now cause no "side effect".
  4. Create a group, commit, then delete the topic's early data with kafka-delete-records.sh past the committed offset. Start the group with auto.offset.reset=none and read the error.

On this page