Skip to content
Kafka Is FunProgress

SpringSpring Kafka Advanced

How does @KafkaListener handle partition assignment and offset control?

Reference answer

Use topicPartitions for explicit partition+offset assignment: @KafkaListener(topicPartitions = @TopicPartition(topic='my-topic', partitionOffsets = @PartitionOffset(partition='0', initialOffset='0'))). For dynamic assignment use topic + partition without offset. ConsumerSeekAware interface lets you programmatically seek after assignment: implement onPartitionsAssigned() to call consumer.seek(). For timestamp-based seeking: use seekForTimestamp() from ConsumerSeekAware. These are useful for replay scenarios or consuming historical data from a specific point.

Expected key concepts: topicPartitions, @TopicPartition, @PartitionOffset, initialOffset, ConsumerSeekAware, onPartitionsAssigned, seek, seekForTimestamp, dynamic assignment, replay