{"id":23808,"date":"2021-09-24T22:50:36","date_gmt":"2021-09-24T22:50:36","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23808"},"modified":"2025-05-04T06:12:24","modified_gmt":"2025-05-04T06:12:24","slug":"what-is-seq_no-and-primary_term-in-elasticsearch","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-seq_no-and-primary_term-in-elasticsearch\/","title":{"rendered":"What is seq_no and primary_term in elasticsearch?"},"content":{"rendered":"\n<p>the _seq_no and _primary_term as parameter needed to implement the optimistic locking.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">primary_term<\/h2>\n\n\n\n<p>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).<\/p>\n\n\n\n<p>The primary term for a replication group is just a counter for how many times the primary shard has changed.<\/p>\n\n\n\n<p>These primary terms are incremental and change when a primary is promoted. They&#8217;re persisted in the cluster state, thus representing a sort of \u201cversion\u201d or \u201cgeneration\u201d of primaries that the cluster is on.<\/p>\n\n\n\n<p>To ensure an older version of a document doesn\u2019t overwrite a newer version, every operation performed to a document is assigned a sequence number by the primary shard that coordinates that change.<\/p>\n\n\n\n<p>Let&#8217;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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">seq_no<\/h2>\n\n\n\n<p>Sequence numbers have been introduced in ES 6.0.0. Just before that release came out.<\/p>\n\n\n\n<p>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<\/p>\n\n\n\n<p>version is a sequential number that counts the number of time a document was updated<br>_seq_no is a sequential number that counts the number of operations that happened on the index<br>So if you create a second document, you&#8217;ll see that version and _seq_no will be different.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Let<span class=\"hljs-string\">'s create three documents:\r\n\r\nPOST test\/_doc\/_bulk\r\n{\"index\": {}}\r\n{\"test\": 1}\r\n{\"index\": {}}\r\n{\"test\": 2}\r\n{\"index\": {}}\r\n{\"test\": 3}\r\nIn the response, you'<\/span>ll get the payload below.\r\n\r\n{\r\n  <span class=\"hljs-string\">\"took\"<\/span> : <span class=\"hljs-number\">166<\/span>,\r\n  <span class=\"hljs-string\">\"errors\"<\/span> : <span class=\"hljs-keyword\">false<\/span>,\r\n  <span class=\"hljs-string\">\"items\"<\/span> : &#91;\r\n    {\r\n      <span class=\"hljs-string\">\"index\"<\/span> : {\r\n        <span class=\"hljs-string\">\"_index\"<\/span> : <span class=\"hljs-string\">\"test\"<\/span>,\r\n        <span class=\"hljs-string\">\"_type\"<\/span> : <span class=\"hljs-string\">\"_doc\"<\/span>,\r\n        <span class=\"hljs-string\">\"_id\"<\/span> : <span class=\"hljs-string\">\"d2zbSW4BJvP7VWZfYMwQ\"<\/span>,\r\n        <span class=\"hljs-string\">\"_version\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"result\"<\/span> : <span class=\"hljs-string\">\"created\"<\/span>,\r\n        <span class=\"hljs-string\">\"_shards\"<\/span> : {\r\n          <span class=\"hljs-string\">\"total\"<\/span> : <span class=\"hljs-number\">2<\/span>,\r\n          <span class=\"hljs-string\">\"successful\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n          <span class=\"hljs-string\">\"failed\"<\/span> : <span class=\"hljs-number\">0<\/span>\r\n        },\r\n        <span class=\"hljs-string\">\"_seq_no\"<\/span> : <span class=\"hljs-number\">0<\/span>,\r\n        <span class=\"hljs-string\">\"_primary_term\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"status\"<\/span> : <span class=\"hljs-number\">201<\/span>\r\n      }\r\n    },\r\n    {\r\n      <span class=\"hljs-string\">\"index\"<\/span> : {\r\n        <span class=\"hljs-string\">\"_index\"<\/span> : <span class=\"hljs-string\">\"test\"<\/span>,\r\n        <span class=\"hljs-string\">\"_type\"<\/span> : <span class=\"hljs-string\">\"_doc\"<\/span>,\r\n        <span class=\"hljs-string\">\"_id\"<\/span> : <span class=\"hljs-string\">\"eGzbSW4BJvP7VWZfYMwQ\"<\/span>,\r\n        <span class=\"hljs-string\">\"_version\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"result\"<\/span> : <span class=\"hljs-string\">\"created\"<\/span>,\r\n        <span class=\"hljs-string\">\"_shards\"<\/span> : {\r\n          <span class=\"hljs-string\">\"total\"<\/span> : <span class=\"hljs-number\">2<\/span>,\r\n          <span class=\"hljs-string\">\"successful\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n          <span class=\"hljs-string\">\"failed\"<\/span> : <span class=\"hljs-number\">0<\/span>\r\n        },\r\n        <span class=\"hljs-string\">\"_seq_no\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"_primary_term\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"status\"<\/span> : <span class=\"hljs-number\">201<\/span>\r\n      }\r\n    },\r\n    {\r\n      <span class=\"hljs-string\">\"index\"<\/span> : {\r\n        <span class=\"hljs-string\">\"_index\"<\/span> : <span class=\"hljs-string\">\"test\"<\/span>,\r\n        <span class=\"hljs-string\">\"_type\"<\/span> : <span class=\"hljs-string\">\"_doc\"<\/span>,\r\n        <span class=\"hljs-string\">\"_id\"<\/span> : <span class=\"hljs-string\">\"eWzbSW4BJvP7VWZfYMwQ\"<\/span>,\r\n        <span class=\"hljs-string\">\"_version\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"result\"<\/span> : <span class=\"hljs-string\">\"created\"<\/span>,\r\n        <span class=\"hljs-string\">\"_shards\"<\/span> : {\r\n          <span class=\"hljs-string\">\"total\"<\/span> : <span class=\"hljs-number\">2<\/span>,\r\n          <span class=\"hljs-string\">\"successful\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n          <span class=\"hljs-string\">\"failed\"<\/span> : <span class=\"hljs-number\">0<\/span>\r\n        },\r\n        <span class=\"hljs-string\">\"_seq_no\"<\/span> : <span class=\"hljs-number\">2<\/span>,\r\n        <span class=\"hljs-string\">\"_primary_term\"<\/span> : <span class=\"hljs-number\">1<\/span>,\r\n        <span class=\"hljs-string\">\"status\"<\/span> : <span class=\"hljs-number\">201<\/span>\r\n      }\r\n    }\r\n  ]\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>As you can see:<\/p>\n\n\n\n<p>for all documents, version is 1<br>for document 1, _seq_no is 0 (first index operation)<br>for document 2, _seq_no is 1 (second index operation)<br>for document 3, _seq_no is 2 (third index operation)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/www.elastic.co\/blog\/elasticsearch-sequence-ids-6-0<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5936],"tags":[],"class_list":["post-23808","post","type-post","status-publish","format-standard","hentry","category-elastic"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=23808"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23808\/revisions"}],"predecessor-version":[{"id":23809,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23808\/revisions\/23809"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}