We are using microservice architecture in our project. We deploy each service to a cluster by using Kubernetes. Services are developed by using Java programming language and Spring Boot framework.Three replicas exist for each service. Services communicate with each other using only events. RabbitMQ is used as a message queue. One of the services is used to send an email. The details of an email are provided by another service with an event. When a SendingEmail event is published by a service, three replicas of email service consume the event and the same email is sent three times. How can I prevent that sending emails by other two services?
Asked
Active
Viewed 646 times
2
-
What type of exchange are you using to publish the event? Sounds like you're using a [fanout exchange](https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-fanout). You can probably solve your problem if you use a [direct exchange](https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-direct) instead. That would guarantee that only one of them will get the message. – Edwin Dalorzo Jun 28 '19 at 21:57
1 Answers
2
I think it depends on how you work with Rabbit MQ.
You can configure the rabbit mq with one queue for these events and make spring boot applications that represent the sending servers to be "Competing" Consumers.
If you configure it like this, only one replica will get an event at a time and only if it fails to process it the message will return to the queue and will become available to other consumers.
From what you've described all of them are getting the message, so it works like a pub-sub (which is also a possible way of work with rabbit mq, its just not good in this case).

Mark Bramnik
- 39,963
- 4
- 57
- 97
-
How can I design only specific events are consumed by one instance? Maybe other events are needed to consume by all instances. – ugur Jun 24 '19 at 19:36
-
2You should read about various exchange types and routing keys in rabbit mq. Its extremely flexible In general you can make exchange to "duplicate" message to N queues (N = number of instances in replica) and each queue will have one consumer => its a pub-sub, probably what you have now. Alternatively you can configure the exchange to send to only one queue, and all recplicas will be competing consumers to that queue. In this case only one consumer will get the message at a time – Mark Bramnik Jun 24 '19 at 19:41