Skip to main content
โšก Calmops

OpenSearch Internals: Lucene, Sharding, and Replication

Introduction

Understanding OpenSearch’s internal architecture helps you optimize queries and troubleshoot issues. This article explores how OpenSearch achieves distributed search and analytics.


Apache Lucene Foundation

OpenSearch is built on Apache Lucene:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           OpenSearch                      โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚       REST API Layer               โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚    Cluster Management              โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚       Lucene Library                โ”‚  โ”‚
โ”‚  โ”‚  - IndexWriter                    โ”‚  โ”‚
โ”‚  โ”‚  - IndexReader                    โ”‚  โ”‚
โ”‚  โ”‚  - Searcher                       โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Segment-Based Storage

Index Structure

Index (shard)
  โ”œโ”€โ”€ _0.segment
  โ”‚   โ”œโ”€โ”€ _0.si    (Segment Info)
  โ”‚   โ”œโ”€โ”€ _0.fdm   (Field Metadata)
  โ”‚   โ”œโ”€โ”€ _0.fdt   (Field Data)
  โ”‚   โ”œโ”€โ”€ _0.fdx   (Field Index)
  โ”‚   โ”œโ”€โ”€ _0.tmd   (Term Metadata)
  โ”‚   โ”œโ”€โ”€ _0.tvd   (Term Vector Data)
  โ”‚   โ”œโ”€โ”€ _0.tvx   (Term Vector Index)
  โ”‚   โ””โ”€โ”€ _0.nvd   (Norm Data)
  โ”œโ”€โ”€ _1.segment
  โ””โ”€โ”€ _2.segment

Document Addition

# When you index a document:
# 1. Added to in-memory buffer
# 2. Written to translog
# 3. Periodically flushed to segment

POST /products/_doc
{
  "name": "Mouse",
  "price": 29.99
}

Segment Merging

# Force merge
POST /products/_forcemerge
{
  "max_num_segments": 1
}

Sharding Strategy

Primary and Replica Shards

# Index with shards
PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}

# Shard distribution:
# Primary: 0, 1, 2
# Replicas: 0, 1, 2 (2 copies each)

Shard Routing

# Document routing
POST /products/_doc
{
  "name": "Mouse"
}

# Uses: hash(_id) % num_primary_shards

Replication

Primary-Replica Flow

Write Request
    โ”‚
    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Primary Shard   โ”‚โ”€โ”€โ–บ In-memory buffer
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚
    โ–ผ (parallel)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Replica 1     โ”‚โ”€โ”€โ–บ Translog
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Replica 2     โ”‚โ”€โ”€โ–บ Translog
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Read Request

# Coordinating node routes to:
# 1. One primary + all replicas
# 2. Waits for responses
# 3. Returns to client

Refresh Interval

# Default refresh: 1 second
PUT /products
{
  "settings": {
    "refresh_interval": "1s"
  }
}

# Make visible for search
POST /products/_refresh

# Disable auto-refresh
PUT /products
{
  "settings": {
    "refresh_interval": "-1"
  }
}

Translog

# Translog provides durability
PUT /products
{
  "settings": {
    "translog": {
      "sync_interval": "5s",
      "size": "5mb"
    }
  }
}

Query Execution

Query Phase

# 1. Coordinator receives request
# 2. Broadcasts to all shards
# 3. Each shard returns top-N results
# 4. Coordinator merges and returns

Search Flow

Search Request
    โ”‚
    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Query Parsing      โ”‚โ”€โ”€โ–บ Parse DSL
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚
    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Query Execution    โ”‚โ”€โ”€โ–บ Execute on shards
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚
    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Reduce Phase       โ”‚โ”€โ”€โ–บ Merge results
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚
    โ–ผ
Response

Caching

Query Cache

# Enabled by default for filter queries
# Caches results per segment

# Clear cache
POST /products/_cache/clear

Field Data Cache

# Used for aggregations, sorts
# Loading costs memory

# Monitor
GET /products/_stats

Conclusion

Understanding OpenSearch internalsโ€”Lucene segments, sharding, and replicationโ€”helps you design better indexes and troubleshoot performance issues.

Comments