Transactional Outbox: A Simplified Approach
Transactional Outbox
I was reading the “Microservices Patterns” book by Chris Richardson and came across this pattern called “Transactional Outbox”, described also on the author’s website. It allows you to solve a case when you want to write a record to the database and send a related message to the consumers via a message broker, while keeping those two actions transactional.
The problem this pattern tries to solve is as follows. Suppose you persist an Order entity in your order-service microservice, and then send an OrderCreated message to another microservice (execution-service) that has to execute this Order. If these two actions are not executed in an atomic transaction, there’s a possibility that you save an Order to the database but fail to send it if the message broker is down.
If you first send the OrderCreated message and then save the Order to the database, it could happen that the database will be down or fails to accept the Order due to some constraint violation. Then the Order will not be saved, but its processing will be started, since we’ve already sent OrderCreated message to the broker.
We can use a distributed transaction (two-phase commit) to solve this. It would guarantee exactly-once delivery by joining multiple different resources (database and message…