Skip to content

Commit 0653ecb

Browse files
committed
GET requests should go to primary shard
GET requests should be routed to the primary shard via the `preference` parameter, to prevent consistency issues. By default, Elasticsearch will round-robin GET requests between all available shards (primaries and replicas). Updates to a document are only consistent from the perspective of the thread updating the document, to all other threads it is possible to see a stale copy while the update is "in-flight" to one of the replicas. By setting `preference=_primary`, the GET request is forced to route to the primary shard. Since the primary is the source of truth, it is guaranteed to always be consistent. You can [read more about preference here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-preference.html). Just as a note, when you add/remove aliases in a single operation, it is an atomic operation. E.g. if you do this: ``` POST /_aliases { "actions": [ { "remove": { "index": "my_index_v1", "alias": "my_index" }}, { "add": { "index": "my_index_v2", "alias": "my_index" }} ] } ``` The alias swaps from `my_index_v1` to `my_index_v2` atomically. You can read more about swapping aliases [here](https://www.elastic.co/guide/en/elasticsearch/guide/current/index-aliases.html) and [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html). If you were using this method and still seeing consistency issues, that's a bug...it'd be great if you can open a ticket! :)
1 parent de23faf commit 0653ecb

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/IndexRotator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public function getPrimaryIndex()
8686
$primaryPayload = [
8787
'index' => $this->configurationIndexName,
8888
'type' => static::TYPE_CONFIGURATION,
89-
'id' => static::PRIMARY_ID
89+
'id' => static::PRIMARY_ID,
90+
'preference' => '_primary'
9091
];
9192
try {
9293
$primary = $this->engine->get($primaryPayload);

0 commit comments

Comments
 (0)