This article first appeared on AlgoSimplified. That URL is canonical. The full post has the architecture diagram, the combined-mode config, sizing notes and a longer FAQ. This is a shorter cut of the same article.
The related course lesson is Kafka Fundamentals and Architecture.
A Kafka cluster is a set of brokers that store topics. Each topic is split into partitions, every partition is copied to a few brokers, and a controller quorum tracks which replica leads each partition. Producers append to partition leaders; consumers read from them in consumer groups. Ordering, scaling, failover and delivery guarantees all follow from those pieces.
Every command output below was copied from a real three-node Kafka 4.4 cluster. The run includes stopping one broker to see what happens on failover.
The pieces
A diagram of this layout is on the original post. Read it left to right:
- Producers decide which partition each record goes to (by hashing the key) and send it to that partition's leader.
- Brokers store partitions as append-only logs on disk. Each partition has one leader and some followers that copy it.
- The controller quorum stores the cluster's metadata (which brokers are alive, which replica leads each partition) and tells brokers about changes.
- Consumers in a group split the partitions between them and read each one in order, tracking their position with offsets.
A broker is one Kafka server process. A cluster is a set of brokers that share the same metadata. Clients only need one or two broker addresses (bootstrap.servers). They fetch the full cluster metadata from there and then talk directly to whichever broker leads each partition they need. There is no central proxy in the data path, which is one reason Kafka scales.
Topics, partitions and offsets
A topic is a named stream of records, like orders or payments. A topic is split into partitions, and each partition is an ordered, append-only log. Each record in a partition gets a sequential offset: 0, 1, 2 and so on.
Two consequences matter in practice:
- Ordering is per partition, not per topic. Records with the same key land in the same partition, so all events for one key stay in order. There is no global order across partitions.
- Partitions are the unit of parallelism. One partition is read by at most one consumer in a group, so a topic with 6 partitions can keep at most 6 consumers in one group busy.
The same rule on the test cluster: seven records with four different keys, written to a 3-partition topic.
$ printf 'alice:p1\nbob:p2\ncarol:p3\ndave:p4\nalice:p5\nbob:p6\nalice:p7\n' | \ kafka-console-producer.sh --bootstrap-server localhost:9092 --topic payments \ --reader-property parse.key=true --reader-property key.separator=: $ kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic payments \ --from-beginning --group billing \ --formatter-property print.partition=true --formatter-property print.offset=true \ --formatter-property print.key=true Partition:0 Offset:0 alice p1 Partition:0 Offset:1 bob p2 Partition:0 Offset:2 alice p5 Partition:0 Offset:3 bob p6 Partition:0 Offset:4 alice p7 Partition:1 Offset:0 dave p4 Partition:2 Offset:0 carol p3 Every alice record went to partition 0, in the order it was sent; offsets restart at 0 in each partition. Notice too that alice and bob share a partition: hashing spreads keys, but it does not guarantee an even spread with only a few keys. Records without a key are spread by the producer's sticky partitioner, which fills a batch for one partition before moving to the next.
Replication, ISR and acks
Each partition is stored on replication.factor brokers. One replica is the leader: all writes go to it, and by default all reads too. The others are followers that continuously fetch from the leader.
The in-sync replicas (ISR) are the replicas that are caught up with the leader. A follower that falls behind for longer than replica.lag.time.max.ms (30 seconds by default) drops out. Only an in-sync replica can be elected leader without losing acknowledged data.
This is the orders topic on the test cluster, created with 3 partitions and replication factor 3:
Topic: orders TopicId: 4B3TSKlDRtCdKi47DG0fwg PartitionCount: 3 ReplicationFactor: 3 Configs: min.insync.replicas=2 Topic: orders Partition: 0 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1 Elr: LastKnownElr: Topic: orders Partition: 1 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2 Elr: LastKnownElr: Topic: orders Partition: 2 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3 Elr: LastKnownElr: Leadership is spread so each broker leads one partition here. All three replicas are in sync. Elr (eligible leader replicas, KIP-966) is empty because nothing has failed yet.
The producer's acks setting decides when a write counts as successful: 0 means the producer does not wait, 1 means the leader has written it, all means every replica in the ISR has it. acks=all on its own is not enough: if the ISR shrinks to just the leader, "all in-sync replicas" means one copy. That is what min.insync.replicas is for. With replication factor 3 and min.insync.replicas=2, an acks=all write needs at least two copies, so the topic keeps accepting writes with one broker down and rejects them (with NotEnoughReplicas) if two are down. Since Kafka 3.0, producers default to acks=all with idempotence on.
Consumers can only read records below the high watermark — the offset up to which every ISR member has the data — which is why a record acknowledged with acks=all survives a leader failure.
The controller and KRaft
Something has to decide which replica leads each partition, notice when a broker dies, and keep the topic list. That is the controller.
Older Kafka used ZooKeeper for this. Since Kafka 4.0, ZooKeeper is gone entirely and every cluster runs in KRaft mode: a small group of controller nodes (usually 3, or 5 for very large clusters) runs Raft over an internal log called __cluster_metadata. One controller is active; the others are hot standbys. On the test cluster:
ClusterId: hzBgkkuDRbyx9OM67laWUA LeaderId: 2 LeaderEpoch: 1 HighWatermark: 142 MaxFollowerLag: 0 MaxFollowerLagTimeMs: 176 CurrentVoters: [{"id": 1, "endpoints": ["CONTROLLER://localhost:19091"]}, {"id": 2, "endpoints": ["CONTROLLER://localhost:19092"]}, {"id": 3, "endpoints": ["CONTROLLER://localhost:19093"]}] CurrentObservers: [] LeaderId: 2 is the active controller. Raft needs a majority of voters, so 3 controllers tolerate 1 failure and 5 tolerate 2. In production, controllers usually run as separate processes from brokers. The test cluster used combined mode (process.roles=broker,controller), which is fine for development. The original post has the full properties file.
Write path and read path
On send(), the record is serialized and assigned a partition (by key hash, or by the sticky partitioner if it has no key), added to an in-memory batch, and sent to the partition leader. Followers fetch the new records. Once every ISR member has them, they sit below the high watermark and the leader replies to an acks=all producer.
Consumers pull. Kafka does not delete records when they are read, so many independent applications can read the same topic, each at its own pace. A consumer group shares a group.id; Kafka assigns each partition to exactly one member. If a consumer joins or dies, partitions are reassigned (a rebalance). Lag — LOG-END-OFFSET minus CURRENT-OFFSET — is the single most useful Kafka health metric.
Kafka 4.0 also made the new consumer rebalance protocol (KIP-848) generally available. Clients opt in with group.protocol=consumer, and the broker then computes assignments incrementally, so a rebalance no longer pauses the whole group.
What happens when a broker fails
On the test cluster, broker 2 was both the leader of orders-0 and the active controller. About 15 seconds after stopping it:
Topic: orders TopicId: 4B3TSKlDRtCdKi47DG0fwg PartitionCount: 3 ReplicationFactor: 3 Configs: min.insync.replicas=2 Topic: orders Partition: 0 Leader: 3 Replicas: 2,3,1 Isr: 3,1 Elr: LastKnownElr: Topic: orders Partition: 1 Leader: 3 Replicas: 3,1,2 Isr: 3,1 Elr: LastKnownElr: Topic: orders Partition: 2 Leader: 1 Replicas: 1,2,3 Isr: 1,3 Elr: LastKnownElr: LeaderId: 1 LeaderEpoch: 2 Three things happened automatically:
- Controller failover. The two remaining controllers elected node 1 as the new active controller (epoch 1 → 2).
- Partition leader election. The new controller noticed broker 2 had stopped heartbeating and made broker 3, the next in-sync replica, leader of
orders-0. - ISR shrink. Broker 2 was removed from every ISR.
Writes kept working: an acks=all produce succeeded because two in-sync replicas still met min.insync.replicas=2. Clients found the new leader on their own by refreshing metadata.
After restarting broker 2, it caught up and rejoined every ISR within about 20 seconds. Broker 3 still led partition 0 for a while; Kafka moves leadership back to the preferred replica on its own a few minutes later (auto.leader.rebalance.enable, checked every 5 minutes by default). A later --describe on the test cluster showed Partition: 0 Leader: 2 again.
A few points that are easy to miss
- Kafka 4.0 removed ZooKeeper support. Older clusters must migrate to KRaft on a 3.x release before upgrading to 4.x.
- Order is guaranteed within a partition, not across partitions. Give related events the same key.
- Brokers hold partition data and serve clients. Controllers hold cluster metadata and run leader elections. A node can be either, or both in combined mode.
The full architecture article has the diagram, the combined-mode config, and the sizing table. From there: the free Kafka Mastery course, starting with fundamentals, plus partitions and consumer lag.