Custom Committers

In auto commit mode, the consumer stores the offset of each message after it is processed, and librdkafka commits the stored offsets in the background. Committers are used when handlers commit offsets themselves, by calling the commit or commitAsync methods of the consumer, usually in manual commit mode.

Sponsorship

Support Laravel Kafka by sponsoring me!

Laravel Kafka is free and Open Source software, built to empower developers like you. Your support helps maintain and enhance the project. If you find it valuable, please consider sponsoring me on GitHub. Every contribution makes a difference and keeps the development going strong! Thank you!

Become a Sponsor Want to hide this message? Sponsor at any tier of $10/month or more!

The Junges\Kafka\Contracts\Committer interface has two methods:

  • commit(ConsumerMessage|Message|array|null $messageOrOffsets = null): void, used for synchronous commits.
  • commitAsync(ConsumerMessage|Message|array|null $messageOrOffsets = null): void, used for asynchronous commits.

Both receive what the handler passed to the consumer: nothing, to commit the offsets of the current assignment, a Junges\Kafka\Contracts\ConsumerMessage or RdKafka\Message, or an array of RdKafka\TopicPartition.

The following committer retries synchronous commits while the consumer group is rebalancing:

use Junges\Kafka\Contracts\Committer;
use Junges\Kafka\Contracts\ConsumerMessage;
use RdKafka\Exception;
use RdKafka\KafkaConsumer;
use RdKafka\Message;
use RdKafka\TopicPartition;

class RetryingCommitter implements Committer
{
    public function __construct(private KafkaConsumer $consumer) {}

    public function commit(ConsumerMessage|Message|array|null $messageOrOffsets = null): void
    {
        retry(3, fn () => $this->consumer->commit($this->offsets($messageOrOffsets)), 100, function (Exception $exception) {
            return $exception->getCode() === RD_KAFKA_RESP_ERR_REBALANCE_IN_PROGRESS;
        });
    }

    public function commitAsync(ConsumerMessage|Message|array|null $messageOrOffsets = null): void
    {
        $this->consumer->commitAsync($this->offsets($messageOrOffsets));
    }

    private function offsets(mixed $messageOrOffsets): mixed
    {
        if (! $messageOrOffsets instanceof ConsumerMessage) {
            return $messageOrOffsets;
        }

        return [new TopicPartition(
            $messageOrOffsets->getTopicName(),
            $messageOrOffsets->getPartition(),
            $messageOrOffsets->getOffset() + 1
        )];
    }
}

To use it, create a committer factory, which is a class that implements the Junges\Kafka\Contracts\CommitterFactory interface, and pass it to the consumer:

use Junges\Kafka\Config\Config;
use Junges\Kafka\Contracts\Committer;
use Junges\Kafka\Contracts\CommitterFactory;
use Junges\Kafka\Facades\Kafka;
use RdKafka\KafkaConsumer;

class RetryingCommitterFactory implements CommitterFactory
{
    public function make(KafkaConsumer $kafkaConsumer, Config $config): Committer
    {
        return new RetryingCommitter($kafkaConsumer);
    }
}

$consumer = Kafka::consumer(['orders'])
    ->withManualCommit()
    ->usingCommitterFactory(new RetryingCommitterFactory)
    ->withHandler(function ($message, $consumer) {
        // ...
        $consumer->commit($message);
    })
    ->build();