Replica Selection in Elasticsearch


#1

What is the replica selection mechanism used in elastic search? I tried to look it up and search in the documentation but couldn't find it.

Thanks :slight_smile:


(Magnus Bรคck) #2

Replicas are allocated with the same algorithm as primary shards. I think the code for that is in BalancedShardsAllocator.java.


#3

Oops there was a typo in the question, my mistake. I meant to ask the replica selection mechanism i.e. on what basis a particular replica of a shard is selected in response to a query.


(Mark Walkom) #4

It just alternates between primary and replica, simple as that.


#5

So it's simply a round robin replica selection mechanism. Can you point me to the second of code or class that deals with this, if possible? Thanks a lot.


(Colin Goodheart-Smithe) #6

In practice its a bit more complex than a simple round robin selection due to taking into account the preference parameter and awareness attributes, although for the purposes of explanation it can be thought of that way. The TransportSearchTypeAction.BaseAsyncAction gets an iterator for each target shard containing the primary and replicas for that shard that match the preference and awareness criteria here and retrieves the replica to send the request to here. The actually iterators are created in OperationRouting class here.

Hope that helps


(Mark Walkom) #7

Heh, I should have prefaced mine with a TLDR really :stuck_out_tongue:


#8

Great, thanks. I will take a look :slight_smile:


(system) #9