Architecture
Brokers, topics, partitions, replicas, and the KRaft controller quorum
Five concepts explain almost everything Kafka does: brokers, topics, partitions, replicas, and the controller. The book's Kafka Internals chapter goes deep; this lesson maps them onto the running cluster.
Topics and partitions
A topic is a named stream of records, like orders. It's split into
partitions, and each partition is an independent, ordered,
append-only log. A record's position in its partition is its offset.
K kafka-topics.sh --bootstrap-server localhost:19092 \
--create --topic demo --partitions 3 --replication-factor 3
K kafka-topics.sh --bootstrap-server localhost:19092 --describe --topic demo
Topic: demo PartitionCount: 3 ReplicationFactor: 3 Configs: min.insync.replicas=2,...
Topic: demo Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3 Elr: LastKnownElr:
Topic: demo Partition: 1 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1 Elr: LastKnownElr:
Topic: demo Partition: 2 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2 Elr: LastKnownElr:Reading one row:
Ordering is guaranteed only within a partition. Records in partition 0 and partition 1 have no defined order relative to each other. That's the trade Kafka makes for parallelism, and why record keys matter: records with the same key always go to the same partition, so they stay in order. See Producer Basics.
Brokers
A broker is a Kafka server process. It stores partition replicas on disk, serves produce and fetch requests for the partitions it leads, and replicates partitions it follows. Leadership is spread across brokers so the load is too: in the output above, each broker leads one partition.
Replication
Each partition has one leader and some followers. Producers write to the leader; followers fetch from it, just like consumers do, and append the same records to their copy. A follower that's caught up is in sync (in the ISR). If the leader's broker dies, the controller promotes an in-sync follower. Full detail in Replication & ISR.
The controller and KRaft
Someone has to decide which broker leads each partition, track which
brokers are alive, and store topic configuration. That's the
controller. In Kafka 4, the controllers form a Raft quorum (KRaft)
and keep all cluster metadata in an internal, replicated log,
__cluster_metadata. One controller is the active leader at a time.
K kafka-metadata-quorum.sh --bootstrap-server localhost:19092 describe --status
ClusterId: 4L6g3nShT-eMCtK--X86sw
LeaderId: 3 # the active controller
LeaderEpoch: 4
HighWatermark: 425 # metadata log position
CurrentVoters: [{"id": 1, ...}, {"id": 2, ...}, {"id": 3, ...}]In this lab every node is both broker and controller
(process.roles=broker,controller). Production clusters usually run 3 or
5 dedicated controller nodes, so a busy broker can't slow down
metadata decisions.
A Raft quorum needs a majority to make decisions: 2 of 3 controllers.
When this module's lab stopped two of the three nodes, the remaining
broker kept serving the partitions it already led, but the quorum was
gone. No new leader elections, no topic changes, and
kafka-metadata-quorum.sh simply hung. Losing a majority of controllers
freezes the cluster's ability to react to anything else that goes wrong.
What's on disk
docker exec kafka-1 ls /var/lib/kafka/data
__cluster_metadata-0 # the KRaft metadata log
__consumer_offsets-0 ... 49 # committed consumer offsets (an internal compacted topic)
demo-0 demo-1 demo-2 # one directory per partition replica on this broker
meta.properties # this node's id and the cluster idEvery replica is a directory of segment files, which the next chapter, Log Segments, opens up.
Features
Kafka 4 tracks cluster capabilities as versioned features, upgraded explicitly after every broker runs a new version:
K kafka-features.sh --bootstrap-server localhost:19092 describe
Feature: metadata.version FinalizedVersionLevel: 4.3-IV0
Feature: group.version FinalizedVersionLevel: 1 # KIP-848 consumer protocol
Feature: share.version FinalizedVersionLevel: 1 # share groups
Feature: transaction.version FinalizedVersionLevel: 2
Feature: eligible.leader.replicas.version FinalizedVersionLevel: 1Lab
- Create
demoas above and describe it. Which broker leads each partition? - Run
kafka-metadata-quorum.sh ... describe --replicationto see each controller's lag on the metadata log. - List
/var/lib/kafka/dataonkafka-2. Are the same partition directories there? Why? - Open http://localhost:8086 and find the same information in the UI.