Offsets, replay and what exactly-once really means
The bookmark is a number
Section titled “The bookmark is a number”A consumer group’s place in a partition is an offset: Kafka’s design document says the broker keeps a single integer for each partition, the offset of the next message to consume. Saving it is a commit. If a consumer dies, the partition passes to another one in the group, which starts from the last committed offset. Because the log keeps its messages, a consumer can also be sent back to an earlier offset on purpose, to rebuild a result after a bug.
The consumer’s position and the committed offset are not the same thing. The consumer moves its position forward with every message it reads. The commit happens from time to time, and only the commit survives a crash. The distance between the two is work that was done and that nobody wrote down.
Mental model. The consumer reads with a finger and keeps a bookmark in the notebook. The finger moves after every line. The bookmark moves only when the consumer stops to place it. If the reader faints, the next reader starts at the bookmark, and rereads every line the finger had passed since.
Try it: crash a consumer
Section titled “Try it: crash a consumer”The group commits its offsets from time to time. Crash a consumer and see how many messages are read again from the last commit.
What is happening
Producers send 8 a second and the 2 consumers at work can process 16, so the lag does not grow overall.
- Consumer lag
- 00 on Partition 0, the furthest behind
- Messages processed
- 8080 different messages, 0 read again
- Consumers at work
- 2 of 2each partition is read by one consumer of the group
- Rebalances
- 0each pauses the whole group for 2 s
Total lag Furthest behind
| Partition number | Read by | Log end | Read up to | Committed | Lag | Share of messages |
|---|---|---|---|---|---|---|
| Partition 0 | Consumer 1 | 17 | 17 | 14 | 0 | 23% |
| Partition 1 | Consumer 1 | 27 | 27 | 20 | 0 | 27% |
| Partition 2 | Consumer 2 | 21 | 21 | 18 | 0 | 27% |
| Partition 3 | Consumer 2 | 15 | 15 | 12 | 0 | 23% |
Offsets count messages from 0: "Read up to" is the offset the consumer reads next, and "Committed" is where a new owner would start.
Last messages processed
Within a partition the offsets only go up. Between partitions there is no order at all.
- Partition 0, offset 14
- Partition 0, offset 15
- Partition 0, offset 16
- Partition 1, offset 26
- Partition 2, offset 19
- Partition 3, offset 13
- Partition 2, offset 20
- Partition 3, offset 14
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
- A message goes to the partition its key hashes to: FNV-1a of the key text mod the number of partitions. There are 64 keys, and the hot key is key-0. The hash is a choice of the lab, not what any particular broker uses.
Simplifications
- Partitions are shared out in consecutive ranges, and a change of the group pauses all consumers for two seconds, as an eager rebalance does. A crashed consumer loses what it processed since the last commit; the commit interval is a setting.
- Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
The lab has 4 partitions, 2 consumers and 8 messages a second in, and the group commits every 8 seconds. It opens at 10 seconds, so the last commit was at 8 seconds. Press Crash a consumer at once. The panel reads: Consumer 2 crashed, the group pauses for 2 s to rebalance, and the messages processed since the last commit that will be read again: 6. Consumer 1 now owns all four partitions, and the 6 messages are processed for the second time. Press Skip 30 s and Messages processed reads 304, of which 298 are different messages, and 6 read again.
The number 6 is not a constant. Press Reset, press Step 1 s once, then Crash a consumer: 9 messages. Press Reset, press Step 1 s five times, then Crash a consumer: 22. The longer since the last commit, the more the crashed consumer had processed that was never recorded, and the more the new owner reads again. Commits at 8 seconds and at 16 seconds bracket those runs. Committing more often shrinks the replay, and costs more commits, but it does not remove it, because a crash can still come between the work and the commit.
Before or after the work
Section titled “Before or after the work”The order of the commit and of the work sets what a crash costs. Kafka’s design document describes both. If a consumer saves its position first and then processes, a crash during processing means the next owner starts after the messages that were never processed, and they are skipped: at-most-once. If it processes first and then saves its position, a crash after processing and before saving means the next owner repeats the messages: at-least-once. The lab commits after the work, so it is at-least-once, and the replayed 6 are the price: nothing is lost, and some messages are seen twice.
What exactly-once really promises
Section titled “What exactly-once really promises”Kafka’s documentation defines exactly-once as each message being processed once and only once, and it says how far that reaches. Within Kafka, transactional producers write the output records and the consumer offsets together, in one atomic step, and consumers can be set to read only committed records. For an output in some other system, the document says the way to get it is to store the offsets with the output, in the same place, so that either the data and the offset are both updated or neither is.
Read it carefully, because there is a boundary in it. Messages may still be read more than once, as the lab shows. What happens once is the effect, inside a boundary where the output and the offset change together. An email that the job sends, a call to another service, or a write to a store that cannot share a transaction with the offset is outside it, and a replay will repeat it.
There are two ways to make an effect happen once when messages repeat:
- Commit the offset with the output. If the results go to a database, write the result and the new offset in one transaction, and start from the stored offset. A crash then cannot leave a result without its offset or the reverse.
- Make the write idempotent. Give each result a key that does not change when the message is read again, such as the partition and the offset, and let the store ignore a key it already has. In the lab run above, 304 messages were processed and 298 were different: with a key on the message, the store ends with 298 rows, whatever the replay did. This is the subject of the idempotency module.
The first needs a store that can hold the offset next to the result. The second needs a stable key and a store that can refuse a repeat.
Check your understanding: replay and exactly-once
This quiz needs JavaScript to check your answers.
What happens if the output is a payment?
Section titled “What happens if the output is a payment?”Say each message asks for a charge. The crash in the lab replays 6 messages, and a handler that charges for every message it reads charges 6 payments twice. A faster commit does not fix it: even a commit after every message leaves a window between the charge and the commit. The handler has to make the charge idempotent, for example by sending the payment provider a key made from the message’s partition and offset, so that the second request with the same key is recognised as a repeat. The idempotency module is about doing exactly that. The broker’s guarantee cannot reach the payment provider, and the key can.
- An offset is a position in a partition. A commit saves it, and only the commit survives a crash, so a new owner starts from the last commit.
- In the lab, a crash at 10 seconds replays 6 messages, after one more second 9, and after five more seconds 22: the longer since the last commit, the more is read again.
- Committing after the work is at-least-once: nothing is lost and some messages are seen twice. Committing before the work is at-most-once: nothing is repeated and some are skipped.
- Exactly-once processing means the effect happens once inside a boundary where the output and the offset change together. Messages are still read more than once, and effects outside the boundary are repeated.
- To make an effect happen once, write the result and the offset in one transaction, or make the write idempotent with a stable key. In the lab run, 304 messages were processed and 298 were different.
Next: change data capture.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.