0

I am currently in the situation where I need to commit two transaction, one going towards the database (postgres, interacted with the npgsql library), and the one going on a kafka message bus (interacted with the confluent.kafka library).

Both support sending/inserting data in transaction, but I am currently having issues with determining how I should commit both the database transaction and messagebus transaction, since I need to perform a rollback on the other in case it fails.

My initial strategy was to do a

try
{
 database.commit()
}
catch
{
 messagebus.abort();
 throw;
}

messagebus.commit()

but this then does not cover the case where committing changes to kafka fails, triggers a rollback.

Can I somehow ensure both cases in anyway?

I am not Fat
  • 283
  • 11
  • 36
  • This is a typical case of a distributed transaction. Take a look at the Outbox Pattern ([here](https://www.kamilgrzybek.com/design/the-outbox-pattern/), [here](https://docs.particular.net/nservicebus/outbox/)), which was created to prevent needing distributed transactions. – Steven Aug 03 '21 at 20:29
  • @Steven what about using savepoints to rollback a committed transaction? – I am not Fat Aug 03 '21 at 21:36
  • You can try implementing a two-phase commit model, but I'd stay away from distributed transactions. There's a lot written about the dangers and downsides of distributed transactions. – Steven Aug 04 '21 at 05:35

0 Answers0