Ensuring Atomicity Dependent on Third-Party API Calls

I have a use case similar to the following:

  1. Inserting a document into a MongoDB collection.
  2. Making an API call (Third-Party).

In this context, I want to ensure that if the API call fails, I can revert the insertion operation. How can I achieve this? Are transactions a good approach?

Thank you in advance for your assistance!

The response of the insert operation will be the unique _id of the document that you insert. The best thing to do would be to try the API call and if it fails, delete (or even better, archive) the document that you inserted. You would need to implement this functionality in your client code.

Do not use transactions unless you have a specific need to alter fields in multiple collections simultaneously.

Thank you for your quick response!

Here, I see that this approach would certainly work in most of the cases. But, if the server itself fails after performing the insert operation, it may leave database in inconsistent state. Any suggestions on this?

The best way to ensure reliability would be to use a 3-member Replica Set with a majority write concern.

Actually, I was concerned about the client facing server failure and not MogoDB. If transactions were to be used, client’s failure would have ensured the transaction doesn’t get committed

Maybe it would be better to make the API call first, and perform the insert only if it is successful. Doing this within a transaction will just add unnecessary overhead.

The API call is dependent on the insert operation. I cannot do that :frowning:

What information does the API call require from the insert operation?

Remember that a transaction is designed to allow multiple documents to be updated atomically. If one of the updates fails then a ‘rollback’ occurs automatically. I speak under correction but I am not sure that you can explicitly ‘fail’ a transaction.

There are multiple ways you could fix this without transactions. For example, you could add a field to the document in the insert operation (like pending: true), and then update or remove it after the API call completes.