Apache Kafka — Event Streaming from Basics to a Platform Service›03 · Core concepts

Lesson 03 of 17 · Foundations

Core concepts

The vocabulary of Kafka, with one picture that ties it together: brokers and clusters, topics split into partitions, offsets, the parts of a record (key, value, headers, timestamp), how the key chooses a partition, producers, consumers and consumer groups, retention, replication, and KRaft controllers (ZooKeeper is gone since Kafka 4.0).

Beginner
Key wordsbrokerclustertopicpartitionoffsetrecordkeyvalueheaderstimestampproducerconsumerconsumer groupretentionreplication factorleaderfollowerKRaftcontrollerZooKeeper removedbootstrap server

The whole picture

                          Kafka cluster
        ┌───────────────────────────────────────────────────┐
        │  broker 1          broker 2          broker 3      │
        │  orders-P0 (L)     orders-P1 (L)     orders-P2 (L) │   L = leader
        │  orders-P1 (F)     orders-P2 (F)     orders-P0 (F) │   F = follower copy
        │  orders-P2 (F)     orders-P0 (F)     orders-P1 (F) │
        │        controllers (KRaft): cluster metadata       │
        └───────────────────────────────────────────────────┘
   producers ──► write to partition leaders
   consumer group "billing":   C1 ← P0, P1     C2 ← P2
   consumer group "analytics": C1 ← P0, P1, P2      (gets everything, separately)

Brokers and clusters

A broker is one Kafka server process. It stores data on its disks and answers clients. A cluster is several brokers; production clusters usually have at least three, spread across availability zones. Clients connect to any broker listed in their bootstrap servers setting, learn the cluster layout from it, then talk directly to the brokers they need.

Topics, partitions and offsets

A topic is a named stream, such as orders. Each topic is split into partitions. A partition is one ordered, append-only log:

orders, partition 0:   [0] [1] [2] [3] [4] [5]  ← new records are added at the end
orders, partition 1:   [0] [1] [2] [3]
orders, partition 2:   [0] [1] [2] [3] [4]

The number in brackets is the offset: the record's position in that partition. Offsets only grow and are never reused. Offset 3 in partition 0 and offset 3 in partition 1 are different records.

Partitions are how Kafka scales: different partitions live on different brokers and are read by different consumers in parallel. They also define ordering: Kafka guarantees order only within a partition.

Records and keys

A record (also called a message or event) has:

Part Example Notes
Key order-123 Optional; chooses the partition
Value {"id":123,"total":45} The payload, as bytes (JSON, Avro, Protobuf…)
Headers trace-id: abc Optional metadata
Timestamp 2026-11-08T10:02:00Z Set by the producer or the broker

The key decides the partition: Kafka hashes the key and picks a partition from the hash. Every record for order-123 lands in the same partition, so the events for that order stay in order. Records without a key are spread across partitions for balance.

Adding partitions changes where keys go

The hash is taken modulo the number of partitions. Increase the count from 6 to 12 and many keys move to a different partition, so for a while old and new events for the same key are in different partitions. Choose the count with growth in mind.

Producers, consumers and consumer groups

  • A producer writes records to a topic.
  • A consumer reads records and remembers how far it has got by committing its offset to Kafka.
  • A consumer group is a set of consumers sharing the work of reading a topic. Kafka gives each partition to exactly one consumer in the group.
Topic partitions Consumers in the group Result
6 1 One consumer reads all 6
6 3 Two partitions each
6 6 One each: maximum parallelism
6 8 Six busy, two idle

Different groups are completely independent: billing and analytics both receive every record, each with its own offsets. That is how one topic feeds many systems.

A topic is like a set of numbered checkout lanes in a supermarket. Each lane (partition) is a queue of customers in order. A team of cashiers (a consumer group) splits the lanes so that each lane has exactly one cashier. Another team (a second group) can watch the very same lanes to count shoppers, without getting in the first team's way. If there are more cashiers than lanes, some cashiers have nothing to do.

Retention: data stays after it is read

Kafka deletes old data by retention policy, not because someone read it:

  • by time: retention.ms (the default is 7 days);
  • by size: retention.bytes per partition;
  • or by compaction: keep only the latest record for each key (lesson 9).

A consumer that is offline for a day can catch up, as long as the data is still retained.

Replication

Each partition has a replication factor, usually 3: one leader and two followers on other brokers. Producers and consumers talk to the leader; followers copy it. If the leader's broker fails, a follower that is fully up to date becomes the new leader. Lesson 8 covers this in depth.

KRaft: the cluster's brain

Some Kafka nodes act as controllers. They keep the cluster metadata: which topics exist, where every partition lives, who leads it. They agree with each other using the Raft consensus protocol, so the mode is called KRaft. Older Kafka needed a separate ZooKeeper cluster for this; Kafka 4.0 removed ZooKeeper entirely. Small clusters can run nodes that are both broker and controller; large ones use dedicated controllers (three or five).

Try it: reason about a design

  1. A topic payments has 12 partitions. How many consumers in one group can work in parallel?
  2. All events for one customer must be processed in order. What should the key be?
  3. Analytics and fraud both need every payment. How many consumer groups do you need?
  4. A consumer is down for two days and retention is 7 days. What happens when it restarts? What if retention were 1 day?

Going deeper

  • Partition count is a capacity decision: it caps consumer parallelism and affects broker memory and recovery time. Hundreds per topic are normal for very large flows; most topics need far fewer.
  • The key is a design decision, not an afterthought: it defines ordering, data locality and how evenly load spreads. A key with a few very hot values (one huge customer) creates a hot partition.

Recap

  • A cluster of brokers stores topics, each split into partitions: ordered logs with offsets.
  • The key chooses the partition, so the same key stays in order.
  • Each partition goes to one consumer per group; different groups each get all data.
  • Data is removed by retention, not by reading; partitions are replicated; KRaft controllers keep the metadata.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.