by BehindJava
What is a Transactional Outbox Table in Microservices
In this tutorial, we are going to learn about Transactional Outbox Table in Microservices.
Transactional Outbox Table in Microservices
- In microservices, applications have to update a record locally and raise an event or send a message.
- Sending a message in the middle of a transaction is not reliable because there is no guarantee that the transaction will successfully commit.
- So to handle this problem, microservices should have a table that is called outbox table to write their messages or events into that table and start a database transaction and that table is also a part of that transaction and isolation level of that transaction is read committed.
- So that other systems cannot read the data that is not yet committed.
- That means only when a transaction is successfully committed, the outbox table will show the events and messages to the outside systems.
- A message relay service reads the committed records from the event or message outbox table and sends it to a message broker or an event streaming platform.
Let’s have a look at this image, where you have order service that has to update its database and send the message.
- So it creates a transaction where order table and outbox table are the part of that transaction, or service inserts the message into outbox table and the order table at the same time.
- If the transaction rolls back, then the Outbox table doesn’t show anything to their message relay service.
- But if the transaction commits successfully, message in outbox table becomes visible to message relay service or message relay service and publishes the message to Message Broker.