Lesson 01 / 10 · Foundation

Kafka Fundamentals and Architecture

Kafka fundamentals: topics, partitions, offsets, brokers, replication and ISR, and how the pieces fit together in a real-time streaming cluster.

2-3 hours beginner Not completed

On this page · 11 sections
  1. What Kafka actually is
  2. Core Data Structure: Topics and Partitions
  3. Brokers, leaders and replicas
  4. ISR, acks and min.insync.replicas
  5. Worked example: kill two brokers out of three
  6. Common mistakes
  7. ZooKeeper vs KRaft
  8. When to Use Kafka
  9. Hands-on exercise: partitions and ordering
  10. Key Takeaways
  11. Quiz

What Kafka actually is

The first thing to unlearn: Kafka is not a message queue.

It looks like one: messages go in, messages come out. But treating it as a queue leads to the wrong partition counts, the wrong retention and surprised consumers.

Kafka is a distributed append-only log. Think of it like an ever-growing spreadsheet where:

  • New rows are always added at the bottom (append-only)
  • Old rows never change (immutable)
  • Multiple people can read any row they want, anytime (replayable)
  • The spreadsheet is split across multiple files (distributed)

Core Data Structure: Topics and Partitions

Imagine you’re organizing a library:

  • Topic = Subject category (e.g., “user-events”, “payment-transactions”)
  • Partition = Physical shelf within that category
  • Message = Individual book on a shelf
  • Offset = Page number that never changes

Example Topic Structure

Topic: user-clicks
├── Partition 0: [msg-0, msg-1, msg-2, msg-3, ...]
├── Partition 1: [msg-0, msg-1, msg-2, ...]
└── Partition 2: [msg-0, msg-1, msg-2, ...]

Each partition is an independent, ordered sequence. Message 0 in Partition 0 has nothing to do with Message 0 in Partition 1.

Key insight: Ordering is only guaranteed within a partition, not across partitions. Kafka partitions explained shows how keys map to partitions and why adding partitions later reorders keyed data.

Brokers, leaders and replicas

A Kafka cluster is a set of servers called brokers. Each partition is stored on several brokers; the number of copies is the topic’s replication factor (RF). With RF=3 and three brokers, every broker holds a copy of every partition.

For each partition, one of those copies is the leader. Producers write to the leader and, by default, consumers read from it. The other copies are followers: they fetch from the leader and append the same records in the same order. Leadership is spread out, so in a healthy 3-broker cluster each broker leads about a third of the partitions.

ISR, acks and min.insync.replicas

The ISR (in-sync replicas) is the set of copies that are caught up with the leader, leader included. A follower drops out of the ISR if it hasn’t caught up for replica.lag.time.max.ms (30 seconds by default), or immediately if its broker dies.

Two settings decide what “my write is safe” means. They live on different sides:

SettingSet onWhat it does
acksproduceracks=all: leader replies only after every ISR member has the record. acks=1: leader replies as soon as it has written the record itself. Default is all since Kafka 3.0.
min.insync.replicastopic or brokerThe smallest ISR an acks=all write is allowed to succeed with. Default is 1, which is too low for anything you care about.
# topic config
min.insync.replicas=2

With RF=3 and min.insync.replicas=2, an acks=all write needs at least two copies. You can lose one broker and keep writing; lose two and acks=all writes are refused rather than silently landing on a single disk.

Producers covers what the producer does with these errors: retries, idempotence and batching.

The high watermark ties it together: it is the offset up to which every ISR member has the data. Consumers only see records below the high watermark, so they never read something that a leader failover could take away.

Worked example: kill two brokers out of three

This is the scenario every new Kafka user should run once. I ran it on 24 Nov 2026 with Kafka 4.4.0: one KRaft controller and three brokers on one laptop (script: course-code/kafka-mastery/three_broker_isr.sh in the repo).

1. Healthy cluster. Create a topic with 3 partitions, RF=3, min.insync.replicas=2, and write 5 records with acks=all:

bin/kafka-topics.sh --bootstrap-server localhost:29091 --create --topic payments \
  --partitions 3 --replication-factor 3 --config min.insync.replicas=2
bin/kafka-topics.sh --bootstrap-server localhost:29091 --describe --topic payments
Topic: payments	Partition: 0	Leader: 1	Replicas: 1,2,3	Isr: 1,2,3	Elr: 	LastKnownElr: 
Topic: payments	Partition: 1	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1	Elr: 	LastKnownElr: 
Topic: payments	Partition: 2	Leader: 3	Replicas: 3,1,2	Isr: 3,1,2	Elr: 	LastKnownElr: 

