Operating a cluster

Shards and replicas, the sizing you cannot change later, aliases for zero-downtime reindexing, and search versus indexing pressure.

13 min read🔎 Search with Elasticsearch

Elasticsearch is a distributed system you operate, and its two hardest facts are decided before the first document: how many shards an index has, which cannot be changed, and how it will be reindexed when the mapping changes, which it will. Everything else — replicas, sizing, the pressure that indexing and search put on each other, what to watch — follows from those two.

Shards and replicas

An index is split into primary shards, each a complete Lucene index holding a slice of the documents, spread across the nodes; each primary has zero or more replica shards, copies on other nodes that serve reads and take over when a primary's node is lost. A search fans out to one copy of every shard, each returns its top hits, and the coordinating node merges them — so search latency is the slowest shard's, and the shard count is the parallelism.

json
PUT /articles-v3
{ "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { ... } }

The number of primaries is fixed at creation (the _split and _shrink APIs exist, with constraints, and are themselves a reindex in disguise). The number of replicas can change any time. A green cluster has every shard and replica allocated; yellow has every primary but a missing replica (one node down, or a single-node cluster with replicas: 1, which can never be green); red has a missing primary and a slice of the data unavailable.

Sizing: the decisions you cannot change later

Two rules from the operators who run large clusters, and they pull against each other:

  • Shards between about 10 and 50 GB, because a shard is the unit of recovery and rebalancing, and moving a 300 GB shard after a node failure takes hours during which the cluster is yellow.
  • As few shards as possible, because every shard costs memory (heap for its segment metadata, file handles, a share of the coordination on every search) whether or not it is busy. A cluster with ten thousand tiny shards is slow for no reason the data explains, and the default of five primaries per index in old versions created exactly that; the default is one now.

So: estimate the index's size at the horizon you can see (documents × average size, plus the inverted index, usually 1× to 1.5× the raw data), divide by the target shard size, round up, and for anything under 30 GB use one primary. Time-based data — logs, events — does not size one index at all: it uses an index per day or week (or ILM, index lifecycle management, which rolls over on size or age) so that each index is small, old ones are deleted or moved to cheaper nodes, and the shard count grows with time rather than with a guess. Node-wise, the heap is at most 32 GB (compressed pointers) and at most half the machine's RAM, because Lucene uses the filesystem cache for the other half, and that cache is what makes search fast.

Aliases: the indirection that makes everything else possible

An alias is a name that points at one or more indices, and clients use the alias, never the index:

json
POST /_aliases
{ "actions": [ { "remove": { "index": "articles-v2", "alias": "articles" } },
               { "add":    { "index": "articles-v3", "alias": "articles" } } ] }

That swap is atomic: every query on articles before it hit v2, every query after hits v3, and no query saw neither. Set an alias for reads and, for time-based data, a write alias with is_write_index that rollover moves. A client that names a concrete index has coupled itself to the one thing you will need to replace.

Reindexing without downtime

A mapping cannot change for an existing field, an analyser cannot change for an existing index, and the shard count is fixed — so a real change is a new index and a copy:

  1. Create articles-v3 with the new mapping and settings.
  2. POST /_reindex { "source": { "index": "articles-v2" }, "dest": { "index": "articles-v3" } }, which reads every document from the old index and writes it to the new one, in batches, resumable, with slices: auto to parallelise. On a large index this runs for hours, and the old index keeps serving through the alias the whole time.
  3. Handle the writes that arrive during the copy. Either dual-write from the application to both indices for the duration, or stop writes briefly, reindex the tail (_reindex with a query on updatedAt since the copy began), and switch. The outbox lesson's relay is what a serious system uses here: the search index is a projection of the source of truth, rebuilt from the events, and dual-writing from the application is the dual-write problem in a new costume.
  4. Swap the alias, in the one atomic call above.
  5. Verify, then delete v2 — not before, because the swap back is the rollback.

Reindexing is routine, and a team that has not done it is a team that will do it for the first time under pressure. Do it once on purpose, with the runbook written, before the mapping change that forces it. The _update_by_query API is the small sibling for a change that needs no new mapping — a computed field, a bulk correction — and it is a rewrite of every matched document, with the segment cost that implies.

Search versus indexing pressure

Indexing and searching compete for the same nodes. A bulk load of a million documents fills the indexing queues and the CPU with segment writes and merges; searches on the same nodes get slower, and if the bulk is large enough the thread pool queues reject with 429 es_rejected_execution_exception, which the client must treat as backpressure — the backpressure lesson's rule, in a 429. The levers: bulk in batches of a few thousand documents or 5 to 15 MB, not one at a time and not a hundred thousand at once; raise refresh_interval (or set it to -1) during a bulk load so the engine is not making segments searchable every second for data nobody is searching yet, and put it back after; hot-warm architectures with dedicated nodes for the index being written and cheaper nodes for the ones only read; and, at the extreme, separate clusters for ingest and query fed by the same events. Merges are the background cost that surprises people — the force_merge API compacts a read-only index after its load and should never run on one still being written.

Monitoring: what to watch

  • Cluster health (_cluster/health): green/yellow/red, unassigned shards, and the pending tasks count.
  • Node stats (_nodes/stats): JVM heap in use — old-generation pressure and long GC pauses are the first sign of too many shards or a too-large aggregation; the filesystem cache hit rate; disk watermarks, because a node past the flood stage (95%) makes every index read-only and that is an outage that looks like a bug.
  • Thread pools: search and write queue depths and rejections, which is the pressure section made visible.
  • Per-index: indexing rate, search rate, query latency percentiles (the observability course's rule: never the average), refresh and merge times.
  • Slow logs: the index and search slow logs with thresholds, which is where the wildcard query and the 10,000-bucket aggregation confess.

Snapshots to object storage on a schedule are the backup, and the backup lesson's rule holds: a restore that has never been run is a hope. Version upgrades are rolling, one node at a time, and the mapping for a major version's breaking changes is the release notes — read before, not after.

Under the hood: how a document finds its shard, and why "just written" and "searchable" are two different moments

Routing decides which primary shard owns a document, and it is arithmetic, not a lookup table. By default shard_num = hash(_id) % number_of_primary_shards (a custom _routing value can replace _id in that formula, to co-locate related documents on the same shard deliberately). That single line of arithmetic is the real reason the primary shard count is fixed at creation: change the count and the modulus changes, so every existing document's hash(_id) % new_count almost certainly lands on a different shard than the one it was actually written to — there is no way to "rebalance" a routing formula, only to reindex through it again. Aliases and _reindex are not a workaround for a limitation; they are the direct consequence of routing being a pure function of the shard count.

A write is a two-stage handoff, and both stages happen before the client sees a response. Any node can receive the index request and acts as a coordinator; it computes the shard, forwards the request to that shard's primary, and the primary applies the write first. Only after the primary has applied it does the primary forward the same write to every one of its replicas, and the response the client eventually sees waits for that replication to finish (governed by wait_for_active_shards, which defaults to requiring just the primary, not every replica). That is a primary-backup replication model — one designated primary is the source of truth for ordering writes to that shard, replicas apply what the primary tells them to apply — and it is a different design from a quorum system like Raft, where a write is committed once a majority of an odd-sized voting group agrees; there is no voting round here, and more replicas do not make a write more "consistent" the way a larger quorum would.

Replication of the write and searchability of the write are two separate guarantees, and conflating them is the most common surprise in production. The write above — primary, then replicas, then acknowledged — only guarantees the document is durably stored (in the on-disk translog, replayed on recovery) and applied in memory. It says nothing about when a search will find it. A shard becomes searchable in increments called refreshes: periodically (refresh_interval, default one second) each shard copy opens a new Lucene reader over the segments written since the last refresh, and only from that moment do those documents appear in search results on that copy. So a document can be fully acknowledged — durable, replicated, unlosable — and still invisible to a search run a hundred milliseconds later, and that gap is not a bug or eventual-consistency lag catching up on failure; it is the normal, designed-in cost of batching many small writes into a few segment refreshes instead of making every write immediately queryable, which is what "near-real-time" (not real-time) means in the product's own name for the feature.

Because any shard copy can answer a search, and refreshes happen per copy, two copies of the same shard can briefly disagree about what is searchable. A search fans out to one copy — primary or replica, Elasticsearch picks using adaptive replica selection, favouring whichever copy has been fastest recently — of every shard. The underlying operation was replicated synchronously to every copy before the write was acknowledged, but each copy refreshes independently, on its own clock, so a replica that refreshed a fraction of a second later than the primary can, for that fraction of a second, return a search result set that omits something the primary would already show. It is a narrow, self-correcting window rather than a genuine data disagreement — the data is already there on both copies — but it is the concrete mechanism behind "I searched right after I indexed and didn't find it," a report that a database engineer's read-your-writes intuition does not prepare them for.

Try it yourself

Diagnose "I just saved it and the search doesn't show it"

A support ticket says: "I updated a product's price, refreshed the product-search page half a second later, and it still showed the old price. A few seconds after that it was correct." The index has refresh_interval at its default. Using only what this lesson covers, explain what happened and name the one-line fix if the product team decides this flow must never show a stale price, even for a moment.

It is the refresh gap, not a bug — and the fix trades latency for freshness on that one request

The write (the price update) was acknowledged as soon as the primary applied it and replicated it to the replicas — that part is not eventually consistent, it already happened, durably, before the response returned. What was not yet true was searchability: the shard copy that answered the half-second-later search had not yet run its next refresh, so the new segment containing the updated document was not part of the Lucene reader that search was using. Once the next refresh happened (up to refresh_interval, one second by default, after the write), the same query returned the new price — which matches "a few seconds after that it was correct" exactly.

If the product decision is that this specific flow — write, then immediately show the result of that write — must never be stale, the fix is ?refresh=wait_for on the price-update write, which holds the response until the refresh has happened, so anything that reads after the response returns is guaranteed to see it. It is not the fix for every write in the system: forcing a refresh (or waiting for one) on every write in a high-volume ingest path reintroduces the per-write cost that batched refreshing exists to avoid. It belongs on the one request where "read your own write" is the actual product requirement.

Misconceptions

  • "More replicas means stronger consistency, the way a larger quorum does in Raft or Paxos." Elasticsearch replication is primary-backup: one primary orders every write to its shard and pushes it to replicas, there is no voting round, and adding replicas increases read capacity and failure tolerance, not the strength of any consistency guarantee on a write.
  • "A 200 response from an index request means the document is now searchable." It means the write is durable and replicated to the required copies. Searchability is a separate event — the shard's next refresh — that happens on its own interval, independent of when the write was acknowledged.

Sources

  • Elasticsearch documentation, "Near real-time search" and "Reading and writing documents" — the refresh cycle, the translog, and the primary/replica write path described above.
  • Clinton Gormley and Zachary Tong, Elasticsearch: The Definitive Guide (O'Reilly) — routing, sharding and the distributed write model.
Progress is saved on this device and to your account when signed in.