Replica Selection in Elasticsearch

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:

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

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.

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

1 Like

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.

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

1 Like

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

1 Like

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