apache-kafkakafka-consumer-apikafka-producer-api

Understanding Kafka Topics and Partitions


I am starting to learn Kafka. During my readings, some questions came to my mind:

  1. When a producer is producing a message, it will specify the topic it wants to send the message to. Is that right? Does it care about partitions?

  2. When a subscriber is running, does it specify its group id so that it can be part of a cluster of consumers of the same topic or several topics that this group of consumers is interested in?

  3. Does each consumer group have a corresponding partition on the broker or does each consumer have one?

  4. Are the partitions created by the broker, and therefore not a concern for the consumers?

  5. Since this is a queue with an offset for each partition, is it the responsibility of the consumer to specify which messages it wants to read? Does it need to save its state?

  6. What happens when a message is deleted from the queue? - For example, the retention was for 3 hours, then the time passes, how is the offset being handled on both sides?


Solution

  • This post already has answers, but I am adding my view with a few pictures from Kafka Definitive Guide

    Before answering the questions, let's look at an overview of producer components:

    Overview of producer components


    1. When a producer is producing a message, it will specify the topic it wants to send the message to. Is that right? Does it care about partitions?

    The producer will decide target partition to place any message, depending on:


    1. When a subscriber is running - Does it specify its group id so that it can be part of a cluster of consumers of the same topic or several topics that this group of consumers is interested in?

    You should always configure group.id unless you are using the simple assignment API and you don’t need to store offsets in Kafka. It will not be a part of any group. Source.


    1. Does each consumer group have a corresponding partition on the broker or does each consumer have one?

    In one consumer group, each partition will be processed by one consumer only. These are the possible scenarios


    1. As the partitions created by the broker, therefore not a concern for the consumers?

    Consumer should be aware of the number of partitions, as was discussed in question 3.


    1. Since this is a queue with an offset for each partition, is it the consumer's responsibility to specify which messages it wants to read? Does it need to save its state?

    Kafka (to be specific Group Coordinator) takes care of the offset state by producing a message to an internal __consumer_offsets topic, this behavior can be configurable to manual as well by setting enable.auto.commit to false. In that case consumer.commitSync() and consumer.commitAsync() can help manage offset.

    More about Group Coordinator:

    1. It's one of the elected brokers in the cluster from Kafka server side.
    2. Consumers interact with the Group Coordinator for offset commits and fetch requests.
    3. Consumer sends periodic heartbeats to Group Coordinator.

    1. What happens when a message is deleted from the queue? - For example, The retention was for 3 hours, then the time passes, how is the offset being handled on both sides?

    If any consumer starts after the retention period, messages will be consumed as per auto.offset.reset configuration which could be latest/earliest. Technically, it's latest (start processing new messages), because all the messages got expired by that time and retention is a topic-level configuration.