What do we mean by 'commit' data in Kafka broker?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the context of Apache Kafka, the term 'commit' refers primarily to the process of marking a specific message or a set of messages as processed within a consumer group, designating the consumer's position in a Kafka topic log as up-to-date. This ensures that each message is processed only once, even in the event of a failure, and is fundamental to Kafka’s ability to provide fault-tolerant message consumption.
Understanding Kafka's Message Committing
Kafka maintains a distributed log consisting of records (messages) that are appended sequentially. Consumers read from this log at their own pace. The position from which a consumer will begin reading after a restart or failure is controlled through offsets. An offset is a unique identifier for each record within a partition and signifies a specific location in the partition.
When a consumer reads messages from a partition, it needs a way to track which messages have been processed. This is managed by committing offsets. When a consumer processes messages from a partition, it can commit the offsets of messages to Kafka, which essentially means it is informing Kafka that it has successfully processed all preceding messages. As a result, in the event of a consumer restart or failure, Kafka will only deliver messages from the last committed offset, ensuring no message is processed more than once.
How Committing Works
Kafka consumers can commit offsets in two modes:
- Automatic Offset Committing: This uses the
enable.auto.commitsetting in the consumer configuration, which is set totrueby default. The interval at which offsets are committed can be controlled using theauto.commit.interval.mssetting. - Manual Offset Committing: This gives the programmer explicit control over when offsets are committed, typically after the consumer has finished processing messages. There are two common ways to manually commit offsets:
- Synchronously: where commit operations will block the thread until the broker acknowledges it, ensuring more precise control over offset commits but introducing delays in consumption.
- Asynchronously: which does not block the consumer while offsets are committed. If a commit fails, there's a callback mechanism to handle this situation.
Example
Consider a consumer application subscribed to a Kafka topic with partitions. The application is set up with automatic committing of offsets:
Key Points Table
| Term | Description |
| Offset | A sequential id (index) given to each record within a partition. |
| Commit | The act of updating the consumer's current position in the log during or after consumption. |
| Automatic Commit | Kafka daemon thread automatically commits offsets at specified intervals. |
| Manual Commit | The application explicitly calls commit; can be synchronous or asynchronous. |
| enable.auto.commit | Property determining whether committing is managed automatically by Kafka. |
Conclusion
In summary, committing in the context of a Kafka broker is crucial for ensuring message processing is reliable and fault-tolerant across consumer restarts and failures. By effectively managing offsets through committing, Kafka supports exactly-once and at-least-once processing semantics, which are vital for building robust streaming applications. Whether manually or automatically managed, understanding and implementing proper offset commits is fundamental for any application using Kafka for message consumption.

