1

Is elasticsearch strongly consistent? By that, I mean if a write to elasticsearch is succcessful, will any query of it return the most up to date data?

user7340
  • 256
  • 1
  • 10

1 Answers1

3

NO. Elastic Search is eventually consistent, NOT strongly consistent.

The index operation is near real-time, if the new writes haven't been refreshed, you cannot get the up-to-date data, even if you get a successful response for your write request.

for_stack
  • 21,012
  • 4
  • 35
  • 48
  • 1
    It is worth noting that you can get up-to-date data right after getting a successful index response by leveraging the [`?refresh=wait_for`](https://stackoverflow.com/a/38864048/4604579) parameter. – Val Nov 07 '19 at 05:09
  • Thanks for pointing it out! However, *wait_for* has penalty on performance. Also IMHO, when network partition happens, even if we use *wait_for*, client might get stale data from replica. – for_stack Nov 07 '19 at 06:46
  • The question didn't mention performance, but yeah, one should not "wait for" every indexing response to be refreshed. Regarding network partition, if that happens you have other issues than simply stale data. All this to say that [ES is NOT a database](https://stackoverflow.com/a/49630227/4604579) in the first place. – Val Nov 07 '19 at 06:51
  • From the CAP Theorem, you can only have 2 of the 3. Since ES allows to continue serving request (A) when network partition (P) happens, it has to sacrifice consistency (C). It's still NOT strongly consistent, although *wait_for* can partially mitigate the consistency problem if the network partition doesn't happen. – for_stack Nov 07 '19 at 07:24
  • 2
    The CAP theorem [doesn't really apply to ES](https://discuss.elastic.co/t/which-side-of-cap-theorem-elasticsearch-satisfy/177810). Other good discussions on the subject: [here](https://discuss.elastic.co/t/elasticsearch-and-the-cap-theorem/15102) and [here](https://discuss.elastic.co/t/cap-theorem/3014) – Val Nov 07 '19 at 07:45
  • @Val, great insights about CAP on ES – Amit Nov 07 '19 at 08:18
  • @for_stack The ES docs don't say that wait_for has a performance impact, except if the max listeners threshold is reached: https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html. Forcing a refresh is documented in that link to reduce performance, though. – user7340 Nov 07 '19 at 17:13
  • @Val These links are great! Thanks again! And I think these discussions confirms that Elastic Search is NOT guaranteed to be strongly consistent. Client might read stale data. – for_stack Nov 08 '19 at 03:25
  • @user7340 It DOES have performance impact. If you set *wait_for*, you won't get response from Elastic Search until the primary and replicas refresh the new writes. However, by default, each node takes 1 second to refresh the data. So you have to wait for at least 1 second to get the response. – for_stack Nov 08 '19 at 03:29