[Recipes] Optimistic Concurrency Control in Elasticsearch


Need to avoid concurrency issues while updating documents. For example, if two users try to update a document simultaneously (e.g. getting and incrementing a value), both might get the same value and update the same value. 

Solution Summary: 

Need to implement optimistic concurrency control to avoid oncurrency issues while updating documents. You can either manually specify the version of the document, in which case ES will not allow to update if your version is the lateast, or you can use the retry_on_conflicts parameter with number of retries to do optimistic concurrency control implicitely. 


You should have a working Elasticsearch cluseter and Kibana.

Create an employee document:

PUT /employee/_doc/1?pretty
  "name": "Heartin",
  "salary": 10000000

You should get back a response with _version = 1. Else delete existing one and create a new record. 

Solution Steps: 

Explicit use of _version for optimistic concurrency control

Replace the employee document created with a updates specifying current version:

PUT /employee/_doc/1?version=1
  "name": "Heartin",
  "salary": 10000001

Documents get updated and version gets updated to 2.


Try executing previous PUT request as is, keeping version=1. 

You will get an error of type "version_conflict_engine_exception":


"[_doc][1]: version conflict, current version [2] is different than the one provided [1]"



Note: Error will be same even if you do a  partial update here using the _update syntax specifying version=1.


Implicit Optimistic Concurrency Control using retry_on_conflicts

POST /employee/_doc/1/_update?retry_on_conflict=5
  "doc": {
    "salary": 10000002


  1. We are doing a partial update here using the _update syntax.

  2. Need to use POST, PUT will not work with partial updates.

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook


Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.


CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream