1

I have a producer producer who wants to send some_persistent_message to dbConsumer and some_Notification_message to notificationConsumer

Producer1 is sending the message with keys

db_key: some_persistent_message

notify_key:some_Notifiction_message

On the consumer side, I have a consumer group App1_group with the two consumers dbConsumer and notificationConsumer

enter image description here

At this point in time, my dbConsumer is always getting messages of some_Notification_message because of my consumer ending up owning a specific partition which is always getting notify_key:some_Notifiction_message

Is it possible to send some_persistent_message to dbConsumer and some_Notification_message to notification_consumer?

tintin
  • 5,676
  • 15
  • 68
  • 97

2 Answers2

1

If you must mix multiple message types in a single topic (to maintain chronological order of the messages, for example), then I'd suggest having a single overloaded message type which can represent each of your disparate message types and which is able to answer the kind of message it holds. Then set up a separate consumer group for each message type and associated processing logic. Finally, modify the logic in each consumer to only process appropriate messages.

Having two different consumer groups assumes that you have roughly the same number of messages of each type, otherwise there could be a lot of wasted CPU. If one message type occurs orders of magnitudes more often that the other, you might be better off merging the two consumers into a single consumer with a branch to perform the appropriate processing.

Widely differing numbers of messages can also interfere with processing the two types of messages chronologically with two different consumer groups.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
0

The best way is to simply use KafkaConsumer#assign() and specify the proper partitions.

Alternatively if you are okay with twice the I/O, simply use two consumer groups - both consumers will get all the messages, and in your consumer logic you can simply skip the messages of unwanted type.

hoodakaushal
  • 1,253
  • 2
  • 16
  • 31