concurrency-control.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. [[optimistic-concurrency-control]]
  2. === Optimistic concurrency control
  3. Elasticsearch is distributed. When documents are created, updated, or deleted,
  4. the new version of the document has to be replicated to other nodes in the cluster.
  5. Elasticsearch is also asynchronous and concurrent, meaning that these replication
  6. requests are sent in parallel, and may arrive at their destination out of sequence.
  7. Elasticsearch needs a way of ensuring that an older version of a document never
  8. overwrites a newer version.
  9. To ensure an older version of a document doesn't overwrite a newer version, every
  10. operation performed to a document is assigned a sequence number by the primary
  11. shard that coordinates that change. The sequence number is increased with each
  12. operation and thus newer operations are guaranteed to have a higher sequence
  13. number than older operations. Elasticsearch can then use the sequence number of
  14. operations to make sure a newer document version is never overridden by
  15. a change that has a smaller sequence number assigned to it.
  16. For example, the following indexing command will create a document and assign it
  17. an initial sequence number and primary term:
  18. [source,console]
  19. --------------------------------------------------
  20. PUT products/_doc/1567
  21. {
  22. "product" : "r2d2",
  23. "details" : "A resourceful astromech droid"
  24. }
  25. --------------------------------------------------
  26. You can see the assigned sequence number and primary term in the
  27. `_seq_no` and `_primary_term` fields of the response:
  28. [source,console-result]
  29. --------------------------------------------------
  30. {
  31. "_shards" : {
  32. "total" : 2,
  33. "failed" : 0,
  34. "successful" : 1
  35. },
  36. "_index" : "products",
  37. "_id" : "1567",
  38. "_version" : 1,
  39. "_seq_no" : 362,
  40. "_primary_term" : 2,
  41. "result" : "created"
  42. }
  43. --------------------------------------------------
  44. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 2/"_primary_term" : $body._primary_term/]
  45. Elasticsearch keeps tracks of the sequence number and primary term of the last
  46. operation to have changed each of the documents it stores. The sequence number
  47. and primary term are returned in the `_seq_no` and `_primary_term` fields in
  48. the response of the <<docs-get,GET API>>:
  49. [source,console]
  50. --------------------------------------------------
  51. GET products/_doc/1567
  52. --------------------------------------------------
  53. // TEST[continued]
  54. returns:
  55. [source,console-result]
  56. --------------------------------------------------
  57. {
  58. "_index" : "products",
  59. "_id" : "1567",
  60. "_version" : 1,
  61. "_seq_no" : 362,
  62. "_primary_term" : 2,
  63. "found": true,
  64. "_source" : {
  65. "product" : "r2d2",
  66. "details" : "A resourceful astromech droid"
  67. }
  68. }
  69. --------------------------------------------------
  70. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 2/"_primary_term" : $body._primary_term/]
  71. Note: The <<search-search,Search API>> can return the `_seq_no` and `_primary_term`
  72. for each search hit by setting <<request-body-search-seq-no-primary-term,`seq_no_primary_term` parameter>>.
  73. The sequence number and the primary term uniquely identify a change. By noting down
  74. the sequence number and primary term returned, you can make sure to only change the
  75. document if no other change was made to it since you retrieved it. This
  76. is done by setting the `if_seq_no` and `if_primary_term` parameters of the
  77. <<docs-index_,index API>>, <<docs-update,update API>>, or <<docs-delete,delete
  78. API>>.
  79. For example, the following indexing call will make sure to add a tag to the
  80. document without losing any potential change to the description or an addition
  81. of another tag by another API:
  82. [source,console]
  83. --------------------------------------------------
  84. PUT products/_doc/1567?if_seq_no=362&if_primary_term=2
  85. {
  86. "product" : "r2d2",
  87. "details" : "A resourceful astromech droid",
  88. "tags": ["droid"]
  89. }
  90. --------------------------------------------------
  91. // TEST[continued]
  92. // TEST[catch: conflict]