Search code examples
microservicesmessage-queuemessaging

How to being ensure from putting message into a Message Queue?


I have set up a RabbitMQ for messaging, the integration works fine. I am sending messages to a queue and consuming from the queue. but how can I be sure about sending a message to the queue after performing a task?

For example, After I Add a new item into DataBase, I should publish a message to the queue.

DBContext.SaveChanges(item);

//what if an error occurred at this moment.
QueueManager.Publish(item)

What is the best practice in this situation? any suggestion will appreciate.

I read this solution Solution.

But I think there should be a better solution because there are two problems with this approach:

  1. in microservice architecture may some of the databases do not support atomic transaction
  2. it needs extra data manipulation and an extra background service for removing records from databases

Solution

  • how can I be sure about sending a message to the queue after performing a task?

    Create the new table name Event, use the Event table which contains record of NonPublished events. Once done with the Database call, make an entry in the event table and mark that event as a NonPublished. And after publishing the event to queue, update that record as a Published event.

    What happens if the application not able to publish the event(Queue system is down, the publisher is failed)?

    Write a background job/cron job that runs periodically, which will check all Unpublished events and publish to queue and make the status as a Published. Also configure the retry mechanism, as mentioned by Tarun if the event published failed.

    Please refer to the below link for more information about Event-driven microservices.

    https://www.nginx.com/blog/event-driven-data-management-microservices/