AI Hints
Home›Elasticsearch Tutorial›Shards, Health and Performance
Elasticsearch · Lesson 106 of 120

Shards and Replicas

How data is spread.

⏱ 2 min read▶ Video at 1:40:03🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

An index isn't stored in one piece. It's split into shards, and each shard can have copies, called replicas. That's how Elasticsearch grows across many machines, and survives a machine failing.

  • Index
  • Split into shards
  • Copied as replicas

Primary and replica shards

Each document goes to one primary shard. The primary copies it to its replicas, on other nodes. Searches can use either copy. If a node dies, a replica is promoted to primary.

  1. Documentrouted by _id
  2. Primary shardstores it
  3. Replicacopy on another node
  4. Node failsreplica takes over

Set shards and replicas

Settings set three primary shards and one replica of each. Then cat shards lists every shard and where it lives. Run it. Three primaries started, and three replicas unassigned, because this cluster has only one node.

Dev Tools Consoletext
PUT logs?filter_path=acknowledged
{ "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }

GET _cat/shards/logs?v&h=index,shard,prirep,state&s=shard,prirep
Output
# PUT logs 200 OK
{"acknowledged": true}

# GET _cat/shards/logs 200 OK
index shard prirep state
logs  0     p      STARTED
logs  0     r      UNASSIGNED
logs  1     p      STARTED
logs  1     r      UNASSIGNED
logs  2     p      STARTED
logs  2     r      UNASSIGNED

Summary

Indices split into primary shards, with replica copies on other nodes for safety and speed. Next, what the health colors mean.

Key takeaways
  • Primary shards
  • Replicas
  • Other nodes
Next up: Health Colors — green, yellow, red