Shards and replicas in Elasticsearch Shards and replicas in Elasticsearch elasticsearch elasticsearch

Shards and replicas in Elasticsearch


I'll try to explain with a real example since the answer and replies you got don't seem to help you.

When you download elasticsearch and start it up, you create an elasticsearch node which tries to join an existing cluster if available or creates a new one. Let's say you created your own new cluster with a single node, the one that you just started up. We have no data, therefore we need to create an index.

When you create an index (an index is automatically created when you index the first document as well) you can define how many shards it will be composed of. If you don't specify a number it will have the default number of shards: 5 primaries. What does it mean?

It means that elasticsearch will create 5 primary shards that will contain your data:

 ____    ____    ____    ____    ____| 1  |  | 2  |  | 3  |  | 4  |  | 5  ||____|  |____|  |____|  |____|  |____|

Every time you index a document, elasticsearch will decide which primary shard is supposed to hold that document and will index it there. Primary shards are not a copy of the data, they are the data! Having multiple shards does help taking advantage of parallel processing on a single machine, but the whole point is that if we start another elasticsearch instance on the same cluster, the shards will be distributed in an even way over the cluster.

Node 1 will then hold for example only three shards:

 ____    ____    ____ | 1  |  | 2  |  | 3  ||____|  |____|  |____|

Since the remaining two shards have been moved to the newly started node:

 ____    ____| 4  |  | 5  ||____|  |____|

Why does this happen? Because elasticsearch is a distributed search engine and this way you can make use of multiple nodes/machines to manage big amounts of data.

Every elasticsearch index is composed of at least one primary shard since that's where the data is stored. Every shard comes at a cost, though, therefore if you have a single node and no foreseeable growth, just stick with a single primary shard.

Another type of shard is a replica. The default is 1, meaning that every primary shard will be copied to another shard that will contain the same data. Replicas are used to increase search performance and for fail-over. A replica shard is never going to be allocated on the same node where the related primary is (it would pretty much be like putting a backup on the same disk as the original data).

Back to our example, with 1 replica we'll have the whole index on each node, since 2 replica shards will be allocated on the first node and they will contain exactly the same data as the primary shards on the second node:

 ____    ____    ____    ____    ____| 1  |  | 2  |  | 3  |  | 4R |  | 5R ||____|  |____|  |____|  |____|  |____|

Same for the second node, which will contain a copy of the primary shards on the first node:

 ____    ____    ____    ____    ____| 1R |  | 2R |  | 3R |  | 4  |  | 5  ||____|  |____|  |____|  |____|  |____|

With a setup like this, if a node goes down, you still have the whole index. The replica shards will automatically become primaries and the cluster will work properly despite the node failure, as follows:

 ____    ____    ____    ____    ____| 1  |  | 2  |  | 3  |  | 4  |  | 5  ||____|  |____|  |____|  |____|  |____|

Since you have "number_of_replicas":1, the replicas cannot be assigned anymore as they are never allocated on the same node where their primary is. That's why you'll have 5 unassigned shards, the replicas, and the cluster status will be YELLOW instead of GREEN. No data loss, but it could be better as some shards cannot be assigned.

As soon as the node that had left is backed up, it'll join the cluster again and the replicas will be assigned again. The existing shard on the second node can be loaded but they need to be synchronized with the other shards, as write operations most likely happened while the node was down. At the end of this operation, the cluster status will become GREEN.

Hope this clarifies things for you.


An index is broken into shards in order to distribute them and scale.

Replicas are copies of the shards and provide reliability if a node is lost. There is often confusion in this number because replica count == 1 means the cluster must have the main and a replicated copy of the shard available to be in the green state.

In order for replicas to be created, you must have at least 2 nodes in your cluster.

You may find the definitions here easier to understand:http://www.elasticsearch.org/guide/reference/glossary/


Shard:

  1. Being distributed search server, ElasticSearch uses concept calledShard to distribute index documents across all nodes.
  2. An index can potentially store a large amount of data that canexceed the hardware limits of a single node
  3. For example, a single index of a billion documents taking up 1TB ofdisk space may not fit on the disk of a single node or may be tooslow to serve search requests from a single node alone.
  4. To solve this problem, Elasticsearch provides the ability tosubdivide your index into multiple pieces called shards.
  5. When you create an index, you can simply define the number of shardsthat you want.
  6. Documents are stored in shards, and shards are allocated to nodes inyour cluster
  7. As your cluster grows or shrinks, Elasticsearch will automaticallymigrate shards between nodes so that the cluster remains balanced.
  8. A shard can be either a primary shard or a replica shard.
  9. Each document in your index belongs to a single primary shard, sothe number of primary shards that you have determines the maximumamount of data that your index can hold
  10. A replica shard is just a copy of a primary shard.

Replica:

  1. Replica shard is the copy of primary Shard, to prevent data loss incase of hardware failure.
  2. Elasticsearch allows you to make one or more copies of your index’sshards into what are called replica shards, or replicas for short.
  3. An index can also be replicated zero (meaning no replicas) or moretimes.
  4. The number of shards and replicas can be defined per index at thetime the index is created.
  5. After the index is created, you may change the number of replicas dynamically anytime but you cannot change the number of shardsafter-the-fact.
  6. By default, each index in Elasticsearch is allocated 5 primary Shards and 1 replica which means that if you have at least two nodesin your cluster, your index will have 5 primary shards and another 5replica shards (1 complete replica) for a total of 10 shards perindex.