Lecture: Lecture 20 - ElasticSearch
Related: ElasticSearch Data Model Inverted Index Analyzers and Query DSL · Distributed Architectures Fragmentation Allocation and Transparency · Replication CAP Theorem ACID vs BASE and Conflict Resolution

Shards, Lucene segments, and clusters (Slides 60–64)

Sharding and near-real-time segments (Slides 61–62)

To scale an index beyond a single disk or machine, sharding divides an index into smaller partitions called shards (e.g., an index split into Shard A, Shard B, Shard C):

  • One shard per document: Each document belongs to a single primary shard.
  • Self-contained Lucene index: Each shard is itself a complete Apache Lucene index instance.
  • 1-second immutable segments: When data is written to a shard, it is periodically (every 1 second) written into a new immutable Lucene segment on disk, at which point it becomes available for querying.
  • Distribution unit: Shards are the elementary units distributed across nodes in a cluster.

Clusters and why sharding matters (Slides 63–64)

A cluster is a collection of multiple machines (nodes), where shards can live on any node (for instance, Node 1 hosting Shard A and Shard B, while Node 2 hosts Shard C and Shard D; Slide 63). Sharding delivers three key benefits (Slide 64):

  1. Scale storage beyond one disk: Split huge volumes across smaller nodes/disks (e.g., storing 1 TB of data without any single node needing 1 TB of disk capacity).
  2. Parallelize query execution: Operations run across multiple nodes in parallel, speeding up queries because several machines work on the same request simultaneously.
  3. Increase availability via shard replication: Shards can be replicated across different nodes so data survives machine failures.

Optimistic concurrency control and _version (Slides 65–68)

Optimistic concurrency control (Slide 66)

Unlike traditional ACID databases that acquire locks (Strict 2PL) before reading and writing, ElasticSearch uses optimistic concurrency control:

  • It assumes conflicts between concurrent writers are unlikely.
  • No locks are held across read-then-modify workflows; instead, if the underlying document changed between reading and writing, the update fails.
  • While straightforward on a single centralized node, concurrent updates plus replication across distributed nodes require explicit ordering.

Out-of-order replication and _version (Slides 67–68)

When shards are replicated (replica shards on other nodes) and a document is created, updated, or deleted (Slide 67):

  1. The primary copy (primary shard) is always written first.
  2. Replication requests to replica shards are sent in parallel—which means they can arrive at their destination out of sequence!

To guarantee that an older write never overwrites a newer write (Slide 68):

  • Every document carries a _version integer that increments whenever the document changes.
  • When a replica receives an update with an older _version after it has already applied a newer _version, the replica ignores the stale update.
  • Update and delete APIs also accept an explicit version parameter, allowing client applications to enforce optimistic concurrency control on demand so concurrent updates do not cause silent data loss.

Common confusions

  • Thinking writing through the primary shard prevents all ordering issues: Primary-first ordering serializes writes at the primary, but because the primary forwards updates to replicas in parallel, network delays can still reorder packets—which is why replicas check _version.
  • Confusing ElasticSearch shards with Lucene segments: A shard is a logical/distributed partition of an ES index (a full Lucene index); inside each shard, writes are flushed every ~1 second into immutable Lucene segments on disk.
  • Equating ES optimistic concurrency with ACID transactions: ES does not lock documents across multi-step transactions; it detects conflicting versions at write time on single documents using _version.

Retrieval questions with answers

1. Why does ElasticSearch still need a _version number on every document even though every write goes through the primary shard first?
After the primary shard executes the write, it sends replication requests to replica shards in parallel, so network delays can cause them to arrive out of sequence. Comparing _version lets replicas ignore an older update arriving after a newer one—and lets clients pass a version parameter for optimistic concurrency control.

2. What happens inside a shard when a new document is written, and how soon does it become searchable?
Writes to a shard are periodically (every 1 second) flushed into a new immutable Lucene segment on disk, at which point the new data becomes available for querying.

3. Give three reasons sharding is essential for an ElasticSearch cluster.
It lets an index exceed the disk capacity of any single machine (e.g., storing 1 TB across smaller disks), parallelizes queries across multiple nodes for higher throughput/speed, and allows shard replication across nodes for high availability.