Video in progress
How is a Kafka partition stored on disk: how do segments, indexes and retention work?
A partition is a directory containing segment files (.log) and their offset and time indexes. Retention deletes whole segments, not individual messages.
📖 Answer
On disk it looks like this:
/var/kafka-logs/orders-3/
00000000000000000000.log
00000000000000000000.index
00000000000000000000.timeindex
00000000000005230145.log <- active segment
00000000000005230145.index
...
The file name is the first offset in the segment. The .index is sparse: for every few KB it holds an "offset → position in the file" entry. If a consumer asks for offset 5230200, the broker finds the right segment by name, takes the nearest position from the index, and reads the rest sequentially. The .timeindex is for offsetsForTimes().
A new segment is opened when segment.bytes (1 GB by default) fills up or segment.ms passes.
Retention (retention.ms, retention.bytes) applies only to closed segments. The practical consequence: on a low-traffic topic, even if you set retention.ms=1h, if the 1 GB segment doesn't fill up for a week, the data stays for a week. For GDPR or to save disk, you also need to reduce segment.ms.
The speed comes from this structure too: append-only writes, sequential reads, the OS page cache and zero-copy (sendfile). If a consumer reads from the page cache, the disk is barely touched.
Sign up to mark studied questions and save your progress.