I see a recommendation for using the transactional outbox pattern (https://microservices.io/patterns/data/transactional-outbox.html) for scenarios in which a message should be published to a broker after some change in the database, so it can be "ACID". My question is: why not put the message publish inside the transaction like this?
try {
beginTransaction()
saveStuff()
publishMessage()
commitTransaction()
} catch() {
rollbackTransaction()
}
This way, whenever the message publish fails, the transaction will be rolled back. I see only one issue: you may not want to rollback the transaction when the message publish fails, trying again later. Are there other issues with this approach?
The publishMessage can work (the message is sent) but the commitTransaction might fail (eg lost connection to the database or the application crashes after sending message and before calling commit)