Apache Kafka Streaming (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Kafka Storage Internals: How Segment Files (`.log`), Index Files (`.index`), and Time Indexes (`.timeindex`) work.
senior LevelMost AskedCode ExampleSystem DesignApache KafkaMicroservices
In Simple Words: Each partition directory on a broker disk contains segment files:
css
/var/lib/kafka/data/orders-0/
├── 00000000000000000000.log # Raw message bytes
├── 00000000000000000000.index # Offset-to-physical-byte-position index
├── 00000000000000000000.timeindex # Timestamp-to-offset index
└── leader-epoch-checkpoint
Fast Offset Lookup Mechanics:
- Kafka does not index every single message (sparse indexing every 4KB via
index.interval.bytes). - When a consumer requests Offset 45,000:
- Kafka performs a binary search on the memory-mapped
.indexfile to find the nearest offset $\le 45,000$ (e.g., Offset 44,950 at byte position1,245,000). - The OS seeks directly to byte position
1,245,000in the.logfile and scans sequentially until it reaches Offset 45,000. - Result: Lookup completes in sub-millisecond time even in multi-terabyte partitions.
- Kafka performs a binary search on the memory-mapped