MongoDb, RabbitMq and outbox pattern

865 views Asked by At

I have a question about mongodb and outbox pattern (and i'm quite newbie in mongo).

I am working on application, that uses mongodb as primary database. I have some use cases, in which i need to save document to the database and then publish some event to message broker (RabbitMq). Saved document must be consistent with published event - this means that if I save document i MUST also send message (solution must be resilient to for example server shutdown between document save and message send) so I decided to use outbox pattern. In relational (sql) database this problem is trivial: I just start new transaction, then persist/change new object, then persist some kind of database scheduler record (that sends message to RabbitMq after transaction is committed), and then commit.

How do I achieve this in mongodb (once again with emphasis on no data loss and no 'phantom' message send). Should I use mongodb transactions (transactions are strongly disadvised in mongo community) or there are other, better solutions?

1

There are 1 answers

0
Gowtham On

you can try mongodb change streams at a collection level. It is similar to triggers on the RDBMS world. You can then listen on that stream for events of your choice, in your case - an insert event, post which you can send that event to downstream systems.