Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

What is seq_no and primary_term in elasticsearch?

the _seq_no and _primary_term as parameter needed to implement the optimistic locking.

Elasticsearch keeps tracks of the sequence number and primary term of the last operation to have changed each of the documents it stores. The sequence number and primary term are returned in the _seq_no and _primary_term fields in the response of the GET API:

primary_term

The primary term increments every time a different shard becomes primary during failover. This helps when resolving changes which occurred on old primaries which come back online vs. changes which occur on the new primary (the new wins).

The primary term for a replication group is just a counter for how many times the primary shard has changed.

These primary terms are incremental and change when a primary is promoted. They’re persisted in the cluster state, thus representing a sort of “version” or “generation” of primaries that the cluster is on.

To ensure an older version of a document doesn’t overwrite a newer version, every operation performed to a document is assigned a sequence number by the primary shard that coordinates that change.

Let’s say your index is made up of 5 primary shards (that was the default prior to version 7). Indexing and Update-Requests are performed against primary shards. If you have multiple primary shards, elasticsearch is able to parallelize/distribute incoming requests (e.g. huge bulk-requests) to multiple shards in order to enhance performance.

So the primary_term gives information about the primary shard (#1, #2, #3, #4 or #5 in this example) that executed/coordinated the change/update.

seq_no

Sequence numbers have been introduced in ES 6.0.0. Just before that release came out.

Once we had the protection of primary terms in place, we added a simple counter and started issuing each operation a sequence number from that counter. These sequence numbers thus allow us to understand the specific order of index operations that happened on the primary

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
So if you create a second document, you’ll see that version and _seq_no will be different.


Let's create three documents:

POST test/_doc/_bulk
{"index": {}}
{"test": 1}
{"index": {}}
{"test": 2}
{"index": {}}
{"test": 3}
In the response, you'll get the payload below.

{
  "took" : 166,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "d2zbSW4BJvP7VWZfYMwQ",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "eGzbSW4BJvP7VWZfYMwQ",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "eWzbSW4BJvP7VWZfYMwQ",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}Code language: PHP (php)

As you can see:

for all documents, version is 1
for document 1, _seq_no is 0 (first index operation)
for document 2, _seq_no is 1 (second index operation)
for document 3, _seq_no is 2 (third index operation)

Reference

  • https://www.elastic.co/blog/elasticsearch-sequence-ids-6-0

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

How to install & configure Elastic Filebeat?

Filebeat client is a lightweight, resource-friendly tool that collects logs from files on the server and forwards these logs to your Logstash instance for processing. Filebeat is…

Read More

Install and Configure Elasticsearch: Step by Step Guide

Install and Configure Elasticsearch in Linux Download and Extract Elasticsearch Run and Verify Elasticsearch 9.3 Run and Verify Elasticsearch 7.x Run and Verify Elasticsearch 8.x Run and…

Read More

About Elastic (The Company)

Certainly! Here’s a detailed overview of Elastic N.V. (commonly known as Elastic, the company behind Elasticsearch), covering its transformation, history, key milestones, product evolution, and major release…

Read More

How to install kibana and configure with Elasticsearch

Download & Extract kibana 8.x Configure kibana 8.x Download, Extract, Install and Configure kibana 9.x vi config/kibana.yml ./bin/kibana –allow-root Provide Enrollment Kye of ElasticSearch Login to Kibana…

Read More

Aws Tutorials: FinOps – List of all Elasticcache cost optimization strategies

Here’s a table summarizing the cost optimization strategies for Amazon ElastiCache: Strategy Description Right-Sizing Instances Select instance types that match workload requirements. Use smaller nodes for development…

Read More

AWS Tutorials: Difference between Amazon Elasticsearch Service and Amazon Elastic cache Service

Here is a comparison table highlighting the differences between Amazon Elasticsearch Service (now known as Amazon OpenSearch Service) and Amazon ElastiCache Service: Feature/Aspect Amazon OpenSearch Service Amazon…

Read More