Search code examples
elasticsearchoptimistic-concurrency

Difference between version=1&version_type=external and if_seq_no=0&if_primary_term=1


Based on this answer, it's clearly to say:

  • version is a sequential number that counts the number of time a document was updated;
  • _seq_no is a sequential number that counts the number of operations that happened on the index, which is used with primary_term to uniquely determine the version on the index;

However as a user, is there any difference between version=1&version_type=external and if_seq_no=0&if_primary_term=1 to achieve concurrency control?

My understanding is that:

  • to achieve "external" concurrency control, if_seq_no=0&if_primary_term=1 can be a little more inconvenient since we have to record two fields at the same time in DB while we only need to record version as to version=1&version_type=external.

  • as for "internal" concurrency control, if_seq_no=0&if_primary_term=1 will improve the parallelism level while version is comparably worse.

Are these two conclusions right? If right, why specifically?


I did some simple experiments in ES 7.4.2 as follows:

POST test/_bulk
{"index": { "_id": 1 }}
{"test": 1}
{"index": { "_id": 2 }}
{"test": 2}

PUT test/_doc/1?version=1&version_type=external&error_trace=true 
{
  "test": 1
}

GET test/_doc/1

PUT test/_doc/1?if_seq_no=0&if_primary_term=1&error_trace=true 
{
  "test": 1
}

Solution

  • Based on this old post Elasticsearch Versioning Support, I see that _version is used in ES for concurrency control.

    While in Breaking changes in 6.7, I might just quote here.

    internal version may not uniquely identify a document’s version if an indexed document wasn’t fully replicated when a primary fails. As such it is unsafe to use for optimistic concurrency control, is deprecated and the option will no longer be available in Elasticsearch 7.0.0. Please use the if_seq_no and if_primary_term parameters instead.

    So basically the answers are:

    • we could use them both as "external" concurrency control if we have external versioning system;
    • while "internal" concurrency control for ES, we should use if_seq_no=0&if_primary_term=1 as the only option.