# Elasticsearch concurrent request handling with optimistic locking in Java UpdateRequest with Upsert

**URL:** <https://discuss.elastic.co/t/elasticsearch-concurrent-request-handling-with-optimistic-locking-in-java-updaterequest-with-upsert/256477>\
**Category:** Elasticsearch\
**Created:** [November 24, 2020, 9:47am UTC](https://discuss.elastic.co/t/elasticsearch-concurrent-request-handling-with-optimistic-locking-in-java-updaterequest-with-upsert/256477 "2020-11-24T09:47:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackOcean](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blackocean/32/19722_2.png) [@blackOcean](https://discuss.elastic.co/u/blackOcean)\
**Post date:** [November 24, 2020, 9:47am UTC](https://discuss.elastic.co/t/elasticsearch-concurrent-request-handling-with-optimistic-locking-in-java-updaterequest-with-upsert/256477/1 "2020-11-24T09:47:16Z")

</div>

## Concurrent request handling

I have one use case where application can get concurrent requests to update single document.  
Earlier I was using IndexRequest with External versionining but IndexRequest I was facing problem that it was replacing the old document with new document rather than updating old document.

```
        <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.1.1</version>
    </dependency>

```

But with IndexRequest external versioning was working but to be honest I find it bit cumbersome.

Neverthless, Now I thought to use UpdateRequest which will create the document with givenId or update the document(rather than replacning).

```
        UpdateRequest updateRequest = new UpdateRequest(elasticSearchConfiguration.getIndexName(), id);
        updateRequest.doc(objectMapper.convertValue(object, Map.class));
        updateRequest.docAsUpsert(true);
        updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

```

I am not able to understand how elasticsearch API will handles the concurrent request and throw VersionConflict exception. As per my initial testing, I don't think UpdateRequest is helping .

Any help would be great like how to best handle the concurrent request with elasticsearch (if possible not manual optimistic locking)

---

<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:** [December 22, 2020, 9:47am UTC](https://discuss.elastic.co/t/elasticsearch-concurrent-request-handling-with-optimistic-locking-in-java-updaterequest-with-upsert/256477/2 "2020-12-22T09:47:30Z")

</div>

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