Topics and partitions
A topic is a named log split into partitions; each event gets the next offset in its partition, order holds within a partition only, and reading never deletes.
Diagrams in this video · 1Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Say what a topic and a partition are
- Explain offsets and a consumer's position
- Explain where ordering holds, and where it doesn't
- Explain why reading an event doesn't remove it
Transcript
Topics and partitions
Space Llama here. Today, where Kafka actually keeps your events. Two words do most of the work: topic, and partition. By the end, you'll know exactly what an offset is, and where Kafka promises order. This is all core Apache Kafka, so it works the same wherever you run it.
Topics
Events are grouped into topics. A topic is a named log, like orders, or payments. Producers write events to a topic, and consumers read from it. A topic can have many producers and many consumers at the same time. And each topic has its own retention, how long its events are kept. More on that next lesson.
A topic, step by step
Here's a topic called orders, with a producer writing on the left and two consumers reading on the right. A producer sends an event. It names the topic, orders, and that's all it needs to know. The topic is split into partitions. Here there are three: P zero, P one and P two. Each new event in a partition gets the next number, its offset. Partition zero's newest event is offset seven. Offsets only grow. New events always go on the end. A consumer reads each partition in offset order. Within a partition, order is guaranteed. Across the topic, it isn't. And reading deletes nothing. A second consumer can read the very same events, at its own pace.
Inside each cell
Each of those numbered cells is one event. It carries a key, a value, a timestamp and, if you like, some headers. The docs call them events, records or messages. In this course, they're all the same thing.
Where order holds
Here's the rule that catches people out. Inside one partition, a consumer always reads events in exactly the order they were written. Across a whole topic, there's no such promise. Each partition is its own ordered log. So if order matters, give related events the same key. Events with the same key go to the same partition. Choose keys well, and you get order where it matters, without giving up partitions. Keys get a whole lesson in part three.
An offset is a position
One more thing about offsets. A consumer's position in a partition is just one number, the offset of the next event to read. Kafka stores those positions too, as messages in an internal topic. Offsets belong to a partition, not the topic. So P zero and P one each have their own offset zero. Because consumers pull, a slow one simply falls behind, and catches up when it can. That's how each consumer keeps its own place, without touching anyone else's.
Remember
So. A topic is split into partitions, and each event gets the next offset. Order holds within a partition, not across the topic. And reading never deletes, so many consumers can share the same events.
Practice and what's next
Next lesson, Retention and compaction. If reading doesn't delete events, what does? See you there.
Gazeek Flash · free
Exam questions on your phone
Free questions on How Kafka holds events: quick-fire batches and timed exams, on the web or Android.





