Topics, partitions and consumer groups
Picture a newspaper archive rather than a mailbox. A mailbox empties when you take a letter out; an archive keeps every issue on the shelf, and each reader keeps a bookmark of how far they’ve read. Kafka is the archive.
- A topic is one shelf, for one kind of event:
orders,payments. - A topic is split into partitions, numbered rows on the shelf. Each new event is appended to the end of one partition and gets a number, its offset.
- Every event has a key, such as the order id. Events with the same key always go to the same partition, so they’re read in the order they were written.
- A consumer group is one team of readers, for example “warehouse”. Every group sees every event. Within a group, each partition is read by exactly one member, so adding members (up to the number of partitions) reads faster.
- Each group remembers its bookmark: the committed offset. Events aren’t deleted when read; they stay for the retention period (say 7 days) so other groups, or a replay, can read them again.
A topic is a set of append-only partition logs, replicated across brokers (replication factor 3, min.insync.replicas=2 is the usual durable setup). The producer’s partitioner hashes the key to choose a partition, so per-key ordering holds as long as the partition count doesn’t change. Consumers in a group split partitions between them; the group coordinator rebalances on joins, leaves and missed heartbeats or poll timeouts. Progress is the committed offset per partition, which makes replay (reset offsets) and independent consumers cheap. Parallelism is capped by partition count; ordering is only within a partition, never across a topic.
One topic, two consumer groups
Tap a box to see what it does.
Checkout
Publishes OrderPlaced with the order id as the key, so all events for one order land in the same partition, in order.
orders · partition 0
An append-only log. Offsets 0, 1, 2… Events stay for the retention period, read or not.
orders · partition 1
Another log with its own offsets. Each order id always lands in the same partition.
orders · partition 2
Ordering holds inside a partition, never across partitions.
Warehouse #1
Group "warehouse": its two instances split the three partitions. A third instance would get one each; a fourth would sit idle.
Warehouse #2
Reads partition 2. If it crashes, a rebalance moves partition 2 to Warehouse #1, which continues from the committed offset.
Group "email" reads every partition on its own, with its own offsets. It can be slower or down without affecting the warehouse.
Predict the output
The orders topic has 6 partitions. The warehouse service scales to 10 instances in the same consumer group. How many instances receive messages?
Within a group, each partition is assigned to one consumer, so at most 6 instances get work and 4 sit idle as standby. Partition count is the ceiling on parallelism for a group. Choose it with growth in mind (adding partitions later changes which partition a key maps to, breaking per-key ordering for a while), or process each partition's records concurrently inside one instance.
- Topic = replicated partition logs (RF 3,
min.insync.replicas=2). Key → partition hash: per-key ordering, none across partitions. - Consumer group: each partition to one member; parallelism ≤ partitions; rebalances on membership changes and poll timeouts.
- Committed offsets per group per partition; retention independent of consumption, so replay and new consumers are cheap.
- Adding partitions remaps keys: plan the count up front.
Check this part
Why use the order id as the message key?
Does reading an event remove it from the topic?
What limits how many consumers in one group can work in parallel?