Following is the stepwise workflow of the Pub-Sub Messaging:

  • Producers publish messages on a topic.
  • Kafka broker stores messages in the partitions configured for that particular topic. If the producer did not specify the partition in which the message should be stored, the broker ensures that the messages are equally shared between partitions. If the producer sends two messages and there are two partitions, Kafka will store one message in the first partition and the second message in the second partition.
  • Consumer subscribes to a specific topic.
  • Once the consumer subscribes to a topic, Kafka will provide the current offset of the topic to the consumer and also saves that offset in the ZooKeeper.
  • Consumer will request Kafka at regular intervals for new messages.
  • Once Kafka receives the messages from producers, it forwards these messages to the consumer.
  • Consumer will receive the message and process it.
  • Once the messages are processed, the consumer will send an acknowledgement to the Kafka broker.
  • Upon receiving the acknowledgement, Kafka increments the offset and updates it in the ZooKeeper. Since offsets are maintained in the ZooKeeper, the consumer can read the next message correctly, even during broker outages.
  • The above flow will repeat until the consumer stops sending the request.
  • Consumers can rewind/skip to the desired offset of a topic at any time and read all the subsequent messages.