Notion-powered devlog

Jade Devlog

Jade의 기술 블로그

DAY-3 - Index, Shard, Segment의 관계

오늘의 주제

  1. 학습 목표
    1. OpenSearch의 논리적 index가 shard와 Lucene segment로 나뉘는 구조를 이해하고 검색·색인·병합 시 각 계층의 역할을 설명한다.

학습 전 기반 지식

  1. Cluster와 Node
    1. Cluster는 함께 동작하는 node의 집합이다. 요청을 받은 coordinating node는 대상 shard로 요청을 분산하고 결과를 모아 응답한다. node 수와 shard 수는 같지 않으며 한 node가 여러 shard copy를 가질 수 있다.
  1. Routing
    1. document ID 또는 custom routing 값을 hash해 어느 primary shard에 저장할지 결정한다. 같은 routing 값은 같은 shard로 모을 수 있지만 특정 값에 traffic과 데이터가 집중되면 hotspot이 생긴다.
  1. Lucene
    1. 각 OpenSearch shard는 독립적인 Lucene index다. 따라서 shard 하나도 자체 inverted index와 segment를 가지며 CPU, heap, file descriptor 등의 overhead를 만든다.

저장 구조

flowchart TD A[Index: 논리적 문서 집합] --> B[Primary Shard: routing·쓰기 단위] B --> C[Replica Shard: primary의 복제본] B --> D[Lucene Segment: immutable 검색 구조] C --> E[Replica의 Lucene Segment]

구조 단계별 핵심 지식

  1. Index: 논리적 문서 집합
    1. mapping, analyzer, shard·replica 설정을 공유하는 검색 단위다. client는 보통 index 이름이나 alias로 요청한다.
  1. Primary Shard: routing·쓰기 단위
    1. 각 document의 원본 shard copy다. 쓰기는 routing으로 선택된 primary에 적용된 뒤 replication 정책에 따라 replica로 전달된다.
  1. Replica Shard: primary의 복제본
    1. primary 장애 시 승격될 수 있고 search 요청을 처리할 수 있다. 같은 shard의 primary와 replica는 같은 node에 배치되지 않는다.
  1. Lucene Segment: immutable 검색 구조
    1. 새 document가 검색 가능해질 때 새로운 segment가 만들어진다. 기존 segment를 제자리에서 수정하기보다 삭제 표시와 새 document를 사용하며, background merge가 작은 segment를 큰 segment로 합친다.
  1. Replica의 Lucene Segment
    1. 복제 방식에 따라 replica가 document 연산을 적용하거나 segment를 전달받는다. 어느 방식이든 replica 수가 늘면 저장 공간과 복제 비용이 증가한다.

검색과 색인 흐름

  1. 색인: coordinating node → routing 계산 → primary shard → replica shard.
  1. refresh: 메모리의 변경이 검색 가능한 segment로 전환된다.
  1. 검색 query phase: 관련 shard copy가 local top results를 계산한다.
  1. fetch phase: coordinating node가 전역 순위를 정하고 선택된 document를 가져온다.
  1. merge: background에서 segment를 병합하고 삭제된 document 공간을 회수한다.

단계별 실습

  1. 2 primary, 1 replica index를 만든다. 단일 node에서는 replica가 할당되지 않아 yellow가 정상이다.
    1. OS=http://localhost:9200 curl -s -X PUT "$OS/shard-lab" -H 'Content-Type: application/json' -d ' {"settings":{"number_of_shards":2,"number_of_replicas":1}}' curl -s "$OS/_cluster/health/shard-lab?pretty" curl -s "$OS/_cat/shards/shard-lab?v"
  1. routing 결과와 segment 정보를 관찰한다.
    1. for id in 1 2 3 4 5; do curl -s -X PUT "$OS/shard-lab/_doc/$id" \ -H 'Content-Type: application/json' -d "{\"message\":\"document $id\"}" done curl -s -X POST "$OS/shard-lab/_refresh" curl -s "$OS/_cat/shards/shard-lab?v" curl -s "$OS/_cat/segments/shard-lab?v"

관찰하고 설명할 내용

  1. index 하나가 여러 primary shard로 나뉘고 각 shard에 segment가 존재하는지 확인한다.
  1. single node에서 replica가 UNASSIGNED인 이유와 cluster가 yellow인 이유를 설명한다.
  1. document 수가 shard에 완전히 균등하지 않을 수 있는 이유를 hash routing 관점에서 설명한다.

문제 상황과 해결 관점

  1. Oversharding: 작은 shard가 지나치게 많으면 heap, cluster state, thread scheduling 비용이 커진다.
  1. 너무 큰 shard: recovery와 relocation 시간이 길어지고 한 shard가 처리할 수 있는 병렬성의 한계가 생긴다.
  1. 강제 merge 남용: merge는 I/O가 큰 작업이다. 활발히 쓰는 index에서 무조건 실행하지 말고 lifecycle과 성능 목적을 확인한다.

정리와 cleanup

curl -s -X DELETE "$OS/shard-lab"

완료 기준

  1. Index → Shard → Segment 관계를 그림 없이 설명할 수 있다.
  1. _cat/shards와 _cat/segments 결과를 읽을 수 있다.
  1. shard가 너무 많거나 너무 클 때의 trade-off를 설명할 수 있다.

공식 자료

  1. https://docs.opensearch.org/latest/getting-started/intro/
  1. https://docs.opensearch.org/latest/api-reference/cat/cat-shards/
  1. https://docs.opensearch.org/latest/api-reference/cat/cat-segments/