Transactional Outbox Pattern
Transactional Outbox pattern with Spring Boot
Background
suppose in a transaction, we have following actions:
db operation 1 – sending message – db operation 2
issues here is:
- if db operation 2 failed, the transaction data will roll back, but the message is sent.
one solution here is:
- db operation 1/2/.. – sending message
we put the sending message in the last step, within the transaction.
most of us may do in this way, it’s simple and reliable. only concern is it could be a long transaction as we interacting with exteral system.
do we have other ways?
- here comes transaction outbox pattern.
Definition
The Transactional Outbox is a way to ensure that 2 systems are in sync without having to use a distributed transaction between those systems.
with this pattern, we can first store the fact (send email/message…) that should do some external action in database. Then, an asynchronous process can look at the database to know what still needs to happen, and can do that whenever there is time. If the external system is not available, the task can be retried later until it succeeds.
Implementation
option 1: Spring Integration.
put the msg to a jdbc-backed output with a polling handler.
1 |
|
option 2: Spring Modulith
Communication between modules can be done asynchronously by using the ApplicationEventPublisher from Spring core.
Spring Modulith has additional infrastructure to ensure no such event is ever lost by first storing it in the database. We can leverage this to build our outbox pattern.
1 | // first transaction, ensure event are stored in db. |
Title: Transactional Outbox Pattern
Author: mjd507
Date: 2024-07-06
Last Update: 2024-07-06
Blog Link: https://mjd507.github.io/2024/07/06/Transactional-Outbox-Pattern/
Copyright Declaration: This station is mainly used to sort out incomprehensible knowledge. I have not fully mastered most of the content. Please refer carefully.