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.
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:
A common pattern: async commits in the loop (fast), and one sync commit
on shutdown and in on_revoke.
The committed offset is the offset of the next record to read, not the last one processed. Committing offset 50 means "everything up to 49 is done." Getting this off by one in manual commits reprocesses or skips a record on every restart.
Idempotent consumers
Since duplicates are unavoidable under at-least-once, make processing them harmless:
- Natural idempotence:
UPSERTrather thanINSERT;SET balance = 70rather thanbalance = 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):
| Value | Starts at |
|---|---|
latest (default) | The end: only new records |
earliest | The beginning of what's retained |
none | Throws an error, so you decide explicitly |
A consumer that's down longer than the topic's retention comes back to a
committed offset that no longer exists, and auto.offset.reset silently
kicks in. With latest, it skips straight to the end and everything in
between is never processed, without any error. Monitor lag against
retention, and consider none for consumers where silent skipping is
unacceptable.
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 --executeLab
- Run
delivery_semantics.pyand trace exactly which offsets were committed when. - Change the crash point to 47 and predict both outputs before running.
- 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". - Create a group, commit, then delete the topic's early data with
kafka-delete-records.shpast the committed offset. Start the group withauto.offset.reset=noneand read the error.