Log Segments
How a partition is stored: segments, offset and time indexes, and batches
Kafka's storage is unusually simple, and that simplicity is a big part of its performance. A partition is a directory; the log is a series of files you only ever append to.
A partition on disk
docker exec kafka-1 ls -la /var/lib/kafka/data/demo-2
-rw-r--r-- 10485760 00000000000000000000.index # offset index (preallocated)
-rw-r--r-- 116 00000000000000000000.log # the records
-rw-r--r-- 10485756 00000000000000000000.timeindex # time index (preallocated)
-rw-r--r-- 8 leader-epoch-checkpoint # leader epochs, for safe truncation
-rw-r--r-- 43 partition.metadata # topic idThe log is split into segments. Each segment is a set of files named
after the first offset it contains (its base offset), so
00000000000000000000.log starts at offset 0. When the active segment
reaches segment.bytes (256 MiB in this cluster) or segment.ms (7 days by
default), Kafka closes it and starts a new one:
00000000000000000000.log # offsets 0 .. 1,839,212 (closed)
00000000000001839213.log # offsets 1,839,213 .. now (active: the only one written to)Closed segments are never modified again. Retention and compaction work on whole segments, and the active segment is never deleted or compacted.
Each cell is a segment named by its base offset (1.84M is the file
00000000000001839213.log, with its .index and .timeindex beside it).
Records and batches
Producers don't send single records; they send batches, and Kafka stores
batches on disk exactly as they arrived, compressed or not. That's why
kafka-dump-log.sh shows a batch header followed by its records:
kafka-dump-log.sh --files .../demo-2/00000000000000000000.log --print-data-log
baseOffset: 0 lastOffset: 4 count: 5 baseSequence: 0 lastSequence: 4
producerId: 2001 producerEpoch: 0 partitionLeaderEpoch: 0
isTransactional: false isControl: false position: 0 CreateTime: 1791799990493 size: 116 magic: 2
| offset: 0 CreateTime: 1791799990488 keySize: 2 valueSize: 2 sequence: 0 key: k0 payload: v1
| offset: 1 ... key: k1 payload: v2
| offset: 2 ... key: k0 payload: v3Because batches are stored as received, a broker never decompresses and
recompresses data on the normal path. And when a consumer fetches, the
broker can send file bytes straight from the page cache to the socket
with sendfile() (zero-copy), without copying them through the JVM. Those
two decisions are a large part of why one broker can serve hundreds of
MB per second. The book's Kafka Internals
chapter covers the physical storage in more depth.
The indexes
To serve "fetch from offset 1,500,000", Kafka must find that offset's byte position without scanning the file.
.indexmaps offsets to byte positions in the.log. It's sparse: one entry everyindex.interval.bytes(4 KB) of log. Kafka binary-searches the index for the nearest entry below the target, then scans forward a little..timeindexmaps timestamps to offsets, which powersoffsetsForTimes()and "reset to a datetime".
Both are memory-mapped and preallocated (the 10 MB files above), then trimmed when the segment closes.
The page cache, not the heap
Kafka doesn't cache data in the JVM. It writes to the OS page cache and relies on it for reads. Consumers that are caught up read recent data straight from memory; only consumers far behind touch the disk. That's why brokers run with a smallish heap (512 MB in this lab, a few GB in production) and leave the rest of the RAM to the OS.
Kafka also doesn't fsync on every write by default. Durability comes
from replication: a record acknowledged with acks=all is in the page
cache of at least min.insync.replicas brokers. Losing it would require
all of them to lose power before flushing. Forcing fsync
(log.flush.interval.messages=1) is possible and rarely worth the cost.
Lab
- Produce to
demoand list a partition directory on two brokers. Are the file sizes identical? (They should be close: followers copy the same batches.) - Dump a segment with
--print-data-log. Find the producer id and sequence numbers. - Create a topic with
--config segment.bytes=1048576(1 MB), produce 5 MB withkafka-producer-perf-test.sh, and list the directory. Count the segments and read their base offsets. - Dump the
.indexfile withkafka-dump-log.sh --files ...index. How many entries does it have relative to the record count?