Kafka
Offset Storage
Distributed Systems
Data Management
Topic Partitioning

How does Kafka store offsets for each topic?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed streaming platform capable of handling high volumes of data and enabling capabilities to process streams of data in real time. An integral part of Kafka's functionality and performance lies in how it manages the storage of consumer offsets for each topic. Consumer offsets are critical as they indicate up to which message a consumer group has consumed in each partition of a topic.

Understanding Kafka Offsets

In Kafka, messages within a partition of a topic are identified and ordered by an offset (an incremental ID). The offset allows consumers to keep track of which messages have been consumed by storing the last offset they have processed. After a consumer in a group has processed messages up to a particular offset, it commits this offset to Kafka. By doing this, the consumer can resume from the point right after the last processed message in case of a failure or restart, hence maintaining message delivery semantics.

Storage of Offsets in Kafka

Originally, Kafka stored consumer offsets in ZooKeeper. However, since version 0.9, Kafka has been using an internal Kafka topic for this purpose, named __consumer_offsets.

__consumer_offsets Topic

This internal topic is not just a regular Kafka topic. Instead, it has specific configurations and functionalities:

  1. Highly Replicated: Kafka ensures high availability and durability of consumer offset data by defaulting the replication factor of the __consumer_offsets topic to be large (default is 3).
  2. Compact Topic: The topic uses Kafka's log compaction feature, which means only the latest offset for each consumer group and partition is retained. This cleans up older offsets automatically and prevents the topic from growing indefinitely.
  3. Partitioned: To ensure scalability and manage the load, the __consumer_offsets topic is partitioned. The offsets for a consumer group are distributed across these partitions based on a hash of the consumer group ID.

How Offsets are Committed

Consumers can commit offsets in Kafka either automatically or manually:

  • Automatic Committing: Enabled by default, where offsets are committed automatically at a configurable interval (auto.commit.interval.ms).
  • Manual Committing: Provides better control to commit an offset after an event is securely processed by the consumer which then avoids data losses or duplications that can occur due to automatic commits.

The actual process involves encoding consumer group, topic, partition, and the offset data into the message keys and values stored in the __consumer_offsets topic.

Table: Kafka Offsets Storage Key Points

FeatureDetail
Storage MediumInternal Kafka topic (__consumer_offsets)
ConfigurationHighly replicated, log-compacted, partitioned
Commit TypesAutomatic and manual
ScalabilityManaged through partitioning of offset data by consumer group ID

Monitoring and Managing Offsets

Admins and users can monitor and manage consumer offsets through Kafka's command-line tools like kafka-consumer-groups.sh. This tool allows viewing the committed offsets of all consumer groups, checking lag (the difference between the latest produced message and last committed offset), and even resetting offsets to particular timestamps or offsets.

Security

Since offset data contains sensitive consumer progress information, access to the __consumer_offsets topic is tightly controlled, and permissions can be managed using Kafka’s security features like ACLs.

Conclusion

The method of storing offsets in Kafka using an internal, highly replicated, and compacted topic, offers durability, scalability, and performance for managing offsets. This intelligent approach empowers Kafka to not only track consumer progress reliably across restarts and failures but also optimize its internal workflows for handling massive amounts of data in consumer groups.

This framework is crucial for maintaining consistent and resilient data processing pipelines, making Kafka an ideal choice for robust large-scale event streaming applications.


Course illustration
Course illustration

All Rights Reserved.