apache-kafka

Why kafka splits partitions into segments?


I clearly understand the algorithm behind splitting partitions into segments, and retention and clean up strategies. But still its unclear why kafka not writing\reading in one file per partition?


Solution

  • Kafka does write to one file per partition. That's the head segment. The rest of the segments are immutable (ignoring compaction).

    If one file were used per partition for reading, it would take a long time to seek to individual offsets in each file. Segments include offset and timestamp markers that make it quicker to seek to.

    If writes never closed segments, there would never be any closed file handles for the segments to be deleted through retention, so you'd eventually run out of disk space.