Publishing an Event to a Single Microservice Instance Publishing an Event to a Single Microservice Instance kubernetes kubernetes

Publishing an Event to a Single Microservice Instance


I understand better now, Here is the Image Link i posted earlier. I'm sure you already know about it. It is a common issue and the best way is to use a redis acting as a "blocking" mechanism. Since the message queue is sending requests asynchronously to all the consumers, we will not know who is receiving and processing the request but we are ensure that it isn't processed by all by using blocking. Before doing any operation, check for the block and if it doesn't exist, process the request.


I believe this can be solved by setting your consumerGroupId. Each instance of your application(each microservice of email service) needs to have a unique groupId so that rabbitmq will send request to any one of those available unique groups.

Here is how it works


I have found and implemented a partial solution, using Spring Cloud Stream along side with Spring Cloud Bus. I'm using Cloud Stream with custom queues and groups for single events (sentMail event for the first example). When I send a message through Cloud Stream and if multiple instances of a microservice are listening the message, they receive it one by one like this:

Sending messages: M1, M2, M3

Listening A microservice instances: A1, A2

With Cloud Bus:

A1 receives M1, M2, M3

A2 receives M1, M2, M3

With Cloud Stream (custom groups defined):

A1 receives M1, M3

A2 receives M2

For implementation: https://www.baeldung.com/spring-cloud-stream