Free preview

Topics, Partitions, and Segments

Why this matters: this is the structure that solves Lesson 4's head-of-line blocking. Immutable records addressed by offset are exactly what let many consumers read independently from one copy.

Key takeaway

A topic is a persistent, immutable sequence of messages. Because disk I/O limits scaling, topics are split into partitions, and a partition consists of messages encapsulated in segments. Segments define the start and end of a message using an offset address, and consumers read from a specific partition offset.

The hierarchy

LevelWhat it is
TopicA persistent, immutable sequence of messages. The logical thing producers write to and consumers subscribe to
PartitionA physical division of a topic, stored locally on a broker. Data for a single topic is distributed across multiple partitions
SegmentThe unit inside a partition — messages encapsulated in segments, each defining the start and end of a message using an offset address

Why partitions exist

As disk I/O limits scaling, we split topics into partitions.

Segments and offsets

Messages are stored in segments and identified by an offset. Because records are immutable, readers can access messages independently from any point in the file using the API.

Segment (append-only file)

[offset: 0][offset: 1][offset: 2][offset: 3][offset: 4][offset: 5][offset: 6][offset: 7]
     ^                                                                            ^
 First entry written                                                    Next entry to write

Producer x  ---- writes ---->  appends at the end
Consumer 1  ---- reads  ---->  offset 2
Consumer 2  ---- reads  ---->  offset 6

Both read the same file, at different positions, without blocking each other.

Key takeaway

Topic → partitions → segments → offsets. Partitions exist because disk I/O bounds a single broker, and they double as the parallelism ceiling for consumers. Segments make retention deletion cheap. And immutable records addressed by offset are what let many consumers read one copy without blocking each other.

Interview signal by level

LevelWhat a strong answer sounds like
L4"Topics are split into partitions across brokers."
L5Gives the reason and the mechanism: "partitions exist because one broker's disk I/O caps throughput, and within a partition messages sit in append-only segments addressed by offset, so consumers read from wherever they are."
Staff+Connects it back to the failed design and forward to capacity: "immutability plus per-reader offsets is what fixes head-of-line blocking — a queue only serves from the head, so one slow consumer froze everyone; a log gives every reader an independent cursor. Partition count is also the consumer parallelism ceiling, so I'd over-provision, because adding partitions later redistributes keys and breaks per-key ordering."

Next: which partition a message goes to, and how to force ordering.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue