Skip to content

Offsets, commits and replay

A consumer reads a partition from left to right. Where it has got to is its position: the offset of the next message it will read, counting messages from 0. Kafka’s documentation describes a consumer’s progress through a partition as that single integer, and says that keeping track of what has been consumed is one of the key performance points of a messaging system.

The position lives in the consumer, and a consumer can crash. So the group saves it from time to time, which is a commit: the committed offset of a partition is where a new owner of that partition will start. Between two commits the position runs ahead of the committed offset, and the difference is work that was done but that nobody has recorded.

The lab’s table shows the three numbers for each partition: Read up to (the position), Committed, and Log end. A partition’s lag is the end minus the position.

When a consumer crashes, its partitions go to the others, and the new owner starts from the committed offset, not from the crashed consumer’s position. Everything between the two is read a second time. This is the at-least-once of the last lesson, built from offsets instead of acknowledgements: the commit is the acknowledgement, given for a batch, and the batch since the last commit is the work that can be repeated. Kafka’s documentation draws the same line: save the position after processing and a crash causes reprocessing, save it before and a crash causes loss.

A consumer that leaves cleanly does not cost anything. It commits first, so the next owner starts exactly where it stopped. Either way the group has to hand the partitions over, and in the lab the whole group pauses for 2 s while it does.

Mental model. The committed offset is a bookmark that is updated every few seconds. A crash sends the next reader back to the bookmark, and everything after the bookmark is read again. The further apart the bookmarks, the more is repeated.

The lab has 4 partitions and a group of 2 consumers, with 8 messages a second in and 8 a second out. Commit offsets every 8 s. Press Crash a consumer and read the sentence at the top of the status.

The group commits its offsets from time to time. Crash a consumer and see how many messages are read again from the last commit.

Topic and group
Partitions
4

Changing the number of partitions starts the topic again, because it changes where every key goes.

8 per second
0%

The other messages carry any of 64 keys with equal chance.

Consumers
Consumers
2
8 per second
8 s

A consumer that crashes loses what it processed since the last commit: the new owner starts from there.

Time controls
Time: 10 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Consumer lag over time
035110 seconds

Total lag Furthest behind

Partitions of the topic, after 10 s
Partition numberRead byLog endRead up toCommittedLagShare of messages
Partition 0Consumer 1171714023%
Partition 1Consumer 1272720027%
Partition 2Consumer 2212118027%
Partition 3Consumer 2151512023%

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.

  1. Partition 0, offset 14
  2. Partition 0, offset 15
  3. Partition 0, offset 16
  4. Partition 1, offset 26
  5. Partition 2, offset 19
  6. Partition 3, offset 13
  7. Partition 2, offset 20
  8. 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.

At 10 s, the last commit was at 8 s, so 2 s of work have not been committed. The crash takes the last consumer, Consumer 2, which owns two partitions, and the status says that messages processed since the last commit that will be read again: 6.

Press Reset and this time press Step 1 s five times before the crash, so that it happens at 15 s, a second before the next commit at 16 s: the count is 22. Press Reset again and step six times, to 16 s, right after the commit, and it is 0. The number repeated depends on when in the cycle the crash falls, between nothing and about a commit interval of that consumer’s work.

Now reset and set Commit offsets every to a smaller value, then step 5 s before the crash. With 8 s it is 22 messages, with 4 s it is 12, with 2 s it is 6, and with 1 s it is 0: a commit every second leaves nothing uncommitted when the crash comes. Committing more often shrinks the replay.

Finally press Reset and Remove a consumer instead of crashing it. The group commits first, and the status says that no message is read twice.

What happens if you commit after every message?

Section titled “What happens if you commit after every message?”

Then a crash repeats at most the message in progress, and the replay is nearly nothing. It is not free: a commit is a write, and in a real system a write to the broker takes time, so committing after every message slows the consumers down. The lab charges nothing for a commit, so it cannot show the price. This is the usual shape of the trade-off: frequent commits lower the repeated work and raise the overhead, and the right interval depends on how expensive a repeat is and how fast the consumers must go. If a repeat is made harmless, the interval can be long.

Replay is also a tool. Because the log keeps its messages and the position is a number, a reader that moves its position back reads the same messages again, in the same order, which is what a crash does by accident. How long a real broker keeps its messages is a setting of the broker, and the lab does not model retention.

  • A partition has a position (where the consumer is) and a committed offset (where a new owner would start). The commit is the bookmark.
  • A crash sends the next owner back to the last commit: what was processed since is read again, which is at-least-once for a log.
  • In the lab, a crash at 10 s repeats 6 messages, at 15 s it repeats 22, and right after a commit it repeats 0. A clean departure repeats nothing.
  • A shorter commit interval shrinks the replay: 22 messages with 8 s, 12 with 4 s, 6 with 2 s, 0 with 1 s. The price is more commits, which the lab does not charge.
  • A consumer that is safe to repeat can use a long interval. One that is not must commit with care, or deduplicate.

Next: recap and quiz, the module on one page.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.