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.
#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.
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.
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.
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.
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.