liveBook · Manning

Primary shard

As explained in Elasticsearch in Action, Second Edition

A primary shard is a shard that holds the authoritative documents for part of an Elasticsearch index.

A primary shard is the authoritative storage unit for part of an Elasticsearch index. It holds documents; replica shards keep copies for redundancy and search capacity. An index can distribute its primary shards across one or more cluster nodes. section 3.2.2

Each document has one permanent primary-shard home. Elasticsearch hashes the document ID, applies modulo against the index’s number of primary shards, and uses the resulting shard number for indexing and retrieval. section 3.5

Creating a chats index

PUT chats { "settings": { "number_of_shards": 1, "number_of_replicas": 1 } } This creates one primary shard and one replica. On a single-node cluster, Elasticsearch does not place the replica on the same node as the primary, so the replica remains unassigned until another suitable node is available. section 14.1.1

Plan primary shards before creating an index:

  • Choose the number of primary shards for expected current and future data size; the setting cannot be changed on a live index. section 3.2.2 section 3.5
  • If growth exhausts the shards, create a new index with appropriate settings and reindex the existing data. section 3.5
  • Increase replicas when read throughput needs to rise, but budget memory because every replica copies its primary shard. section 14.1.3 section 14.3.1
shard_number = hash(id) % number_of_shards

Use the routing formula to see why a document belongs to exactly one primary shard and why changing the shard count disrupts lookup. section 3.5

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains physical storage, document routing, shard-count immutability, replica placement, and memory sizing.
Examples
Uses a chats index, a hash-modulo routing formula, single-node placement, and a 10-shard, two-replica memory-sizing scenario.
What it assumes
Assumes basic familiarity with Elasticsearch indexes, documents, nodes, and replicas while explaining shard roles from the ground up.
What you can do after
Configure primary-shard counts during index creation and handle growth, placement, throughput, and sizing constraints operationally.
How it connects
Connects Elasticsearch indexes with replica shards, routing algorithms, reindexing, cluster health, read throughput, and capacity planning.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.