mongodbrabbitmqmicroservicesevent-driven-designoutbox-pattern

Is it possible to implement transactional outbox pattern for only RabbitMQ publish fail scenarios


I have a system that uses mongoDB as persistence and RabbitMQ as Message broker. I have a challenge that I only want to implement transactional outbox for RabbitMQ publish fail scenarios. I'm not sure is it possible because, I also have consumers that is using same mongoDB persistence so when I'm writing a code that covers transactional outbox for RabbitMQ publish fail scenarios, published messages reaching consumers before mongoDB commitTransaction so my consumer couldn't find the message in mongoDB because of latency.

My code is something like below;

1- start session transaction

2- insert into document with session (so it doesn't persist until I call commit)

3- publish rabbitMQ

4- if success commitTransaction

5- if error insert into outbox document with session than commitTransaction

6- if something went wrong on mongoDB abortTransaction (if published succeed and mongoDB failed, my consumers first check for mongoDB existence and if it doesn't exist don't do anything.)

So the problem is in here messages reaching consumer earlier than mongoDB persistence, do you advice any solution that covers my problem?


Solution

  • So I would like to share my solution.

    Unfortunately it's not possible to implement transactional outbox pattern for only fail scenarios.

    What I decided is, create an architecture around High Availability so;

    MongoDB as High Available persistence and RabbitMQ as High Available message broker.

    I removed all session transactions that I coded before and implemented immediate write and publish.

    In worst case scenario:

    1- insert into document (success)

    2- rabbitmq publish (failed)

    3- insert into outbox (failed)

    What will I have is, unpublished documents in my mongo. Even in worst case scenario I could re publish messages from MongoDB with another application but I'll not write that application until I'll face with that case because we can not cover every fail scenarios on our code. So our message brokers or persistences must be high available.