Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MassTransit - publish to all consumer instances

Tags:

masstransit

I am looking for a way for each consumer instance to receive a message that is published to RabbitMQ via MassTransit. The scenario would be, we have multiple microservices that need to invalidate a cache on notification. Pub-Sub won't work in this instance as there will be 5 consumers of the same type as its the same code per service instance, so only one would receive the message in a traditional PubSub.

Message observation could be an option but this means the messages would never be consumed and hang around forever on the bus.

Can anyone suggest a pattern to use in the context of MassTransit?

Thanks in advance.

like image 783
KnowHoper Avatar asked Dec 23 '22 08:12

KnowHoper


1 Answers

You should create a management endpoint in each service, which could even be a temporary queue (just request a receive endpoint without a queue name and one will be dynamically generated). Then, put your queue invalidation consumers on that endpoint. Each service instance will receive a unique instance of the message (when Publish is called), and those queues and bindings will automatically be removed once the service exits.

This is exactly how the bus endpoint works, but in your case, you're creating a receive endpoint which can have consumer message type bindings, so that published messages are received, one copy per service.

cfg.ReceiveEndpoint(cfg => { ... });

Note that the queue name is not specified, and will be automatically generated uniquely.

like image 68
Chris Patterson Avatar answered Apr 01 '23 19:04

Chris Patterson