Skip to content
Kafka Is Fun

SpringSpring Kafka Configuration

How do you configure multiple @KafkaListener annotations for the same topic with different group IDs?

Reference answer

Use the groupId attribute: @KafkaListener(topics='orders', groupId='order-processor') and @KafkaListener(topics='orders', groupId='analytics'), in different beans or the same bean. Each groupId is a separate consumer group with its own committed offsets, and Kafka delivers each message in the subscribed topics to one process in each consumer group, so both listeners see every message. Note that if the annotation has an id, that id is used as the consumer group.id (overriding the consumer factory's group.id) unless you set groupId explicitly or set idIsGroup to false. You cannot set group.id through the annotation's properties attribute; use groupId. Alternatively, listeners can use different container factories (containerFactory attribute) built on consumer factories with different group.id settings. A common practice in tests is to give each listener a unique groupId so state does not leak between tests.

Expected key concepts: groupId attribute, @KafkaListener, consumer group, committed offsets, idIsGroup, container factories, containerFactory, group.id, analytics, order-processor

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.