In this lesson · topic 6 of 7
Topic 6
Where the commit goes decides the guarantee
7 min read
The ack problem on a log
On a queue, the ack deletes the message. On a log, nothing is deleted, and the equivalent is the committed offset: the position the group will resume from. Where in the consumer’s loop that commit happens decides what a crash does, exactly as in the delivery guarantees lesson.
There are three common placements.
Commit before processing. Read a batch, commit its offset, then process. A crash after the commit and before the work loses the batch, because the new owner starts past it. This is at-most-once.
Commit after processing. Read a batch, process it, then commit. A crash after the work and before the commit means the new owner re-reads the batch. This is at-least-once, and the usual choice.
Auto-commit. The client commits on a timer without being asked. In the Java Kafka consumer, enable.auto.commit is on by default, with a 5 second interval. Commits happen inside the poll call and cover whatever the last poll returned. If your processing of that batch is still running, or has failed, a commit can still cover it, so you can lose messages. Many teams switch it off and commit explicitly after processing.
Predict first
The price of committing often
A commit is a write to the broker. Commit after every message and a consumer finishing 2,000 messages a second sends 2,000 commits a second. Commit once per batch of 500 and it sends 4. The cost shows up as extra load on the coordinator and as extra latency in the loop.
The other side of the choice is how much repeats after a crash. With batches of 500, a crash repeats up to 500 messages. With a commit per message, at most one. A short commit interval trades broker load for a smaller replay.
This is a tuning choice with a measurable answer. If the effect is idempotent and cheap, commit less often. If a repeat is costly, such as sending a message to a customer, commit more often, and also make the effect idempotent.
Slow processing looks like death
A consumer that takes too long between polls is assumed to have failed. In the Java client, max.poll.interval.ms defaults to five minutes. A batch that takes longer, perhaps because each message calls a slow API, makes the group evict the consumer and rebalance. Its partitions go to someone else, who re-reads from the last commit, and the slow batch is processed twice. The original consumer then fails to commit.
Fixes are a smaller batch per poll, a longer interval, or moving heavy work off the polling thread. All of them keep the poll-to-poll gap well below the limit.
Catching up after a restart
Lag is the size of the gap. Suppose a partition has 90,000 messages of lag after a deploy. Producers write 400 a second to it, and the consumer processes 1,000 a second. The lag shrinks by 600 a second, so the catch-up takes 90,000 / 600 = 150 seconds. If the consumer only did 450 a second, it would shrink by 50 a second and need 1,800 seconds. The same lesson as for queues applies: recovery depends on the spare capacity.
Sources
- Apache Kafka documentation, Consumer configs, for enable.auto.commit, auto.commit.interval.ms and max.poll.interval.ms.
- Apache Kafka documentation, Message delivery semantics.
- Kreps, Narkhede and Rao, Kafka: a distributed messaging system for log processing, NetDB, 2011.