Learn Labs
Foundations

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:

Partition: 0the partition numberLeader: 1broker 1 handles all reads and writes for itReplicas: 1,2,3brokers holding a copy; the first is the "preferred" leaderIsr: 1,2,3in-sync replicas: copies that are fully caught upElr:eligible leader replicas (KIP-966), used after failures

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.

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 id

Every 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: 1

Lab

  1. Create demo as above and describe it. Which broker leads each partition?
  2. Run kafka-metadata-quorum.sh ... describe --replication to see each controller's lag on the metadata log.
  3. List /var/lib/kafka/data on kafka-2. Are the same partition directories there? Why?
  4. Open http://localhost:8086 and find the same information in the UI.

On this page