Why Replace Zookeeper with KRaft
ํ์ฌ์์ Confluent๋ฅผ ํตํด Kafka ํด๋ฌ์คํฐ๋ฅผ ์ ์ฌ์ฉํ๊ณ ์์์ต๋๋ค. ๊ทธ๋ฐ๋ฐ, ์ด ์นดํ์นด์ ๋ํด์ ์ข๋ ์์ธํ ์๊ณ , ์ ๋ฌธ์ฑ์ ๊ฐ์ถ๊ณ ์ถ๋ค๋ ์๊ฐ์ด ๋ค์ด์ 2025๋ ์ฒซ ๋ชฉํ๋ก Confluent์ Kafka ์๊ฒฉ์ฆ์ธ Confluent Certified Developer for Apache Kafkaยฎ ์๊ฒฉ์ฆ์ ์ค๋นํ๊ณ ์์ต๋๋ค โ๏ธ
๋ค์ด๊ฐ๋ฉฐ
CCDAK ์ํ์ ์ค๋นํ๋ฉด์, Confluent์์ ๋ฐํํ ๊ฐ์ข ๋ฌธ์๋ค์ ์ฝ์ด๋ณด๊ณ ์์ต๋๋ค. ์ด๋ฒ ํฌ์คํธ์์
โWhy ZooKeeper Was Replaced with KRaft โ The Log of All Logsโ
๋ผ๋ ์ํฐํด์ ์ฝ์ผ๋ฉด์, ๋ฉ๋ชจํ ๊ฒ๋ค์ ํฌ์คํธ๋ก ๊ธฐ๋ก ํฉ๋๋ค. ์ฐธ๊ณ ๋ก ์ด ํฌ์คํธ๋ ๊ฐ์ธ์ ์ธ ๋ฉ๋ชจ์ด๋ฏ๋ก ์ธํฐ๋ท์์ ๊ฒ์๋์ง ์์ต๋๋ค.
Introduction
์ Zookeeper์์ KRaft๋ก ๋ฐ๊พธ์๋๊ฐ?
์ Raft ์๊ณ ๋ฆฌ์ฆ์ ์ฑํ ํ๋๊ฐ?
2012๋ , kafka controller์ ๊ธฐ๋ฅ ์ถ๊ฐ? ์ด controller๋ topic partition log ๋ฟ๋ง ์๋๋ผ, cluster/broker metadata์ cluster-wide config, security credential ๋ฑ์ ๋ชจ๋ ์ ์ฅํ๋๋ก ๋์์ธ ํ๊ณ ์์๋ค?
zookeeper๋ SOT๋ก ์ฌ์ฉ ํ์.
๊ทธ๋ฐ๋ฐ, non-controller broker๋ zookeeper์ ์ ์ํด ISR ์ ๋ณด๋ ๋ฆฌ๋ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ ์ ์์์. ๊ทธ๋ผ controller๋ ZK์์ ๊ทธ ์ ๋ณด๋ฅผ ๋ฐ์์ผ ํ์.
๊ทธ๋์ ์๋๋ Controller๊ฐ ZK๋ฅผ SOT๋ก ์ฌ์ฉํ๋ฉด์ ํด๋ฌ์คํฐ ๊ด๋ฆฌ๋ฅผ ํ๋ ค๊ณ ํ๋๋ฐ, ์ด๋ ๊ฒ ํ๋ฉด ZK์ ์ํตํ๋ ๋ชจ๋ ์ปดํฌ๋ํธ๊ฐ controller๋ฅผ ๊ฑฐ์น์ง ์๊ณ ํด๋ฌ์คํฐ ์ ๋ณด๋ฅผ ์ ๋ํ๋ ์ผ์ด ์ผ์ด๋๊ฒ ๋จ.
์ด์ฐฝ๊ธฐ ์นดํ์นด๋ ์ปจ์๋จธ๋ ์ง์ ZK์ ํต์ ํ ์ ์์์. ๊ทธ๋ฐ๋ฐ, ์นดํ์นด๊ฐ ๋ฐ์ ํ๋ฉด์ ์ด๊ฑธ ๋ง๊ณ ์ปจ์๋จธ๋ ๋ธ๋ก์ปค๋ฅผ ํตํด ๊ฐ์ ์ ์ผ๋ก ๋ฆฌ๋ ์ ๋ณด๋ฅผ ์ ๋ํ๋๋ก ๋ณ๊ฒฝ ๋์ด ์์.
์ด๋ ๊ฒ ZK ์ง์ ์ ๊ทผ์ ๋ง๋ ์ด์ ๋ ZK์ ๋ํ R/W ๋ถํ๋ฅผ ์ค์ด๊ธฐ ์ํด์ ์์.
Zookeeper watcher
Q. โZooKeeper watcherโ ์ด๊ฒ ๋ญ์ง? ์ด๊ฒ ๊ณง Controller ๋ธ๋ก์ปค ์๋?
โwatcherโ๋ ZK ์์คํ ๋ด์์์ ๊ฐ๋ ์. ๊ตฌ์ฒด์ ์ผ๋ก๋ ZK์ โevent notificationโ ๋ฉ์ปค๋์ฆ์.
ZK watcher๋ Znode(๋ฐ์ดํฐ ๋ ธ๋)์ ๋ณํ๋ฅผ ๊ฐ์งํ๊ณ ZK ํด๋ผ์ด์ธํธ๊ฐ ์๋ฆผ์ ๋ฐ์ ์ ์๋ ๋ฉ์ปค๋์ฆ ์ ๋๋ค. ์นดํ์นด์์๋ ZK ํด๋ผ์ด์ธํธ๋ โ๋ธ๋ก์ปคโ ์ ๋๋ค.
Znode์ ๋ณํ๋ (1) ๋ธ๋ก์ปค๊ฐ ํด๋ฌ์คํฐ์ joinํ๊ฑฐ๋ leave ํ ๋, (2) ํ ํฝ ๋ฉํ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋ ๋, (3) ์ปจํธ๋กค๋ฌ ๋ธ๋ก์ปค๊ฐ ๋ณ๊ฒฝ๋ ๋ ์ ๋๋ค. ์ด๋ฐ ๋ณ๊ฒฝ ์ฌํญ์ ๋๋ถ๋ถ ์ปจํธ๋กค๋ฌ ๋ธ๋ก์ปค๊ฐ ๋ง๋ค๊ฒ ๋ฉ๋๋ค.
์นดํ์นด ๋ธ๋ก์ปค๋ Znode์ ๋ณํ๋ฅผ ๊ฐ์ง(watch)ํ๊ณ , ๋ณํ๊ฐ ์์ผ๋ฉด ๊ทธ ์ด๋ฒคํธ๋ฅผ ํ์ธํด ๋์ ํฉ๋๋ค.
Controller Scalability Limitaiton
๋, ํ์ฌ๋ ํ๋์ Controller ๋ธ๋ก์ปค๊ฐ ZK์ ์ ์ํ๋๋ก ํ๋ฉด์, ํด๋ฌ์คํฐ ๋๊ท๋ชจ๋ก ํ์ฅ๋๋ฉด ํด๋น Controller ๋ธ๋ก์ปค๊ฐ ํฐ ๋ถํ๋ฅผ ๋ฐ๊ฒ ๋จ.
Broker Shutdown
non-controller ๋ธ๋ก์ปค ํ๋๋ฅผ ๋ด๋ฆฌ๊ฒ ๋๋ฉด, ์ด ๋ธ๋ก์ปค๋ฅผ ๋ด๋ฆฌ๊ธฐ ์ํด์ broker->controller->ZK ์ผ๋ จ์ ๊ณผ์ ์ด ์ผ์ด๋์ผ ํ๊ณ , ๋ฐ๋๋ก ZK->controller->(other) borkers๋ก ์ ํ๊ฐ ์ผ์ด์๋ ํจ. ๊ทธ๋ฐ๋ฐ, ์ด ๊ณผ์ ์ ๊ฝค ๊ธด ์๊ฐ์ด ๊ฑธ๋ฆฌ๊ณ ํด๋ฌ์คํฐ์ ์ฆ๊ฐ์ ์ธ ๋ค์ด์ ์ด๋ ต๊ฒ ๋ง๋ค์์.
Controler Failover
์ปจํธ๋กค๋ฌ ๋ธ๋ก์ปค๋ ์ ง๋ค์ด ๋ ์ ์์.
์ด ์ํฉ์ด ๋ฐ์ํ๋ฉด, ๋๋จธ์ง ๋ชจ๋ ๋ธ๋ก์ปค๊ฐ ZK์ ํ์ ๋ณด๋ด๊ณ , ๊ทธ ์ค์์ ๊ฐ์ฅ ๋จผ์ ์๋ต์ ๋ฐ๋ ๋ ์์ด Controller ๋ธ๋ก์ปค๋ก ์๋ช ๋จ.
Controller๊ฐ ๋ ๋ธ๋ก์ปค๊ฐ ๊ฐ์ฅ ๋จผ์ ํ๋ ์ผ์ ZK์์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์. ๋ง์ฝ ๊ธฐ์กด ์ ๋ณด์ ๋ถ์ผ์น ํ๋ ์ ๋ณด๊ฐ ์๋ค๋ฉด, ๊ทธ๊ฑธ ํธ๋ค๋ง ํ๊ณ (=์ ๊ท ๋ฉํ ์ ๋ณด๋ก overwrite), ์ ๊ท ์ ๋ณด๋ฅผ ํ์ ๋ธ๋ก์ปค์ ์ ํํจ.
์ด ๊ณผ์ ์์์ ๋ณ๋ชฉ์ ์ ๊ท๋ก ์๋ช ๋ controller๊ฐ ZK์์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ fetching ํ๋ ๊ฒ์. ์ด ๊ณผ์ ์ topic partition์ด ๋ง์์๋ก ์ค๋๊ฑธ๋ฆผ. ์ด๋ฐ bootstraping ๊ณผ์ ๋์์ ์ปจํธ๋กค๋ฌ ๋ธ๋ก์ปค๊ฐ ์ด๋ค ์์ฒญ๋ ์ฒ๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ํด๋ฌ์คํฐ์ ์ผ๋ถ ๊ธฐ๋ฅ์ด ์ ๋๋ก ๋์ํ์ง ์๊ฒ ๋จ.
Why choose KRaft?
ZK์ ์ ์ฅํ๋ ์ ๋ณด๊ฐ ๋ญ์ง ์ ์๊ฐํด๋ณด๋, ๊ฒฐ๊ตญ์ ๋ฉํ๋ฐ์ดํฐ๊ฐ ์ด๋ป๊ฒ ๋ฐ๋๋์ง์ ๋ํ transaction log ์์.
๊ทธ๋ฆฌ๊ณ ์ด๋ฐ log ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋๋ฐ ๊ฐ์ฅ ์ข์ ๋ ์์ด ๋ฐ๋ก ์นดํ์นด ์์ ์!! ใ ใ
๊ทธ๋์ ์ด metadata log๋ฅผ ์ ์ฅํ๋ ๋ด๋ถ ํ ํฝ์ ๋๊ณ , controller๊ฐ ์ด ์ ๋ณด๋ฅผ โ์ง์ โ ๊ด๋ฆฌํ๋๋ก ํ๋๊ฒ ์ด๋จ๊น ํ๋ ์๊ฐ์ผ๋ก ์ด์ด์ง.
๊ทธ๋์ ๋ช๊ฐ์ ๋ธ๋ก์ปค๋ฅผ ๊ทธ๋ฃน์ผ๋ก ๋ฌถ์ด์ ์ด metadata log๋ฅผ ๊ด๋ฆฌํ๋๋ก ์ญํ ์ ๋ถ์ฌํจ. ๊ทธ๋ฆฌ๊ณ ์ด ๊ทธ๋ฃน์ quorum์ ์ด๋ฃธ. (์ด์ ์๋ ํ๋์ ์ปจํธ๋กค๋ฌ ์๋๋ฐ, ์ด์ quorum์ผ๋ก ์ด์!)
โPrimary-backupโ replication algorithm
- single leader replica takes all of the incoming writes
- and tries to replicate them to other replicas as its followers.
- after the followers have acks, the leader considers it committed and returns to writing ot its client. ^the metadata log also takes this procedure also!
โQuorumโ replication algorithm
- still a single leader trying to take writes, and replicating to followers.
- But, instead of waiting for all followers to ack
- it only waits for the majority of replicas, including itself. ^ ์ผ์ข ์ ๋ถ์ฐ ํ๊ฒฝ์์ ์ ๋ณด ์ ์ฅ์ ์ํด ์ฌ์ฉํ๋ consuensus algorithm ์.
primary-backup๊ณผ ๋น๊ตํด์ quorum ๋ฐฉ์์ ์ข๋ ๋น ๋ฅธ availability guaratee๋ฅผ ์ ๊ณตํจ.
Kraft
Kafka Raft Implementation
์๋ก์ด ์ปจํธ๋กค๋ฌ ๊ทธ๋ฃน์ metadata log์ ๋ํด์ ์ด quorum replication ๋ฐฉ์์ ์ฑํ ํจ.
๊ทธ๋ฐ๋ฐ, ์ด๊ฒ์ metadata log์ ๋ํด์ ZK์ ์ญํ ์ ๊ฐ์ ธ์จ ๊ฒ์. ๊ธฐ์กด์ ZK์์ ๋ด๋นํ๋ ๋ฆฌ๋ ์ปจํธ๋กค๋ฌ ์ ์ถ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด์์๋ ์ด๊ด์ด ํ์ํจ. ์ด ์ ์ถ ์๊ณ ๋ฆฌ์ฆ์ ZK์ ๋ณด์ฅํ๋, ๋์์ ์ฌ๋ฌ ๋ฆฌ๋๊ฐ ์กด์ฌํ๋ ์ํฉ์ ๋ฐฉ์งํ ์ ์์ด์ผ ํจ.
โgridlock scenarioโ = ๊ต์ฐฉ์ํ
- ๋ธ๋ก์ปค๊ฐ ํน์ ์กฐ๊ฑด์์ ์๋ก ๋๊ธฐํ๊ฑฐ๋ ์ถฉ๋ํ์ฌ ์๋ฌด๊ฒ๋ ์งํํ์ง ๋ชปํ๋ ์ํ์.
deadlock๊ณผ ๋น์ทํ ๊ฐ๋ ์ด๊ธด ํ๋ฐ, gridlock์ ๋ถ์ฐ ์์คํ ์ ๊ฐ์ฉ ์ํ์ ๋ํด ๋งํ ๋ ์ฐ๋ ๋ฏ?
Leader Election
๋ธ๋ก์ปค๋ 3๊ฐ์ง ์ญํ ๋ก ๋๋จ.
- voter
- ์ปจํธ๋กค๋ฌ ๊ทธ๋ฃน์ ์ํ๋ ๋ธ๋ก์ปค
- leader candidate
- ๋๋คํ๊ฒ ์ผ์ ์๊ฐ์ด ์ง๋๋ฉด voter ์ค ํ๋๊ฐ ๋ฆฌ๋ ํ๋ณด๊ฐ ๋จ.
- observer
- ์ปจํธ๋กค๋ฌ ๊ทธ๋ฃน์ ์ํ์ง ์๋ ์ผ๋ฐ ๋ธ๋ก์ปค
๋ง์ฝ ์ด๋ค ํ๋ณด๋ ๋ค์๊ฒฐ์ ์ป์ง ๋ชปํ๋ค๋ฉด, ํฌํ๋ฅผ ๋ฌดํจ๋ก ํ๊ณ epoch์ ๋๋ฆฐ ํ, ๋ค์ ์ ๊ฑฐ๋ฅผ ์งํํจ.
Log Replication
๊ธฐ์กด Raft ์๊ณ ๋ฆฌ์ฆ์ push-based๋ก log replication์ ์ํํจ. KRaft ์๊ณ ๋ฆฌ์ฆ์ ZK ๋์ฒ๋ผ pull-based๋ก log replication์ ์ํํจ.
we can see that this fetch request is also leveraged as a heartbeat to determine the liveness of the leader.
Other voters within the quorum actively replicate the metadata log so that newly appended records get committed.
Quorum Controller
The quorum controller receives the linveness of all registered brokers with heartbeats.
When an existing broker is shutting down, it can piggyback its intention within a heartbeat and the controller can remove it from all of its partitions.
Quorum controlller batch all of the partition movement events when appending them to the metadata log.
๋งบ์๋ง
KRaft ๋ชจ๋์ ๋ํ ์ญ์ฌ์ ๋์ ๋ฐฉ์ ๊ทธ๋ฆฌ๊ณ ์คํ ๊ฒฐ๊ณผ๊น์ง ์์ฃผ ์์ฐฌ ํฌ์คํธ ์์ต๋๋ค! ์ฃผ๋ง์ ์ฌ์ ๋กญ๊ฒ ์นดํ์์ ์ฝ๋๋ฐ ์ ์ ์ฝํ๋๋ผ๊ตฌ์ ใ ใ
Raft๊ฐ push-based ์๋ค๋ฉด, KRaft๋ pull-based๋ก ๋์ํ๋ค๋ ๊ฒ๋ ์ด ํฌ์คํธ๋ฅผ ์ฝ์ผ๋ฉด์ ์ฒ์ ์์๋ค์ ^^
์ํฐํด์ ์๋ ํํ ์ค์ โnot reinventing the wheelโ
๊ฐ ์ธ์์ ์
๋๋ค. ์นดํ์นด ๊ฐ๋ฐ์๋ค์ด ๊ธฐ์กด ์ฝ๋์ ์์คํ
์ ์ต๋ํ ํ์ฉํ๋ ๋ฐฉํฅ์ ์ ํํ๋ค๋ ๊ฒ์ด ์ฐธ ๋๋ํ ๊ฒ ๊ฐ์ต๋๋ค.
โnot reinventing the wheelโ
์ ์ฑ
์ ๋๊ท๋ชจ ์คํ์์ค๋ฅผ ์ด์ํ๊ณ ๊ด๋ฆฌํ๋ ๋ฐ์ ์ข์ ๊ธธ์ก์ด๊ฐ ๋์ด์ค ๊ฒ ๊ฐ์ต๋๋ค. ๋ฌผ๋ก ์ด๋ค ์๊ฐ์๋ ์์ ํ ์๋ก์ด ๋๊ตฌ๋ฅผ ๊ฐ๋ฐํด์ผ ํ ์๋ ์๊ฒ ์ง๋ง, ๊ทธ๊ฒ์ด ๋ฐํด(wheel)๊ฐ ์๋ ๋ค๋ฅธ ๊ฒ(์๋ฅผ ๋ค๋ฉด, ๋ฐํด๋ฅผ ํ์ฉํ๋ ์์ ๊ฑฐ๋ผ๋ ์์คํ
?)์ธ์ง ๋น ๋ฅด๊ฒ ํ๋จํ๊ณ ๊ฐ๋ฐ ๋ฐฉํฅ์ ์ ํ ์ ์์ด์ผ ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.