# Update upsert and concurrency control

**URL:** <https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805>\
**Category:** Elasticsearch\
**Created:** [June 1, 2019, 5:54am UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805 "2019-06-01T05:54:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayantabrizi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shayantabrizi/32/45398_2.png) [@shayantabrizi](https://discuss.elastic.co/u/shayantabrizi)\
**Post date:** [June 1, 2019, 5:54am UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805/1 "2019-06-01T05:54:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [June 1, 2019, 9:18am UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805/2 "2019-06-01T09:18:55Z")

</div>

> [@shayantabrizi](#):
>
> Is it possible that ... the first request inserts its upsert document overriding the update of the second request?

No, because both of these requests will be performed with [`op_type` set to `create`](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type) so the second one will fail with a version conflict exception.

> [@shayantabrizi](#):
>
> Also, how can I make sure both of the updates are done and one is not overridden by the other request?

You will need to handle version conflicts by retrying.

---

<div class="post-metadata">

**Author:** ![shayantabrizi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shayantabrizi/32/45398_2.png) [@shayantabrizi](https://discuss.elastic.co/u/shayantabrizi)\
**Post date:** [June 1, 2019, 11:22am UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805/3 "2019-06-01T11:22:05Z")

</div>

> [@DavidTurner](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [June 1, 2019, 11:25am UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805/4 "2019-06-01T11:25:40Z")

</div>

> [@shayantabrizi](#):
>
> Can I be sure that this scenario does not happen

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

---

<div class="post-metadata">

**Author:** ![shayantabrizi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shayantabrizi/32/45398_2.png) [@shayantabrizi](https://discuss.elastic.co/u/shayantabrizi)\
**Post date:** [June 1, 2019, 12:02pm UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805/5 "2019-06-01T12:02:10Z")

</div>

> [@shayantabrizi](#):
>
> applied

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 29, 2019, 12:02pm UTC](https://discuss.elastic.co/t/update-upsert-and-concurrency-control/183805/6 "2019-06-29T12:02:11Z")

</div>

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