1

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?

user1079877
  • 9,008
  • 4
  • 43
  • 54

2 Answers2

5

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.

Vijay Parmar
  • 795
  • 4
  • 13
0

If you can't use additional service like RabbitMQ, add host-id (which C instance) in the message header before you broadcast. C instances check the header, if its id is present in the header it will process.

This mechanism requires that each instance has accurate information of other running instances.

You can further improve by load balancing among C instances.

srk
  • 91
  • 4