How does Kafka store offsets for each topic?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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:
- Highly Replicated: Kafka ensures high availability and durability of consumer offset data by defaulting the replication factor of the
__consumer_offsetstopic to be large (default is 3). - 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.
- Partitioned: To ensure scalability and manage the load, the
__consumer_offsetstopic 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
| Feature | Detail |
| Storage Medium | Internal Kafka topic (__consumer_offsets) |
| Configuration | Highly replicated, log-compacted, partitioned |
| Commit Types | Automatic and manual |
| Scalability | Managed 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.
Related reading
- How does kafka streams compute watermarks?
- How does Kafka Streams work with Partitions that contain incomplete Data?
- how does kafka synchronize data timestamps between different brokers and how is timestamp created in kafka
- How does Kinesis achieve Kafka style Consumer Groups?
- How does leaderless replication actually work ? Is there really no single co-ordinator / leader node which maintains the replication?
- How does 'LOAD DATA INFILE' work in statement-based replication?
- How does max.poll.records affect the consumer poll
- How does one Kafka consumer read from more than one partition?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.