Quick revision
The short answer
The broker keeps the partition's segments in a sorted map keyed by base offset, so it first picks the segment with the largest base offset not greater than 48,213,907. In that segment's memory-mapped offset index it binary-searches for the last entry at or below the target, which gives a byte position, and scans the .log forward batch by batch until it reaches the batch containing the offset. The index is sparse, one entry per about 4096 bytes, so it stays small enough to live in memory, and because batches are stored and sent whole, the consumer can receive records below its fetch offset and simply skips them.
What to remember
In this answer, we followed one fetch: a floor lookup in the sorted map of segments, a binary search in the memory-mapped sparse offset index, and a forward scan of batch headers in the .log file to the batch containing the target. The index is sparse to stay small and in memory, at the cost of a short scan. Because batches are stored and sent whole, often compressed and via zero-copy, the consumer may receive records below its fetch offset and drops them, and timestamp lookups use the time index first and then the same offset path.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The broker keeps the partition's segments in a sorted map keyed by base offset, so it first picks the segment with the largest base offset not greater than 48,213,907. In that segment's memory-mapped offset index it binary-searches for the last entry at or below the target, which gives a byte position, and scans the .log forward batch by batch until it reaches the batch containing the offset. The index is sparse, one entry per about 4096 bytes, so it stays small enough to live in memory, and because batches are stored and sent whole, the consumer can receive records below its fetch offset and simply skips them.
Why this question is asked
Every consumer fetch, every replica fetch and every offset reset goes through this lookup, millions of times a day, so its design explains much of Kafka's read performance. It also explains behaviour that confuses people, such as a consumer receiving offsets it did not ask for, or a seek being slightly slower on a cold segment. Interviewers ask it to see whether we know how Kafka finds data without a database index. In this answer, we will find the segment, search the offset index, scan to the batch, see why the index is sparse, see why whole batches come back, and look at how timestamp lookups reuse the same path.
1Finding the Segment
A partition with 300 segments has 300 base offsets, one per segment. The broker keeps the segments in an in-memory sorted map, keyed by base offset (in the Java code it is a concurrent skip list map). Finding the right segment is a floor lookup: the entry with the largest key that is less than or equal to the target.
Notice that this costs a few comparisons, not a scan of 300 files. The reason it works is that segments cover contiguous, non-overlapping offset ranges in order, so the floor entry is always the segment that holds the target, provided the target is not beyond the log end.
2Searching the Sparse Offset Index
Inside the segment, the broker opens its .index file, which is memory-mapped, so it reads like an array in memory. Each entry is eight bytes: a four-byte offset relative to the segment's base and a four-byte byte position in the .log file. The target relative to the base is 48,213,907 minus 48,105,000, which is 108,907.
The broker binary-searches the entries for the last one whose relative offset is at or below 108,907. Suppose it finds relative offset 108,880 at byte position 41,201,664. That tells it: the batch containing offset 48,213,880 starts at byte 41,201,664, so our target is at or after that position.
- Floor lookup in the segment mapsegment with base 48,105,000
- Compute the relative offset48,213,907 - 48,105,000 = 108,907
- Binary-search the offset indexentry 108,880 -> byte 41,201,664
- Scan batch headers forward in .logskip batches that end before the target
- Batch range contains the target?YesSend from this batch's startNoRead the next header
- Consumer drops records below 48,213,907
6 steps in order. Play them, or step through them one at a time.
Two cheap searches and a short scan; no record is decompressed by the broker.
Then it scans the .log file forward from that position, reading only batch headers. Each header carries the base offset and the last offset delta, so the broker can skip whole batches without decompressing them, until it finds the batch whose range includes 48,213,907. That batch's starting position is where the fetch response begins, and the broker returns data from there up to the fetch size limits.
3Why the Index Is Sparse
The broker adds an index entry only after about index.interval.bytes of log data has been written since the last entry, 4096 bytes by default, rather than one entry per record or per batch. With 1 GiB segments that is about 262,000 entries at eight bytes each, roughly 2 MB per segment index. A dense index with one entry per small record could be fifty or a hundred times larger.
Small indexes matter because the index is memory-mapped and Kafka relies on the operating system's page cache. Indexes of recently used segments stay in memory, so the binary search touches no disk. The cost of sparseness is the short forward scan, at most about 4 KB of batch headers, which is usually in the page cache as well. It is a deliberate trade: a little scanning in exchange for indexes that fit in memory for thousands of partitions.
4Why the Consumer Gets Lower Offsets
The batch is the unit of storage, and when compressed, its records are compressed together. The broker cannot cut a batch in half without decompressing it, rebuilding it and recomputing its CRC, which would cost CPU on every fetch and would prevent zero-copy transfer, where the operating system sends file bytes straight to the socket. So the broker returns whole batches, starting with the batch that contains the requested offset.
If that batch covers offsets 48,213,880 to 48,214,379, the consumer receives 27 records below 48,213,907. The consumer client knows its fetch position and silently discards them before poll() returns anything. Application code never sees them; we only notice in low-level tools or in byte counts.
5Timestamp Lookups Use the Same Path
Looking up by time, through consumer.offsetsForTimes() or kafka-consumer-groups.sh --reset-offsets --to-datetime, sends a ListOffsets request. The broker finds the first segment whose newest timestamp is at or after the requested time, then searches that segment's .timeindex, whose entries map a timestamp to a relative offset, for the right position. That gives an offset, and from there the offset index and the forward scan work exactly as above to find the first record with a timestamp at or after the target.
So the two indexes cooperate: the time index turns a time into an offset, and the offset index turns an offset into a byte position.
6Follow-up questions
Why are index files preallocated and then trimmed when a segment rolls?
A memory-mapped file needs a fixed size when it is mapped, and growing the mapping on every append would be expensive. So when a segment is created, its index files are created at log.index.size.max.bytes (10 MB by default) and mapped once. Appends simply write the next eight-byte slot. When the segment rolls, the broker trims each index file to the size actually used, so closed segments do not waste disk or page cache. A segment also rolls early if its index fills up before the log reaches segment.bytes.
What happens if the requested offset was already deleted by retention?
The floor lookup finds nothing valid, and the broker answers with OFFSET_OUT_OF_RANGE. The consumer then applies auto.offset.reset, earliest or latest, or throws if it is set to none.
Is the lookup slower for old data?
The algorithm is the same, but old segments are less likely to be in the page cache, so the index search and the reads go to disk. That is why consumers far behind can raise disk reads and slow the broker for everyone, and why tiered storage moves such reads to object storage.
7Conclusion
In this answer, we followed one fetch: a floor lookup in the sorted map of segments, a binary search in the memory-mapped sparse offset index, and a forward scan of batch headers in the .log file to the batch containing the target. The index is sparse to stay small and in memory, at the cost of a short scan. Because batches are stored and sent whole, often compressed and via zero-copy, the consumer may receive records below its fetch offset and drops them, and timestamp lookups use the time index first and then the same offset path.
Open with
First the broker finds the right segment. It keeps the partition's 300 segments in a sorted map keyed by base offset, so it takes the floor entry for 48,213,907, the segment with the largest base offset not greater than the target.
Then, if they want more
Then it computes the offset relative to that base and binary-searches the segment's offset index, which is memory-mapped, for the last entry at or below the target.
That entry gives a byte position in the .log file, and the broker scans forward reading only batch headers until it finds the batch whose offset range contains the target, and it sends data starting from that batch.
The index is sparse, with one entry about every 4 KB of log, index.interval.bytes, so each index stays a few megabytes and stays in the page cache; the price is a short scan.
The reason the consumer can get offsets lower than it asked for is that batches are the unit of storage and transfer, and compressed batches are compressed as a whole, so the broker returns whole batches, often with zero-copy, rather than cutting them. The consumer client knows its position and drops the earlier records before poll returns.
Time lookups like offsetsForTimes work the same way, using the time index to get an offset first and then the offset index.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how the broker finds the bytes to return, why the offset index is sparse, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/5b68f32067/