New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save on condition (Optimistic concurrency) #4004
Comments
How is this different from versioning, other than the fact that versioning only affects array fields? |
@vkarpov15 Yes, it's same but versioning only affects array field. |
It should be pretty doable to implement this as a plugin for now. I'm hesitant to put it as a priority for core because versioning already confuses people a lot and is unnecessary in many cases. I think the way forward is plugin for now and if it turns out to be indispensable we can put it in core |
@vkarpov15 Could you give a quick pointer as to how this could be implemented as a plugin? I don't see how to hook into the .save() logic properly... |
Actually I'm not entirely sure if it's possible to do this with |
@vkarpov15 Thanks. If I have some free time, I'll try to make a plugin for this. |
8b4870c should give you the general direction of how one would write a plugin for this |
Is there a suggested plugin for this? |
Not that I know of, but this test is the general idea of how you would implement the plugin. I would love it if someone from the community could pick it up and run with it, I don't have much experience with OCC and I feel like I wouldn't be the right person to maintain it. |
@vkarpov15 can you elaborate on what you mean by "versioning only affects array fields?" Its my understanding that versioning is on a document, so no matter what field you try to update it will fail if version is out of date. |
That's correct, |
Has anyone had success using this plugin in production? Coming from a .NET/Java background, where optimistic concurrency in enterprise systems is widely supported (and unavoidable), lack of support here was surprising. I don't yet know enough about node/mongoose/mongodb so I'm scared to try some "random" plugin, so if anyone has enterprise experience with this please let us know how you tackled this problem? Side note: why was this issue closed? Is OC supported natively without plugins? |
@lonix1 we added the ability to implement OCC plugins, and it looks like OCC hasn't really been a concern because for most apps I've worked on, Mongoose's ability to only update the paths that have actually changed is good enough to prevent accidentally overwriting. |
@vkarpov15 Thank you for clarifying. I guess I'll use that plugin! I'm surprised though that optimistic concurrency (and related concurrency patterns) isn't natively supported... I notice that sequelize has support. Mongoose is otherwise a pleasure to use, so many thanks. |
What is the difference between this plugin and the VersionError already built-in mongoose? |
@mobicity Mongoose versioning is a limited subset of OCC that only looks at array fields. |
Please is there any support for this yet? Seems https://www.npmjs.com/package/mongoose-update-if-current is dead as there is no recent update again |
Background
This is a proposal for
optimistic concurrency
for mongoose.We sometimes use the following code to modify the document instead of
update
because validation is not ok (runValidator
doesn't support everything)However this is troublesome when another request is also modifying this document, it may cause the doc overwritten without any errors. I don't know what to call this phenomenon, maybe
race condition
Optimistic Concurrency Control
A better solution is that, when reading the model, store some important values, e.g.
updatedAt
or__v
version key.and when updating, add a condition that the timestamp should be the same.
This mechanism is called
Optimistic concurrency control
, orOCC
orupdate-if-current
.Reference: Mongodb Official document: Update Document if Current
However, this is very ugly.
Hence I propose an elegant way in mongoose.
Proposal 1 (General)
Proposal 2 (Native Support)
The previous proposal is a general way.
Maybe we can include occ during find:
If there are multiple values needed to be checked, use this way
Proposal 3 (Schema options)
update-if-current
is the formal name.Here, we emphasize that this behavior is working on
save-if-current
.The text was updated successfully, but these errors were encountered: