Problem:
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.
Prerequisites:
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
}
}
Note:
-
We are doing a partial update here using the _update syntax.
-
Need to use POST, PUT will not work with partial updates.
Recent comments