Update upsert and concurrency control

Is upsert in Update API atomic? Assume that client 1 sends an update request with an upsert document and client 2 sends another request with the same upsert document. Is it possible that the first request checks if the object exists or not and sees that it does not exist; Then, the second request's thread starts and checks if the object exists and sees it does not exists; Then, the second request inserts the upsert document and performs its update; Then the first request inserts its upsert document overriding the update of the second request?

Also, how can I make sure both of the updates are done and one is not overridden by the other request?

No, because both of these requests will be performed with op_type set to create so the second one will fail with a version conflict exception.

You will need to handle version conflicts by retrying.

Thank you. About the second question: Can I be sure that this scenario does not happen:
Request 1 inserts the upsert document and reads it to perform the update. Before writing the updated value request 2 reads the document that request 1 has inserted and performs the update. Then both of them write their updated values, and thus one of them is overridden by the other (only one of the updates is applied).

I do not understand. Inserting a value and then updating it are two separate actions. "Request 1" can't do both of these things.

Thank you. Maybe I thought it inserts the upsert document and then performs the update. You are right. Documentation says that:

If the document does exist, then the script will be executed

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.