Each broker leads one partition (which broker leads which changes from run to run; the pattern below doesn’t). The first broker in Replicas is the preferred leader. The 5 unkeyed records all went to partition 2 (payments:2:5 from kafka-get-offsets.sh): the default partitioner sticks to one partition per batch for unkeyed records, so a small burst lands in one place.

2. Kill broker 3 with kill -9, the closest you get to pulling the power cable. About 9 seconds later (broker.session.timeout.ms, 9 s by default) the controller fences it:

Topic: payments	Partition: 0	Leader: 1	Replicas: 1,2,3	Isr: 1,2	Elr: 	LastKnownElr: 
Topic: payments	Partition: 1	Leader: 2	Replicas: 2,3,1	Isr: 2,1	Elr: 	LastKnownElr: 
Topic: payments	Partition: 2	Leader: 1	Replicas: 3,1,2	Isr: 1,2	Elr: 	LastKnownElr: 

Partition 2 lost its leader, and broker 1 took over. Broker 3 left every ISR. An acks=all write of pay-6 still succeeded, because two in-sync copies is exactly the minimum. No producer config changed and no data moved: the follower already had the data.

3. Kill broker 2 as well. Now one broker holds everything:

Topic: payments	Partition: 0	Leader: 1	Replicas: 1,2,3	Isr: 1	Elr: 2	LastKnownElr: 
Topic: payments	Partition: 1	Leader: 1	Replicas: 2,3,1	Isr: 1	Elr: 2	LastKnownElr: 
Topic: payments	Partition: 2	Leader: 1	Replicas: 3,1,2	Isr: 1	Elr: 2	LastKnownElr: 

An acks=all write fails:

ERROR Error when sending message to topic payments with key: null, value: 5 bytes with error:
org.apache.kafka.common.errors.NotEnoughReplicasException: Messages are rejected since there are fewer in-sync replicas than required.

This is the setting working as designed. With one copy left, accepting the write would mean one more disk failure loses it, so Kafka says no and the producer gets an error it can retry or alert on.

Elr: 2 is new in Kafka 4.x (KIP-966, eligible leader replicas). Broker 2 had all the committed data when it dropped out, so the controller remembers it as a safe leader candidate even though it isn’t in the ISR any more.

4. The surprise: acks=1 “succeeds” but nobody can read it. Same cluster, same single broker, acks=1:

echo pay-8 | bin/kafka-console-producer.sh --bootstrap-server localhost:29091 \
  --topic payments --command-property acks=1

The producer reports no error and the record is on broker 1’s disk (kafka-dump-log.sh shows payload: pay-8 at offset 5 of partition 2). But kafka-get-offsets.sh still says payments:2:5 and a consumer from the beginning stops after pay-5. In Kafka 4.x the high watermark does not advance while the ISR is below min.insync.replicas, so the record stays invisible. acks=1 only bought you a faster “OK”.

5. Restart broker 2. Within 20 seconds it catches up, rejoins the ISR on all three partitions (Isr: 1,2), the high watermark moves to 6, and pay-8 becomes readable at offset 5. Note that broker 1 is still the leader of partition 1 even though broker 2 is its preferred leader. The cluster moves leadership back on its own only when the imbalance check runs (leader.imbalance.check.interval.seconds, 300 s by default), so right after a restart one broker can carry most of the traffic.

What to take from it: replication factor decides how many copies exist, min.insync.replicas decides how many must be alive to accept writes, and acks decides whether the producer waits to find out.

Common mistakes

  • RF=3 with min.insync.replicas=1 (the default). The cluster keeps acknowledging acks=all writes with a single copy alive. Then that broker’s disk dies and those writes are gone. Use min.insync.replicas=2 with RF=3.
  • Setting min.insync.replicas equal to the replication factor. RF=3 and min ISR 3 means any single broker restart, including a routine rolling upgrade, stops all acks=all writes. Always leave one broker of headroom.
  • Thinking acks=all means “all replicas”. It means all replicas currently in the ISR. If the ISR has shrunk to one, acks=all is as weak as acks=1 unless min.insync.replicas stops it.
  • Expecting order across partitions. Order holds per partition. If two events about the same order must stay in order, give them the same key.
  • Picking partition count by guessing small. You can add partitions later, but keyed data then hashes to different partitions and per-key order breaks at the moment of the change. Size for the throughput you expect in a year or two.
  • Running RF=3 on fewer than three brokers. Topic creation fails with InvalidReplicationFactorException. In development use RF=1 on a single broker and know that you are testing none of the above.
  • Using Kafka as a work queue with one consumer per message. A partition is read by one consumer in a group, so 6 partitions cap you at 6 parallel consumers. Kafka 4.x share groups (queues) relax this, but the default consumer group model does not.

ZooKeeper vs KRaft

