기존 topic의 replication factor 값 변경하기

김소은·2026년 2월 4일

핵심부터 말할게.

Kafka에서 기존 토픽의 --replication-factor는 직접 변경 ❌
대신 “파티션 reassignment(재할당)”으로 복제본 수를 늘리거나 줄인다 ⭕

토픽 삭제 → 같은 이름으로 재생성은 가능하지만 데이터는 전부 날아감.

아래를 보면 “정석 루트”랑 “테스트용 지름길”이 딱 구분될 거야.


1️⃣ 왜 --replication-factor를 바로 못 바꾸나?

  • replication factor는 토픽 생성 시 메타데이터 구조
  • Kafka는 파티션 단위로 replica 목록을 관리
  • 그래서 변경은 항상 partition reassignment 형태로만 가능

2️⃣ 정석: 기존 토픽의 replication factor 변경 (데이터 유지)

예시 상황

  • 현재: partitions=3, RF=1
  • 목표: partitions=3, RF=3
  • 브로커: 3대

Step 1) 현재 상태 확인

bin/kafka-topics.sh \
  --bootstrap-server localhost:9192 \
  --describe \
  --topic test

Step 2) reassignment JSON 생성 (핵심)

reassign.json 예시:

{
  "version": 1,
  "partitions": [
    { "topic": "test", "partition": 0, "replicas": [1,2,3] },
    { "topic": "test", "partition": 1, "replicas": [2,3,1] },
    { "topic": "test", "partition": 2, "replicas": [3,1,2] }
  ]
}

⚠️ 주의

  • replicas 길이 = 원하는 replication factor
  • replicas의 broker id는 broker 파일 내의 node.id 기준
  • 파티션마다 순서를 조금씩 섞는 게 일반적

Step 3) reassignment 실행

bin/kafka-reassign-partitions.sh \
  --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
  --reassignment-json-file reassign.json \
  --execute

Step 4) 진행 상황 확인

bin/kafka-reassign-partitions.sh \
  --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
  --reassignment-json-file reassign.json \
  --verify

Step 5) 결과 확인

bin/kafka-topics.sh \
  --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
  --describe \
  --topic test

Replicas / Isr 가 3개로 나오면 성공.


3️⃣ Kafka 4.x 보너스: 자동 생성도 가능

(환경에 따라 지원)

bin/kafka-reassign-partitions.sh \
  --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
  --generate \
  --topics-to-move-json-file topics.json \
  --broker-list "1,2,3"

topics.json:

{ "topics": [{ "topic": "test" }], "version": 1 }

4️⃣ 테스트/학습용: 토픽 삭제 → 재생성 (데이터 포기)

가능 조건

  • delete.topic.enable=true
  • 토픽에 중요 데이터 없음

삭제

bin/kafka-topics.sh \
  --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
  --delete \
  --topic test

(완전 삭제까지 수 초~수십 초 걸릴 수 있음)

재생성

bin/kafka-topics.sh \
  --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
  --create \
  --topic test \
  --partitions 3 \
  --replication-factor 3

5️⃣ 언제 어떤 방법을 써야 하냐 (결론)

상황추천
실서비스 / 데이터 유지✅ reassignment
로컬 테스트 / 실험⚠️ 삭제 후 재생성
RF 줄이기reassignment (주의)
컨트롤러/브로커 실습reassignment 경험 꼭 해볼 것

⚠️ 실수 많이 하는 포인트

  • broker id ≠ 포트 번호
  • RF > 브로커 수 ❌
  • reassignment 중 ISR 일시 감소는 정상
  • 컨트롤러(quorum) 살아있어야 함

다음으로 도움될 것

  • reassignment 중 리더가 바뀌는 타이밍
  • RF=3 + min.insync.replicas=2 실험
  • reassignment 자동 생성 JSON 검증법

원하면 네 현재 test 토픽 --describe 출력 그대로 붙여줘.
브로커 id 기준으로 딱 맞는 reassignment JSON 바로 만들어줄게.

profile
개발자

0개의 댓글