Why is Kakfa called pub-sub and can we read randomly from an offset in Kafka
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 event streaming platform that has gained immense popularity for its high-throughput, fault-tolerance, and low-latency capabilities. It is often referred to as a "pub-sub" (publish-subscribe) system, though it has unique characteristics that differentiate it from traditional pub-sub models. Additionally, Kafka provides flexible consumption options, including the capability to read messages from a specified offset.
Why Kafka is Called Pub-Sub
A publish-subscribe system is a messaging paradigm where messages are published to a "topic" by the producer without being directed specifically to a receiver. Subscribers can then register to receive messages from topics of interest. Kafka embodies this model with several enhancements:
- Producers and Consumers: In Kafka, producers publish data to topics. Consumers subscribe to topics and process the streams of data that come through. This separation of concerns and decoupling of producers from consumers enables high scalability and fault tolerance.
- Topics and Partitions: Kafka topics are divided into multiple partitions. This allows for the data within a topic to be split across different brokers in the Kafka cluster, facilitating load balancing and parallel processing.
- Persistence: Unlike many traditional messaging systems where messages disappear once consumed, Kafka retains all messages for a set amount of time or until a specified size threshold is reached. This allows multiple consumers to read the same data independently and at their own pace.
Random Read Capability: Reading from an Offset
Kafka’s architecture allows consumers to read messages from a specific offset. An offset is a unique identifier for each record within a partition. This capability is crucial for many use cases such as system crashes where consumers need to resume reading from where they last left off, rather than reprocessing all messages.
How Offset Management Works in Kafka
- Offset Storage: Consumer offsets are stored in Kafka's internal
__consumer_offsetstopic. Each consumer group tracks its own offset per partition. - Seek Operations: Consumers in Kafka have the ability to "seek" to a specific offset in a partition. This can be done manually in the consumer application, allowing for random access reading. Here’s a basic example in Java:
Implications of Random Access
Being able to jump to specific offsets allows Kafka to support various advanced use cases, including:
- Event Replaying: Systems can reprocess events, e.g., for debugging or running analytics on historical data.
- Log Compaction: In log compacted topics, Kafka ensures that the partition only holds the last message produced for each key. This allows consumers to rewind back to the earliest offset and get a full snapshot of the latest values by key.
Summary Table
| Feature | Description |
| Pub-Sub Model | Producers publish to topics; consumers subscribe. |
| Topics & Partitions | Data is split across various partitions for scalability. |
| Persistence | Messages are stored even after being consumed. |
| Random Access | Consumers can read from any offset in a partition. |
| Use Cases | Suitable for streaming, event sourcing, logging, and more. |
Conclusion
Kafka’s robust design incorporates the strengths of traditional pub-sub systems while introducing capabilities like persistent storage and random access through offsets, which enhance its utility for a broader range of applications. Its architecture not only supports high-performance message publishing and consuming but also accommodates complex data processing workflows, making it an invaluable tool in modern data architectures.
Related reading
- Why is queue visibility timeout is recommended to be six times function timeout plus batch window?
- Why is RabbitMQ not persisting messages on a durable queue?
- Why is the kafka consumer consuming the same message hundreds of times?
- Why is the Kafka distributed connector dying when the node I created it on is killed?
- Why is location transparency called location transparency?
- Why is multi-paxos called multi-paxos?
- Why is the topics argument of KafkaUtils.createStream() a Map rather then array?
- Why Kafka consumer performance is slow?

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.