Currently I am trying to understand how to implement a CAS operation in mongodb properly to support optimistic locking. I've found out that updates in mongodb
are atomic but I am not sure what this means (only the document rewrite is atomic or all cycle of update, including search for corresponding document and its rewriting, is atomic?). Lets consider following example.
- Some document exists in some collection with
_id
value set to 123 and has attributecas_val
set to 10. - The first client wants to update
cas_val
of the document with_id
equal to 123 to 11. - The second client wants to update
cas_val
of the document with_id
equal to 123 to 11. - Both clients operate at the same time and operations can interleave.
So, is it possible that both operation succeed in case when no other updates to the document with _id
123 were performed?
P.S. Is there some build in technique in mongodb
for the optimistic locking scenario?