Learn Labs
Storage

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 id

The 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.

orders-0/0closed…closed1.84Mclosed…closed3.72Mactive← only the active segment is writtenoldest: deleted firstrolls at segment.bytes

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: v3
baseOffset / lastOffsetoffsets covered by this batchproducerId / sequenceused by the idempotent producer to drop duplicatespartitionLeaderEpochwhich leadership term wrote it (for truncation after failover)isTransactional/isControltransaction markers (commit/abort) are "control" batchessize: 116five tiny records cost 116 bytes: per-batch overhead dominates

The indexes

To serve "fetch from offset 1,500,000", Kafka must find that offset's byte position without scanning the file.

  • .index maps offsets to byte positions in the .log. It's sparse: one entry every index.interval.bytes (4 KB) of log. Kafka binary-searches the index for the nearest entry below the target, then scans forward a little.
  • .timeindex maps timestamps to offsets, which powers offsetsForTimes() 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.

Lab

  1. Produce to demo and list a partition directory on two brokers. Are the file sizes identical? (They should be close: followers copy the same batches.)
  2. Dump a segment with --print-data-log. Find the producer id and sequence numbers.
  3. Create a topic with --config segment.bytes=1048576 (1 MB), produce 5 MB with kafka-producer-perf-test.sh, and list the directory. Count the segments and read their base offsets.
  4. Dump the .index file with kafka-dump-log.sh --files ...index. How many entries does it have relative to the record count?

On this page