3 · What a commit really guarantees
In one sentence: a commit is a record saying “for this partition, read offset N next”; written after the work, a crash repeats work (at-least-once), written before it, a crash skips work (at-most-once), and what is repeated or skipped is whatever lies between the bookmark and the work.
The problem it solves
Billing’s member has fetched 41 orders and billed 23 of them when its machine dies. A new process carries on from the group’s bookmark. If the bookmark was moved past the 41 before billing them, 18 customers are never billed. If it is still before them, 23 customers are billed twice.
Kafka can’t know which of the 41 were billed: only your code knows. What a crash costs is decided by when your code moves the bookmark.
The idea: move the bookmark after the work
Moving the bookmark is a commit. Commit after processing, and a crash repeats at most what was processed since the last commit: at-least-once, nothing lost, some things twice. Commit before processing, and a crash skips what was fetched but not yet done: at-most-once. Most systems choose at-least-once and make a repeat harmless (the second Go deeper).
A commit is a record
Six partitions, 100 records each (offsets 0 to 99), all read by group c-one, which committed after processing:
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
c-one orders 0 100 100 0 - - -
...
c-one orders 5 100 100 0 - - -
The last record processed was 99; the bookmark says 100, the next one to read. It is a record that the group’s
coordinator writes to __consumer_offsets, keyed by group, topic and partition:
{"key":{"type":1,"data":{"group":"c-one","topic":"orders","partition":3}},"value":{"version":4,"data":{"offset":100,"leaderEpoch":0,"metadata":"","commitTimestamp":1796009993539,"topicId":"AyIxLlCeQciYRLorVUm78A"}}}
A newer commit for the same key replaces it. A restarted member, or a new owner after a rebalance, asks for it and starts there.
Auto-commit: every 5 seconds, inside poll()
By default a member commits by itself: enable.auto.commit=true, auto.commit.interval.ms=5000. In three runs of
group c-auto, commits came 4,987 to 5,001 ms apart (plus one as each run closed).
There is no timer behind it. A member that spends 20 s on each batch (500 records, 40 ms each) committed 20,105 to
20,150 ms apart (three runs): auto-commit happens inside poll(), and commits what earlier polls returned. So
it is at-least-once only if your loop processes everything a poll() returned before calling it again (the
KafkaConsumer javadoc
says so); hand records to another thread and a crash can lose them.
Three ways to commit, killed mid-stream
A producer sends 100 orders a second; a member processes them at 10 ms each, writing each order’s partition and
offset to a file. It is killed with docker kill and started again with the same group, three times per way:
| How it commits | Processed twice | Never processed |
|---|---|---|
| auto-commit (the default) | 304, 380, 7 | 0, 0, 0 |
commitSync() after each batch | 23, 20, 8 | 0, 0, 0 |
commitSync() before processing | 0, 0, 0 | 2, 30, 9 |
The kills came at random points, 5 to 14 s in, so compare the columns, not the numbers. Three more auto-commit runs, killed 1.1 to 3.1 s in, before the first commit, each lost about 1,780 orders: there was no bookmark to return to (the next section).
Committing after each batch repeats at most the batch in progress. The first killed run had just fetched 41:
04:10:03.064 m1 processed 71
04:10:03.065 m1 polled 33
04:10:03.404 m1 committed {0=29524, 1=29524, 2=29523, 3=29523, 4=29522, 5=29522} in 3 ms
04:10:03.405 m1 polled 41
Started again, it resumed at that commit and processed 23 of the 41 a second time:
04:10:21.361 m1 assigned [0, 1, 2, 3, 4, 5] committed {0=29524, 1=29524, 2=29523, 3=29523, 4=29522, 5=29522}
...
total: 3232 lines, 23 duplicates, 0 missing
No bookmark yet: auto.offset.reset
When a group has no commit for a partition, auto.offset.reset decides where it starts. On 289,729 records
already in the topic, with nothing being produced:
auto.offset.reset | New group read |
|---|---|
latest (the default) | 0: it waits for new records |
earliest | all 289,729 |
none | nothing: the consumer throws |
04:29:26.118 m1 stopped: org.apache.kafka.clients.consumer.NoOffsetForPartitionException: Undefined offset with no reset policy for partitions: [orders-0, orders-1, orders-2, orders-3, orders-4, orders-5]
It only applies when there is no commit. c-one, started again with earliest, resumed at its bookmark, 100.
commitSync or commitAsync
commitSync() waits for the coordinator’s answer and throws if the commit fails. commitAsync() returns at once
and reports the result to a callback; a failure reaches only that callback. On this machine, committing after
every poll, a commit’s round trip took 1 to 22 ms either way (median 2 ms, three runs each, about
1,450 commits a run); measure on yours. The difference that holds everywhere: only commitSync() stops your loop when a commit
fails.
Commits around a rebalance
In an eager rebalance every member gives up all its partitions, and each new owner, even the old owner getting
one back, starts from the bookmark. Two members, m2 joining m1, then m2 stopping:
| How m1 and m2 commit | Processed twice |
|---|---|
| every 1,000 records | 138, 573, 1,208 |
every 1,000 records, and in onPartitionsRevoked | 0, 0, 0 |
| auto-commit | 0, 0, 0 |
With commits every 1,000 records, each rebalance sent every partition back to its last commit, even m1 getting
its own partitions back: in one run from its commit of 1.6 s before the revoke. In another, the rebalance came
before any commit, so both started at the end of their partitions (latest): 55 orders were never processed.
Committing in onPartitionsRevoked, which the
ConsumerRebalanceListener javadoc
recommends, hands over exactly:
04:24:06.560 m1 committed on revoke {0=43579, 1=43579, 2=43578, 3=43578, 4=43577, 5=43577}
04:24:06.581 m1 assigned [0, 1, 2] committed {0=43579, 1=43579, 2=43578}
04:24:06.599 m2 assigned [3, 4, 5] committed {3=43578, 4=43577, 5=43577}
Auto-commit repeated nothing in its three runs either.
Try it on your computer
The capstone, after lesson 4, has you write a consumer that survives exactly these kills, and count the orders it repeats and loses.
Go deeper: what else a commit holds, and when it expires
The value has more than the offset: leaderEpoch (the partition leader’s epoch of the last record read; 0
in the record above), metadata (a string you may attach to a commit) and commitTimestamp.
Bookmarks don’t last forever. The broker setting offsets.retention.minutes=10080 (7 days) expires a group’s
committed offsets once the group has been empty that long
(broker configs). A member that comes back after
that has no bookmark, and auto.offset.reset decides again: with latest, everything produced while it was away
is skipped.
Go deeper: making a repeat harmless
At-least-once plus an effect that can be repeated safely gives the result of exactly-once:
- Idempotent writes: key the result by partition and offset (or by an order id), so writing it again overwrites instead of adding. The file in the runs above could be deduplicated that way.
- Bookmark and result together: store the offset in the same database transaction as the result, and on
onPartitionsAssignedseek()to it instead of using Kafka’s commit. - Kafka to Kafka: a transactional producer can commit the consumer’s offsets in the same transaction as the
records it writes (
sendOffsetsToTransaction, KIP-98).
None of the three is measured in this course.
Common misunderstandings
- “commitSync() gives exactly-once.” It is still after the work: a crash repeats the batch in progress.
- “Auto-commit loses messages.” Not when every record is processed inside the poll loop (0 missing in the
three runs killed after a commit). It loses them when work leaves the loop, or when there is no commit yet and
latestapplies. - “
earliestmakes my group re-read the topic.” Only a group, or partition, with no commit.
Check yourself
1. A member auto-commits every 5 s and processes 100 orders a second. It is killed. About how many orders can be processed twice?
Up to about 500, the work of one 5 s interval: the commit can be up to 5 s behind. The runs above repeated 7 to 380.2. Sending a payment twice is far worse than missing one, which a nightly job catches. When should the payment consumer commit?
Before sending: at-most-once. A crash then skips the payments in progress (2 to 30 in the runs above) and never repeats one.3. A new group is deployed with the defaults, crashes 2 s after it starts, and comes back a minute later. What did it miss?
Probably everything produced in that minute: with no commit yet,auto.offset.reset=latest starts it
at the end of each partition. The runs above lost about 1,780 orders each that way. Set earliest for a group that
must see everything from its first day.
Next: 4 · Consumer groups in production: how the group decides a member is gone, and how to read lag.