Search code examples
apache-kafkakafka-consumer-apikafka-producer-api

How ack works for pub/sub in Kafka?


Using publish/subscribe how message is confirmed?

When message is sent to some consumers in unique group. Does it mean message was confirmed if all consumers confirms or message is confirmed for all consumers?


Solution

  • Producer

    On the Producer side, you have the option to wait for an acknowledgment from the broker that the message is successfully stored in the topic. You can use the Producer configuration called acks and set it to the value 1 or all:

    acks=1: This will mean the leader will write the record to its local log but will respond without awaiting full acknowledgement from all followers. In this case should the leader fail immediately after acknowledging the record but before the followers have replicated it then the record will be lost.

    acks=all: This means the leader will wait for the full set of in-sync replicas to acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica remains alive. This is the strongest available guarantee. This is equivalent to the acks=-1 setting.

    Consumer

    On the Consumer side, you have the concept of a Consumer Group which are committing offsets back to the broker confirming which messages the consumer as already processed. Each Consumer Group will commit its own offset confirming that it consumed them. This is independent to other Consumer groups.

    If you have two Consumer Groups consuming the same topic, each Consumer Group will independently commit its offsets based on its individual configuration.

    By default, the Consumers are committing the offsets back to Kafka automatically through the Consumer configuration enable.auto.commit and auto.commit.interval.ms which defaults to 5 seconds. Or you can manually commit the offsets based on your processing logic within the consumer.