Search code examples
messagingmicroservicesbroadcastjgroups

Broadcast message in microservices and run it once in multiple instances


I'm using micro services, suppose I have 3 services and they all are connected to each other through a JGroup UDP channel (JGroups can broadcast messages between them):

 ---            ---  ---
| A | --msg--> | B || C |
 ---            ---  ---

If I have just one instance of each service, everything in fine. But for example, if I have two instances of service C, both of them will receive the event and both of them will run their own procedure and the result would be duplicated in the database (sum the value two times).

 ---            ---  ---  ---
| A | --msg--> | B || C || C |
 ---            ---  ---  ---

Is there any way to manage this?


Solution

  • Create a seperate queue for each microservice (if not already created)

    In Rabbit MQ ,use Fanout Exchange hence each queue will reacieve all the messages.

    Now if multiple instance of a single microservice is bind with same queue then only one instance will be able to pick a message from that queue.Due to round robin nature of queue.