Log

  • Open one local partition replica to see segment files, sparse indexes, append and cleanup.
100%
Broker A · orders-0 partition directoryClosed segment · base 100Active segment · base 104One partition replica on a broker diskFetchIndex maintenanceSegment IOselect segment 100seek and scanindex entriesappend batchConsumer fetchOffset 103Producer writeKey DTime + offset indexes102 at byte 896100.log · records100, 101, 102, 103Offset and timeindexes104.log · records104 and 105select segment 100seek and scanindex entriesappend batch

Select a component to trace its direct connections. Drag to pan · Ctrl + wheel or pinch to zoom.

  • A partition offset numbers a record. A segment stores batches of records in one file. Its base offset marks the start of its offset range.
  • Base 100 is closed and still readable. The next order appends to the active segment, base 104, at offset 106.
  • The sparse offset index samples batch locations. To fetch 103, start at the indexed batch for 102, byte 896, and scan forward.
  • The time index gives a starting offset for a timestamp search. The broker uses the offset index to reach the bytes, then checks record timestamps.
  • A fetch and an append can touch different files on the same disk. In this example the fetch reads 100.log while the append extends 104.log.
  • If cleaning removes the older value at offset 100, fetching 100 can next return 101. Compaction preserves the surviving records’ offsets.
Log segments, reads and writes

Log segments, reads and writes

Apache Kafka 4.1 · illustrative cluster and offsets

One directory per local partition replica

  • A record is one event. A batch groups records for storage and transfer. Each record keeps its own offset even when several share one batch.
  • On a broker, the orders-0 directory holds one replica of the orders topic’s first partition. Each segment has a .log data file, a .index offset index and a .timeindex timestamp index.
  • Segment filenames share a zero-padded base offset. The example shortens them to 100.log and 104.log. When size or time triggers a roll, Kafka closes one segment to appends and opens the next.
Partition segment and compaction examplejson
{
  "scope": "Illustrative local orders partition 0 replica with one record per batch. Keys and values are abbreviated, byte positions are relative to each data file, and times are on the same day in UTC.",
  "directory": "orders-0",
  "index.interval.bytes": 700,
  "segments": [
    {
      "baseOffset": 100,
      "active": false,
      "records": [
        {"offset":100,"key":"A","value":"v1","timestamp":"10:00:00","bytePosition":0},
        {"offset":101,"key":"B","value":"v1","timestamp":"10:00:03","bytePosition":448},
        {"offset":102,"key":"A","value":"v2","timestamp":"10:00:08","bytePosition":896},
        {"offset":103,"key":"C","value":"v1","timestamp":"10:00:10","bytePosition":1248}
      ],
      "sparseOffsetIndex": [{"relativeOffset":2,"bytePosition":896}],
      "sparseTimeIndex": [{"timestamp":"10:00:08","relativeOffset":2},{"timestamp":"10:00:10","relativeOffset":3}]
    },
    {
      "baseOffset": 104,
      "active": true,
      "records": [
        {"offset":104,"key":"E","value":"v1","timestamp":"10:00:11","bytePosition":0},
        {"offset":105,"key":"F","value":"v1","timestamp":"10:00:12","bytePosition":432}
      ],
      "sparseOffsetIndex": [],
      "sparseTimeIndex": []
    }
  ],
  "fetch": {"requested":103,"baseOffset":100,"indexFloor":102,"startBytePosition":896,"resultBytePosition":1248},
  "timestampSearch": {"requested":"10:00:09","baseOffset":100,"timeIndexFloor":"10:00:08","indexFloor":102,"startBytePosition":896,"firstMatchingOffset":103},
  "append": {"key":"D","baseOffset":104,"assignedOffset":106},
  "afterEligibleCompaction": {"supersededKey":"A","removedOffsets":[100],"survivingClosedOffsets":[101,102,103],"firstRecordForFetch100":101}
}

Fetch by offset or by timestamp

Lookup Index entry What the broker reads
Fetch offset 103 Base 100 contains 103. The nearest indexed offset at or below 103 is 102, at byte 896. Seek to 896 in 100.log and scan batches. The batch at byte 1248 contains record 103.
Find the first record at or after 10:00:09 The time index supplies the earlier sample 10:00:08 → offset 102. The offset index gives byte 896. Starting there, scan timestamps. Record 103 at 10:00:10 qualifies. A consumer can now seek to 103.
  • The example uses one record per batch. On disk the offset index stores offsets relative to the segment base. Its entry for 102 stores relative offset 2 and byte position 896. In a multi-record batch the indexed offset can be the batch’s last offset and the position points to its start.
  • Between index samples Kafka scans forward. A smaller index.interval.bytes can shorten the scan at the cost of larger indexes.

Log compaction and deletion

Topic policy Storage behavior Consequence for replay
delete Expire eligible old segments by time or size A group can lose its recovery point when the log start advances
compact Background cleaning can remove older values superseded by a newer record for the same key in that partition Surviving records retain offsets and ordering. Reading a compacted hole returns the next available record
compact,delete Apply both policies A latest key value can disappear when its retained segment expires
  • In the fixture A at 102 supersedes A at 100. After a cleaning pass, an eligible old value at 100 can be removed and B at 101 becomes the first surviving record. Cleanup runs asynchronously.
  • A keyed null value represents a tombstone. Consumers rebuilding key state need a retention window long enough to observe deletions. Budget cleaner IO and transient space while old segments are rewritten.