Reputation: 1330
My service needs to publish messages as part of a transaction that updates the database. I am using Postgres and Kafka, and I have decided to employ the Transactional Outbox pattern: https://microservices.io/patterns/data/transactional-outbox.html
The main idea behind the Transactional Outbox pattern is to write the message as part of the same transaction that updates the application's primary data source. This ensures atomicity and consistency by guaranteeing that both the data modification and the message creation either succeed together or fail together.
Within the database transaction that involves creating, updating, and deleting business objects, the service sends messages by inserting them into an outbox table. This insertion is performed within a local ACID transaction, ensuring atomicity. The outbox table serves as a temporary message queue.
In addition, I am utilizing Debezium to read the Postgres Write-Ahead Log (WAL) for the outbox table and publish the messages to Kafka.
However, I am currently facing a problem with the rapid growth of my outbox table. It has become crucial for me to find a reliable solution to delete events that have already been sent.
Do you have any ideas on how to solve this problem?
I have come up with several possible approaches:
Upvotes: 2
Views: 1265
Reputation: 8047
How are you processing messages in the outbox table?
If you are reading from the Write-Ahead Log (WAL), you can delete the messages as soon as you insert them, as the insertion entry in the WAL will still be there. This is a common practice when implementing this pattern.
You can even take it a step further and write directly to the WAL without even writing to a DB table using pg_logical-emit-message().
Upvotes: 1