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:
- in microservice architecture may some of the databases do not support atomic transaction
- it needs extra data manipulation and an extra background service for removing records from databases