Search code examples
javac#rabbitmqmessage-queuemicroservices

RabbitMQ - How to handle 2 asynchronous messages as consuming microservice?


I have a question searching for the best approach of solving a problem. We are currently working on replacing our software monolith by a microservice architecture.

We want to use a message queue as communication between the different microservices.

One of our microservice (the mail service) need to react after he has received two messages from two several microservices. The first message gives information about an order that has been made. It provides info about email content and recipients, the second message provides link to attachments, the email should include.

The mail service should wait until both messages are received. Currently i am planning to store all messages into the database (one table for each message) and check every 5 seconds if both info is existing and the email can be send.

But I have the feeling that maybe RabbitMQ also offers an approach, so I do not need to make my own development to avoid that race condition between those two messages.

What would be your approach to solve this?


Solution

  • I would change the message pattern so that you have three messages;

    1. First message is published containing order information. The outcome of this message being consumed is a second message being published containing additional information.

    2. Second message containing the additional information is processed and the information persisted. This is where you publish the third message.

    3. The third and final message acts as a notification to say "hey, I now have everything needed to actually send the email".

    This way you don't have to check a database every x seconds and you can extended the message workflow rather cheaply.

    Hope that helps :)