Kafka
Pub-Sub Model
Data Streaming
Kafka Offsets
Distributed Systems

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.

Practice system design

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:

  1. 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.
  2. 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.
  3. 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_offsets topic. 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:
java
    consumer.seek(new TopicPartition("mytopic", 0), 100); // Seeking to offset 100 in partition 0 of "mytopic"

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

FeatureDescription
Pub-Sub ModelProducers publish to topics; consumers subscribe.
Topics & PartitionsData is split across various partitions for scalability.
PersistenceMessages are stored even after being consumed.
Random AccessConsumers can read from any offset in a partition.
Use CasesSuitable 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
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