When making parallel requests using Promise.all inside of an acid transaction, MongoDB will occasionally throw this error which I’m assuming is something to do with a race condition: “Given transaction number x does not match any in-progress transactions. The active transaction number is x”.
I’m not the only one running into this issue, as I have scoured the web to try and find a solution or trying to figure out why this error is being thrown as I don’t believe this is expected behaviour and cannot see it documented anywhere.
If you have a look at this answer on the same issue posted on StackOverflow: mongodb - MongoTransactionException: Query failed with error code 251 - Stack Overflow this persons conclusion after debugging the issue is to “Ensure that the first request to MongoDB within a transaction is not done in parallel with any other request in that transaction.”
I can confirm that after implementing that change, so making sure that the first request to MongoDB in my transaction is not run within Promise.all solves the issue of the transaction occasionally failing with the error mentioned above.
I would like to understand if this is expected behaviour, and if so where it’s documented and why? Otherwise I believe this is a potential bug and probably something that needs to be addressed.
The expectation is that when making an acid transaction, I should be able to make multiple requests in parallel using Promise.all without this transaction intermittently failing, including the first request within the transaction.