e-oj / Fawn

Transactions for MongoDB (See the README)
https://www.npmjs.com/package/fawn
MIT License
485 stars 54 forks source link

Is the rollback safe? #58

Closed marcospgp closed 6 years ago

marcospgp commented 6 years ago

Is it possible that between the time a document is retrieved and a transaction is attempted, the document has been updated by some other process?

This would mean if the transaction fails, the rollback will change the document back to an older version, losing the new data in the process.

Can this happen or is this prevented in some way?

marcospgp commented 6 years ago

There is an interesting way this could be prevented:

The question is, if this happens, what should we do? We can't go back anymore because our changes are now cemented into the history of the document we changed.

I suppose at this point it would be a better idea to just pick a database that supports transactions or wait for version 4 of MongoDB 😄

Edit: Another option is to instead of rolling back by changing the document to a previous version, directly rolling back the changes that were made, such as decrementing if previously and increment was done. This could also fail in some ways, for example if in the meantime a user's balance has come down to zero and now we wanted to decrement it in order to roll back a previous update.

e-oj commented 6 years ago

Answered Here