watermill icon indicating copy to clipboard operation
watermill copied to clipboard

Multiple topics, same handler

Open odannyc opened this issue 3 years ago • 4 comments

I have multiple topics and 1 generic handler that sends those messages to an external system.

For example:

Topics: user_topic, post_topic, comment_topic Handler: send_to_segment_handler

The router currently has a check for this and returns an error. Am I designing this correctly, or is there a better way to do what I'm trying to accomplish?

odannyc avatar Jun 27 '22 17:06 odannyc

Hey, can you describe this a little bit more in detail? Are the events coming from a single place and you need to route them onto these 3 topics?

m110 avatar Jun 27 '22 20:06 m110

i have something very similar, basically a single message that might require mutliple messages sent out to different topics.

So, single place (ex. change_email_request_handler injesting from topic.change_email_request) that outputs to two different topics (ex. user_detail_changed topic and change_email_response)

ianthpun avatar Oct 18 '22 04:10 ianthpun

I think what you want is consumer groups.

I'm going to write proper docs on this, but let me just paste what I shared on our discord server recently:

Use case 1

  • You have 5 replicas of the same service, each with an event handler subscribing for topic OrderPlaced.
  • You want a particular message to be delivered only to one of them to process it once.
  • In that case, you would have all 5 event handlers with the same consumer group, for example orders-service_order-completed-handler.

Use case 2

  • You have one service with 5 event handlers.
  • Each subscribes to OrderPlaced and each does something different in reaction to it.
  • You want a particular message to be delivered to all of them.
  • You would give each a unique consumer group, like order-completed-handler, send-newsletter-handler, add-discount-handler, etc.

Use case 3

  • Both cases combined. You have use case 2 but decided to run 5 replicas of your service.
  • Now when a message comes in, you want to deliver it to all 5 handlers but only to one replica each.
  • You end up with groups: orders-service_order-completed-handler, orders-service_send-newsletter-handler, orders-service_add-discount-handler, etc.

m110 avatar Jan 24 '23 21:01 m110

Actually, I think I got it wrong, as you want to publish messages on two topics.

In this case, you can either:

  1. Have two handlers, each publishing one message on a different topic.
  2. Have one handler that calls a publisher directly. You could use AddNoPublisherHandler for this on the router.

m110 avatar Jan 24 '23 21:01 m110