Kafka
Consumer API
OffsetCommit
High-Level Consumer
API Request

Kafka offsetcommit request with high level consumer API

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular distributed streaming platform that is extensively employed for building real-time streaming data pipelines and applications. Kafka facilitates the publishing and subscribing of streams of records. In a Kafka cluster, messaging is partitioned and spread over multiple nodes to ensure fault tolerance and horizontal scalability. One of the key components in the Kafka ecosystem involves managing the offset, which is a way to track the progress of a consumer in reading messages from a partition.

Understanding Offsets

In Kafka, an offset is a unique identifier for each record in a partition. It denotes the position of a consumer in the partition. Whenever a consumer reads a message, it advances its offset. Proper management of these offsets ensures that a consumer can resume reading from where it left off in case of any failures or restarts.

The High-Level Consumer API

The High-Level Consumer API abstracts a lot of the complex details involved in managing partitions and offsets. Users can focus on specifying the topics, the group of consumers, and how the records need to be processed. The API is responsible for maintaining the proper offset in the background.

OffsetCommitRequest in Kafka

The OffsetCommitRequest is a crucial part of the Kafka Consumer API. It's the formal way a consumer indicates to Kafka that it has completed processing messages up to a certain point, and Kafka can consider those messages as consumed (i.e., commit the offset). Typically, this operation can either be automated within the high-level API, or explicitly managed by the developer for finer control.

Usage

The high-level consumer uses either an automatic periodic commit based on a timer (auto.commit.interval.ms) or manual committing using the commitSync() and commitAsync() methods from the consumer object. The key distinction is:

  • Automatic Committing: With this setting, the consumer offsets are committed automatically at specified intervals to Kafka.
  • Manual Committing: Provides the developer more control over when offsets should be committed. This is particularly useful in ensuring that messages are processed and successfully committed only when certain conditions are met.

Technical Details

Under the hood, OffsetCommitRequest keeps track of which messages have been consumed by storing the offset of the next message to be read. Here's a breakdown of how these requests are managed:

  1. Consumer Group: Each consumer in a group reads from exclusive partitions of the topic. Kafka keeps track of the last offset read by each member of the consumer group.
  2. Session Management: Kafka also recognizes when a new consumer joins a group or when an existing member leaves. It redistributes the partitions accordingly and ensures that the new consumer starts reading from the correct offset.

Example of Manual Offset Management

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test");
4props.put("enable.auto.commit", "false");  // Disabling auto-commit
5props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
6props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7
8try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
9    consumer.subscribe(Arrays.asList("my-topic"));
10    final int minBatchSize = 200;
11    List<ConsumerRecord<String, String>> buffer = new ArrayList<>();
12
13    while (true) {
14        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
15        for (ConsumerRecord<String, String> record : records) {
16            buffer.add(record);
17        }
18        if (buffer.size() >= minBatchSize) {
19            // Process the buffered records here...
20            
21            consumer.commitSync();  // Manually commit the offsets
22            buffer.clear();
23        }
24    }
25}

Summary Table

FeatureDescription
Offset ManagementKeeps track of read/messages to ensure data processing continuity.
OffsetCommitRequestUsed by consumers to report position in stream to Kafka, can be automated or manual.
Automatic CommittingOffsets are committed at configurable intervals. Useful for typical use-cases.
Manual CommittingOffers greater control, allowing commitments to be made post-conditions verification.

Conclusion

Properly managing offsets through OffsetCommitRequest in Kafka ensures reliable message consumption in distributed systems, providing flexibility depending on the application’s need for manual control versus convenience. By understanding and utilizing this mechanism effectively, developers can build robust systems that maintain data integrity and consistency, even in the face of failures.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.