Older clusters store metadata (which brokers exist, who leads each partition, topic configs) in a separate ZooKeeper ensemble. That is a second distributed system to deploy, secure, monitor and upgrade, and the controller had to reload all metadata from it on failover, which is slow for clusters with hundreds of thousands of partitions.

KRaft keeps metadata in Kafka itself, in a replicated __cluster_metadata log managed by a small quorum of controller nodes using Raft. It has been production-ready since Kafka 3.3, and Kafka 4.0 removed ZooKeeper support entirely. New clusters use KRaft; existing ZooKeeper clusters must migrate to KRaft on a 3.x release before upgrading to 4.x.

When to Use Kafka

Perfect fit:

  • Event streaming pipelines

  • Real-time analytics

  • Microservices event bus

  • Log aggregation

  • Change data capture

  • IoT data ingestion

Not ideal:

  • Simple request/response (use REST/gRPC)

  • Small-scale apps (<1000 msg/s)

  • Complex routing logic (use RabbitMQ)

  • Guaranteed ordering across partitions

  • Sub-millisecond latency requirements

Want the whole picture in one place? Kafka architecture explained has a full diagram and shows what a three-broker cluster does when a broker fails.

Hands-on exercise: partitions and ordering

Goal: see with your own eyes that keys pick partitions and that ordering only holds inside a partition.

Set up a local Kafka (once for the whole course)

Pick one option. Both give you a single-broker cluster in KRaft mode on localhost:9092.

# Option A: Docker
docker run -d --name kafka -p 9092:9092 apache/kafka:3.9.0
# Run the CLI tools inside the container, e.g.:
# docker exec -it kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --list

# Option B: plain Java 17+ (no Docker)
curl -LO https://archive.apache.org/dist/kafka/3.9.0/kafka_2.13-3.9.0.tgz
tar xzf kafka_2.13-3.9.0.tgz && cd kafka_2.13-3.9.0
bin/kafka-storage.sh format -t "$(bin/kafka-storage.sh random-uuid)" -c config/kraft/server.properties
bin/kafka-server-start.sh config/kraft/server.properties

The exercises write bin/kafka-...sh. With Docker, use docker exec -it kafka /opt/kafka/bin/kafka-...sh instead.

Steps

  1. Create a topic with 3 partitions:
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 3 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders
  1. Produce keyed messages (key:value, one per line, then Ctrl+D):
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders \
  --property parse.key=true --property key.separator=:
alice:order-1
bob:order-2
carol:order-3
dave:order-4
alice:order-5
bob:order-6
alice:order-7
  1. Read everything back and print where each message lives:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --from-beginning \
  --property print.key=true --property print.partition=true --property print.offset=true

Questions: Which partition did each key go to? Are all of alice’s orders in order? Is the output in the order you typed it?

What you should see

On a fresh topic you get output like this (Kafka hashes each key with murmur2, so the same key always lands on the same partition):

Partition:2	Offset:0	carol	order-3
Partition:1	Offset:0	dave	order-4
Partition:0	Offset:0	alice	order-1
Partition:0	Offset:1	bob	order-2
Partition:0	Offset:2	alice	order-5
Partition:0	Offset:3	bob	order-6
Partition:0	Offset:4	alice	order-7
  • Every message with the same key is in one partition, and within it the offsets follow the order you produced (alice: 1, 5, 7).
  • The overall output is not in the order you typed: the consumer reads partitions independently. That is the “ordering only within a partition” rule.
  • Each partition numbers its own offsets from 0. The --describe output shows each partition’s leader and ISR; with one broker, both are broker 1.

Key Takeaways

  1. Kafka is a distributed commit log, not a queue
  2. Partitions are your concurrency dial - more partitions = more parallelism
  3. Ordering is only guaranteed within a partition
  4. RF=3 with min.insync.replicas=2 and acks=all is the safe default: lose one broker and keep writing, lose two and writes are refused instead of being risked
  5. Use KRaft; Kafka 4.0 removed ZooKeeper
  6. Choose Kafka for event streaming, not simple messaging

Quiz

  1. Kafka keeps messages after they are read. Which property of its data model makes replay possible?
  2. Messages for one topic land in partitions 0, 1 and 2. Where is ordering guaranteed?
  3. A topic has 3 replicas and min.insync.replicas=2. How many broker failures can it tolerate while still accepting acks=all writes?
Show answers
  1. It’s an append-only, immutable log: consumers just track an offset, so any consumer can re-read from an earlier offset as long as the data is retained.
  2. Only within a single partition. There is no ordering guarantee across partitions.
  3. One. With one broker down, two in-sync replicas remain, which still meets the minimum of 2.

Done with this lesson?

Marking it complete updates the sidebar, the course page and the Continue button.