Highlights

Contents

Part VI
06

Shards and replicas

We split one logical collection across machines. Distribution adds coordination work and incomplete-result failure modes. We will trace shard ownership, replica copying, query scatter, and result fetches.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Search basics: why LIKEfails2Tokenization and analysis3The inverted index4Ranking: TF-IDF and BM255The indexing pipeline6Shards and replicas7Autocomplete and fuzzysearch8Operating a searchcluster9Case study: search end toend

#Section 21A shard is part of the searchable collection

Heron’s illustrative million-document index occupies 750,000,000 bytes under an explicitly assumed index-to-source ratio of 1.5. Four equally sized primary shards would each hold 187,500,000 bytes. This is an arithmetic exercise; real shard balance depends on document sizes and routing, and actual index expansion must be measured.

A shard is a separately managed subset of a logical index. A primary shard accepts a routed write for its replication group. A replica shard is another copy of that shard’s data, used for availability and potentially read capacity. Replicas duplicate a subset; they do not create additional logical partitions.

FOUR PRIMARIES WITH THREE COPIES EACH MAKE TWELVE SHARD COPIESFOUR PRIMARIES WITH THREE COPIES EACH MAKE TWELVE SHARD COPIESshard 0copycopyprimary187,500,000 Bshard 1copycopyprimary187,500,000 Bshard 2copycopyprimary187,500,000 Bshard 3copycopyprimary187,500,000 B4 × 3 = 12 copies; 2,250,000,000 B total
Figure 15.21Illustrative equal shard sizes from the stated index-size assumption. Real distributions must be measured.

With three total copies per primary, four primaries produce twelve shard copies and 2,250,000,000 data bytes before temporary work and metadata. The front matter’s replication factor means total copies. Many engine settings instead specify the number of replicas excluding the primary. State which convention you use so storage arithmetic does not silently gain or lose a copy.

#Section 22Route writes deterministically

A write for report 7 must repeatedly reach its ownership group. Routing maps a document’s routing value to a shard. In our tiny illustrative rule, shard equals document ID modulo 4. IDs 1, 2, 3, and 4 map to shards 1, 2, 3, and 0.

This simple rule explains determinism, not a deployed engine’s routing algorithm. Changing the modulus when adding a shard changes many assignments, so resizing is a data movement operation. A tenant routing key can narrow tenant queries to fewer shards, but a large tenant can become a hot ownership group. The routing key is therefore both a query optimization and a potential skew source.

THE SAME STABLE DOCUMENT ID SELECTS THE SAME SHARDTHE SAME STABLE DOCUMENT ID SELECTS THE SAME SHARDdoc 11 mod 4 = 1doc 22 mod 4 = 2doc 33 mod 4 = 3doc 44 mod 4 = 0P0P1P2P3illustrative modulo routing, not an engine-specific hash implementation
Figure 15.22Computed illustrative modulo routing. This is not a claim about Elasticsearch’s internal hash.

A routed read must use the same routing context that located the write. If a custom routing value is omitted later, a point lookup can ask the wrong shard even though the document exists. A wider search might still find it and conceal the bug. Keep routing metadata with the operation and distinguish lookup scope from document identity.

The coordinating node forwards a write to the primary, which validates and replicates according to the engine’s protocol. Promotion and acknowledgement rules decide what survives failures. Elastic’s replication description explains its primary-backup model and in-sync copies. The generic design lesson is to name the copies and acknowledgement boundary, not assume replication is instantaneous.

#Section 23Scatter queries and merge candidates

The user asks for the best ten reports across all four shards. A coordinating node distributes the query to a usable copy of each relevant shard and merges their candidate results. This is often called scatter-gather, because work spreads outward before the answers return to one place.

Each shard can return its local best ten document IDs and scores. The coordinator merges forty candidates and keeps the global best ten. Under a consistent scoring and tie-breaking rule, any global top-ten document must be within its shard’s local top ten. A document behind ten better local documents cannot enter the global top ten.

ONE SELECTED COPY OF EACH SHARD CONTRIBUTES ITS LOCAL TOP TENONE SELECTED COPY OF EACH SHARD CONTRIBUTES ITS LOCAL TOP TENcoordP0P1P2P34 × 10 = 40 scored candidates
Figure 15.23Computed illustrative candidate count is 4 times 10, or 40. Each logical shard contributes one selected copy.

Heron’s assumed 1,000 search requests per second would create 4,000 shard query executions per second when every request touches all four shards. More shards can divide local work but also add dispatch, per-shard overhead, and straggler exposure. Distribution is not free parallel speedup.

#Section 24Fetch bodies after choosing winners

Returning every matching body from every shard wastes bandwidth. A query phase returns compact candidate information. A fetch phase retrieves stored fields or source content for the selected winners. In our example, the coordinator receives forty candidate IDs but fetches only ten final documents.

MERGE SMALL SCORED CANDIDATES BEFORE FETCHING THE WINNING BODIESMERGE SMALL SCORED CANDIDATES BEFORE FETCHING THE WINNING BODIES40 candidates: ID + scoretop 1010 display documentsquery-then-fetch carries bodies only after global winner selection
Figure 15.24Computed illustrative query-then-fetch trace. Stored source bytes are fetched after candidate selection.

The fetch phase still needs the shard copy and search view that can resolve those candidate IDs. A failure between phases can require retry or produce an explicit partial response. Highlighting may need source text or stored offsets, so it adds work beyond candidate selection. A result’s display features belong in the capacity model.

A search timeout needs a declared response policy. Returning partial results can be useful for browsing, but the response must expose incompleteness, and aggregations may be wrong. Rejecting partial results gives a stronger completeness contract but increases visible failures. Choose per product action.