spring-bootspring-webfluxspring-kafkareactor-kafka

Polling behavior when using ReactiveKafkaConsumerTemplate


I have a Spring Boot application using ReactiveKafkaConsumerTemplate for consuming messages from Kafka.

I've consume messages using kafkaConsumerTemplate.receive() therefore I'm manually acknowledging each message. Since I'm working in an asynchronous manner, messages are not processed sequentially.

I'm wondering how does the commit and poll process work in this scenario - If I polled 100 messages but acknowledged only 99 of them (message not acknowledged is in the middle of the 100 messages I polled, say number 50), what happens on the next poll operation? Will it actually poll only after all 100 messages are acknowledged (and offset is committed) and until then I'll keep getting the un-acknowledged messages over and over to my app until I acknowledge it?


Solution

  • Kafka maintains 2 offsets for a consumer group/partition - the current position() and the committed offset. When a consumer starts, the position is set to the last committed offset.

    Position is updated after each poll, so the next poll will never return the same record, regardless of whether it has been committed (unless a seek is performed).

    However, with reactor, you must ensure that commits are performed in the right order, since records are not acknowledged individually, just the committed offset is retained.

    If you commit out of order and restart your app, you may get some processed messages redelivered.

    We recently added support in the framework for out-of-order commits.

    https://projectreactor.io/docs/kafka/release/reference/#_out_of_order_commits

    The current version is 1.3.11, including this feature.