Sub topic 1
How Kafka organizes data: topics, partitions and offsets
Prerequisites
This chapter assumes you can read basic Java and Spring Boot code. It also assumes you know what publish/subscribe messaging is, and that you can run a local Kafka broker, for example with Docker. It uses no Spring code yet. It explains the Kafka model that every later chapter relies on.
The working model
Kafka stores messages in an append-only log. Four terms describe how that log is organized:
- A broker is a Kafka server. A cluster has one or more brokers.
- A topic is a named stream of records, such as
orders. - A partition is one ordered log that holds a slice of a topic. A topic has one or more partitions.
- An offset is the position of a record inside one partition. Offsets start at 0 and increase by one for each record.
A record has a key, a value, a timestamp and optional headers. The key and value are bytes to Kafka. Serialization is covered in a later chapter.
An offset is only meaningful together with its topic and partition. Offset 1 in partition 0 and offset 1 in partition 2 are different records.
How records reach a partition
The producer picks the partition for each record:
- If the record has a key, Kafka hashes the key and maps the hash to a partition. The same key always goes to the same partition, as long as the partition count does not change.
- If the record has no key, the producer spreads records across partitions. The exact strategy depends on the client version.
This rule explains the ordering guarantee, which is the most important thing to remember:
- Kafka guarantees order within one partition.
- Kafka does not guarantee order across partitions of a topic.
If all events for one order must be processed in sequence, give them the same key, such as the order ID. They then land in one partition and keep their order.
Partitions set the limit on parallelism
A partition is read by only one consumer at a time within a consumer group. The number of partitions is therefore the maximum number of consumers in one group that can work in parallel. A topic with three partitions can keep three consumers busy. A fourth consumer in the same group would sit idle. The next chapter covers consumer groups in detail.
More partitions give more parallelism, but they also add overhead on the brokers and make key-to-partition mapping less stable when you change the count.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Expecting global order across a topic | Records from different partitions arrive interleaved | Use a key so related records share a partition |
| Adding partitions to a live topic that relies on keys | The hash mapping changes, so a key can move to a new partition and its new records may be processed out of order relative to old ones | Choose the partition count with growth in mind, up front |
| Treating an offset as a global record ID | The same offset number exists in every partition | Identify a record by topic, partition and offset |
| Expecting a record to disappear after it is consumed | It stays until retention removes it | Design consumers to track their own position (offsets) |
Where offsets matter next
Each consumer group stores, per partition, the offset of the next record it should read. This stored position is called a committed offset. If a consumer restarts, it resumes from there. Later chapters show how Spring decides when to commit and what happens if processing fails before a commit.
Exercise
An orders topic has 4 partitions. You publish three events for order "A-17" with the key "A-17", and three events for order "B-02" with the key "B-02". Which ordering guarantees hold? Also, what is the most consumers in one group that can read this topic in parallel?
Show answer
All three events for "A-17" go to the same partition, so they are read in the order they were sent. The same holds for "B-02". Kafka gives no guarantee about the order between A-17 events and B-02 events, because they may be in different partitions. At most 4 consumers in one group can read in parallel, one per partition. A fifth would be idle.
Exercise
A record has offset 5. Is that enough to find it? Explain.
Show answer
No. Offsets are per partition, so you also need the topic name and the partition number. Offset 5 exists in every partition that has at least six records.