When you start using messaging, you will notice that your application oftentimes updates the database and then needs to publish an event or message to the message broker. The problem is because the message broker and database are two different resources and you are going to need a distributed transaction. Here’s how you can avoid the distributed transaction but still atomically saved it to the database and publish a message.

You have your client and a database wherever you are storing your data that could a relational database or a document store doesn’t really matter but the point is in your code, you are going to be making that statement to save some data or change some data to the database and then a secret step altogether is where you need to publish that message to your message broker to a queue.

The problem is because these are two different operations. What happens if you were able to save to the database but then for whatever reason in code some you had a failure where you couldn’t actually publish the message. This could be because of many different reasons. Let’s say that the queue is unavailable for whatever reason or more likely you have a bug in your code. So you saved it to the database but you are unable to publish the message.

#outbox-pattern #microservices #cap

Outbox Pattern — Save it Reliably
2.10 GEEK